1

I am trying to send SMS from my mobile by using PushBullet API from Google Apps Calc script.

The script is (auth data redacted)

function send_SMS() {
  Logger.log("send_SMS start");
  var options = {    
    "method" : "post",
    "Content-Type" : "application/json",
    "headers" : { "Authorization": "Basic aaaaaaaaaaaaaaaaaa" },
    "payload" : {    
      "push" : {
        "type" : "messaging_extension_reply",
        "package_name" : "com.pushbullet.android",
        "source_user_iden" : "iiiiiiiiiiiii",
        "target_device_iden" : "iiiiiiiiiiiiiiidddddddddd",
        "conversation_iden" : "0999999999",
        "message" : "TestSMS"   },
      "type" : "push"
     }
  };  
  var push_bullet_url = "https://api.pushbullet.com/v2/ephemerals";
  Logger.log(options);  
  UrlFetchApp.fetch(push_bullet_url, options);
  return;
}

I seem to be making some JSON packing error, as I get 400. "Failed to parse JSON body.”

The documentation is on https://docs.pushbullet.com/#send-sms

I tried CURL on Windows and command works without problems (one I did “”” escaping)

curl --header "Access-Token: ttttttttttttttt" --header "Content-Type: application/json" --data-binary "{ """push""": { """type""": """messaging_extension_reply""", """package_name""": """com.pushbullet.android""", """source_user_iden""": """iiiiiiiiiiiiiiii""",  """target_device_iden""": """iiiiiiiiiiiiiiiiiddddddddddddd""", """conversation_iden""": """0999999999""", """message""": """TestSMS"""  }, """type""": """push"""}"  --request POST https://api.pushbullet.com/v2/ephemerals

I successfully wrote Google Apps scripts to get user_ident, device_ident and to send test “note” message. I think that the problem might be due to proper formatting of string quotes in JSON payload in the script.

1 Answers1

1

At the end the answer was simple!

The payload part of the options JSON has to be a string. You can do that in (at least) two ways:

You can stringify:

  var options = {
    "method" : "post",
    "headers" : { "Authorization": "Basic "+Utilities.base64Encode(PushToken+":"); },
    "payload" : JSON.stringify({
      "push" : {
        "type" : "messaging_extension_reply",
        "package_name" : "com.pushbullet.android",
        "source_user_iden" : "iiiiiii",
        "target_device_iden" : "iiiiiiiidddddd",
        "conversation_iden" : "0999999999",
        "message" : "Test SMS" },
      "type" : "push" } )

Or you can write payload as a string, but it has to be in one line

"payload" : '{ "push" : { "type" : "messaging_extension_reply", "package_name" : "com.pushbullet.android", "source_user_iden" : "iiiiii", "target_device_iden" : "iiiiiiiidddddd", "conversation_iden" : "099999999", "message" : "Test SMS" }, "type" : "push" }'

Most importantly, you can debug by using service such as http://httpresponder.com/ to see the request you sending and if your JSON is properly formed.