AutoRemote Manual POST request to send event to device

Discussion in 'AutoApps' started by MaddoScientisto, Jun 10, 2015.

  1. MaddoScientisto

    MaddoScientisto New Member

    Joined:
    Jun 10, 2015
    Messages:
    7
    Likes Received:
    0
    Hi, I've been looking into using autoremote from a javascript application and I've been successful in sending GET requests with the required parameters and the phone receiving them, all from my javascript app.

    After looking around a while I found out that there seems to be a way to use a POST request through HTTPS instead.
    While I generally know how to build and send a post request from javascript, I'm entirely unsure on where to send the request and what parameters or content I should specify.
    Is there any specification or documentation on the feature?
     
  2. joaomgcd

    joaomgcd Administrator Staff Member

    Joined:
    Feb 3, 2015
    Messages:
    9,476
    Likes Received:
    797
    Everything's exactly the same :) You can use the same endpoints.
    IIRC you only need to send the key parameter in the URL.
    Please let me know if it works. If it doesn't, I'll try it out myself.
     
  3. MaddoScientisto

    MaddoScientisto New Member

    Joined:
    Jun 10, 2015
    Messages:
    7
    Likes Received:
    0
    I tried the following code (using jquery) which didn't really work, I received no response from autotasker

    Code (Text):

    AutoRemote.SendIntent = function(message, key, target, sender, password){
     var testCode = "ExecuteTask PopupTest=:=Spaghetti";
     var url = "https://autoremotejoaomgcd.appspot.com/sendmessage";
     
     var dataObj = { target: target,
      password: password,
      sender: sender,
      message: testCode
     }

     $.post(url+"&key="+key, dataObj, function(result){
      console.log(result);
      });

    }
     
    all I get back is just a generic sent message. Note that putting the data in the URL actually works but I don't want to expose my password and I'd like to work with potentially long strings, which of course I can't do with regular GET
     
  4. joaomgcd

    joaomgcd Administrator Staff Member

    Joined:
    Feb 3, 2015
    Messages:
    9,476
    Likes Received:
    797
    Sorry about that.

    Ok, here's how to actually do it: https://github.com/AutoApps/AutoRem...in/AutoRemote/Communications/Communication.cs

    Look under the Send method after the part that says " //if it fails"

    You need to send a POST to https://autoremotejoaomgcd.appspot.com/sendrequest
    with the POST data "request=JSON_MESSAGE&key=RECEIVER_KEY&sender=SENDER_KEY"

    Look through the code there, and let me know if you need more info :) Let me know if you have trouble figuring out how to get the JSON serialization of the message
     
  5. MaddoScientisto

    MaddoScientisto New Member

    Joined:
    Jun 10, 2015
    Messages:
    7
    Likes Received:
    0
    thanks for the tips, I tried to adapt my code but something didn't really work and autotasker crashed.
    I sent a crash report through the interface so you should be able to see it, here I'll post the revised code:

    Code (Text):

    AutoRemote.SendIntent = function(message, key, target, sender, password){
     var testCode = "ExecuteTask PopupTest=:=Spaghetti";
     var url = "https://autoremotejoaomgcd.appspot.com/sendrequest";
     
     var dataObj = {
      key:key,
      sender: sender,
      request: JSON.stringify(testCode), // Here I converted the string to JSON but maybe I should have done something else instead?
      target: target,
      password: password, // your example had no password but I tried to include it anyway
     }

    console.log("DataObj: " + JSON.stringify(dataObj));
     $.post(url, dataObj, function(result){
    console.log(result);
      });

    }
     
     
  6. joaomgcd

    joaomgcd Administrator Staff Member

    Joined:
    Feb 3, 2015
    Messages:
    9,476
    Likes Received:
    797
    The message object needs to be something like this:

    Code (Text):
    {
        "message": "ExecuteTask PopupTest=:=Spaghetti",
        "password": null,
        "ttl": 0,
        "collapsekey": null,
        "key": "RECEIVER_KEY",
        "sender": "SENDER_KEY",
        "communication_base_params": {
            "sender": "SENDER_KEY",
            "type": "Message"
        }
    }
    You can stringify an object like this and put it in the request field.

    Hope this helps!
     
    Last edited: Jun 12, 2015
  7. MaddoScientisto

    MaddoScientisto New Member

    Joined:
    Jun 10, 2015
    Messages:
    7
    Likes Received:
    0
    It works!

    I'm posting the revised code so that other people will have a working example:
    Code (Text):

    AutoRemote.SendIntent = function(message, key, target, sender, password){
     var testCode = "ExecuteTask PopupTest=:=Spaghetti"; // ToDo: put this in the function parameters, maybe make a function to build this
     var url = "https://autoremotejoaomgcd.appspot.com/sendrequest";

     var messageObj = {
      message: testCode,
      password: password,
      ttl: 0,
      collapsekey: null,
      key: key,
      sender: sender,
      communication_base_params: {
    sender: sender,
    type: "Message"
    }
     };
     var dataObj = {
      key:key,
      sender: sender,
      request: JSON.stringify(messageObj)
     };


     $.post(url, dataObj, function(result){
     
    // do stuff with the result (still untested)
      });
     
    }

     
    This opens up so many possibilities
     
  8. joaomgcd

    joaomgcd Administrator Staff Member

    Joined:
    Feb 3, 2015
    Messages:
    9,476
    Likes Received:
    797
    Awesome! :) Let me know what you end up doing with it, sounds interesting!
     
  9. MaddoScientisto

    MaddoScientisto New Member

    Joined:
    Jun 10, 2015
    Messages:
    7
    Likes Received:
    0
    I'm making a remote control panel that can either be used locally on the device embedded in a tasker webview or remotely from a browser, in which case it will use autotasker to execute commands, tasks, etc.

    This is the WIP project: http://plnkr.co/edit/lLmYK7

    Now I got another question: can I have this POST request return whatever I want from the device back to the webpage?
     
  10. joaomgcd

    joaomgcd Administrator Staff Member

    Joined:
    Feb 3, 2015
    Messages:
    9,476
    Likes Received:
    797
    You mean like have the phone respond right away and have that be the result of the post? Sorry, nope, that's not possible right now :)
     
  11. MaddoScientisto

    MaddoScientisto New Member

    Joined:
    Jun 10, 2015
    Messages:
    7
    Likes Received:
    0
    I hope it's going to be a future feature.
    So right now the only way to send data back would be to have a tasker script send an http post with the data to whatever server I control and then update the web interface through callbacks?
    I can get to do that.

    Meanwhile, I just managed to set up the infrastructure to send arbitrary javascript code to the phone through autotasker and have it be executed, that's pretty great because now I can pretty much do anything through tasker's js functions.

    Another question: do third party plugins (autotasker, etc) expose their own javascript functions or are they all exclusively task-only?
     
  12. joaomgcd

    joaomgcd Administrator Staff Member

    Joined:
    Feb 3, 2015
    Messages:
    9,476
    Likes Received:
    797
    plugins don't run on javascript, but on Java :) So I don't think there's a way to interact with them through javascript, but I'm not sure.

    But yeah, to get a response you should do that.

    You could also implement an AutoRemote 3rd party client like the C# example I linked above. Then your server will appear in the AutoRemote device list itself on your phone and will be easier to interact with :)
     
  13. MaddoScientisto

    MaddoScientisto New Member

    Joined:
    Jun 10, 2015
    Messages:
    7
    Likes Received:
    0
    I guess I could just make tasks for the plugins and run these, being able to run javascript code remotely also allows me to run any task and apparently it's also possible to dynamically add new tasks through a function.
    I really got to polish and then publish my web application, there's a lot of fun things that are opening up thanks to autoremote!
     

Share This Page