0

I'm working on a cli for the Pushbullet HTTP API using Bash scripting. Sending pushes (notes and links), as well as creating, deleting, and modifying contacts & devices are all straight forward using curl and Bash. However, sending SMS and files are a bit more complex, as both require sending more complex JSON-formatted requests to the server (multiple JSON-formatted requests, in the case of pushing files).

I've tried sending many variations on the following (both with and without escape characters), but the server keeps replying about JSON-formatting errors. The following code is based off of the example given in the Pushbullet HTTP API documentation.

 curl -su $auth_id: -X POST https://api.pushbullet.com/v2/ephemerals --header "Content-Type: application/json" 
      --data-binary '{ "\"type"\": "\"push"\", "\"push"\": { \
      "\"type"\": "\"messaging_extension_reply"\", \
      "\"package_name"\": "\"com.pushbullet.android"\", \
      "\"source_user_iden"\": "\"$source_idens"\", \
      "\"target_device_iden"\": "\"$target_idens"\", \
      "\"conversation_iden"\": "\"$sms_device"\", \
      "\"message"\": "\"Hello"\" \
} }'

Using bash -x, I can see that this is (supposedly) what is being sent to the server:

--data-binary '{"type": "push", "push": { 
"type": "messaging_extension_reply", 
"package_name": "com.pushbullet.android", 
"source_user_iden": "<source_idens>", 
"target_device_iden": "<device_idens>", 
"conversation_iden": "<sms_phone_number>", 
"message": "Hello" } }'

In all cases, the server returns: {"error":{"type":"invalid_request","message":"Failed to parse JSON body.","cat":"(=^‥^=)"}}

What is the appropriate formatting of a JSON request using curl to send an SMS via the Pushbullet API? Am I overlooking something obvious? I'm trying to accomplish this using only curl and Bash, I see no reason why it's not possible (maybe not the fastest or most elegant way, but certainly possible).

Sophie
  • 304
  • 10
  • 12

2 Answers2

1

I found the solution to my issue so I thought I'd share it. It was actually very simple:

Because the curl command includes a JSON-formatted response with single quotes, variable expansion was not occurring. This is a limitation (or perhaps a feature) of Bash. So, even though the server responded with { } indicating no errors in the request, the requests were actually being sent without the proper values for parameters, such asuser_iden,source_user_iden, etc.

Solution: Enclose all variable expansions inside the JSON-formatted request in a double-quote and single-quote, like so:

"'"$user_idens"'"

Sophie
  • 304
  • 10
  • 12
0

First I'd like to apologize for how bad the API is, especially file upload and sending SMS. I was thinking of adding multipart or base64 file uploads to /v2/pushes. I think the first one might help you with curl, not sure about the base64 one. multipart is a huge pain though, so I'd prefer to make it better than the current setup if possible, rather than about equally as bad. Suggestions are welcome.

I tried your command line and it seemed to work, so I'm not sure what is going wrong. Here's the command line I did. Perhaps your quote escaping or newlines are causing the JSON error?

curl -u <access_token> -X POST https://api.pushbullet.com/v2/ephemerals --header "Content-Type: application/json" --data-binary '{"type": "push", "push": {"type": "messaging_extension_reply","package_name": "com.pushbullet.android","source_user_iden": "iden","target_device_iden": "device_idens", "conversation_iden": "sms_phone_number","message": "Hello" } }'
Chris Pushbullet
  • 1,039
  • 9
  • 10
  • I, too, was able to get a { } response with that code. Previously, I had everything after the header stored in it's own variable, and I think that was the main problem. Probably has to do with the way Bash handles certain characters inside a variable (hence why I was escaping all the double quotes). Putting it on a single line seemed to fix that. I did not receive the SMS though. I'll have to make sure the proper `source_user_iden` and `target_device_iden` are being specified by the script. This gives me some idea of why pushing files wasn't working for me, either. – Sophie Jul 26 '15 at 12:53
  • So, it seems that although I have the correct `source_user_idens` and `target_device_idens` specified, and I am getting the correct `{ }` response, my phone doesn't actually receive the SMS. Am I missing something else? – Sophie Jul 27 '15 at 01:56
  • Could you give me the exact request you are using? Also, to be clear, the Pushbullet API is sending the SMS via the Pushbullet App on your android phone. (I don't know if the docs make that clear) – Chris Pushbullet Jul 27 '15 at 16:45
  • I should still be able to send an SMS to myself though, correct? `curl -su $auth_id: -X POST https://api.pushbullet.com/v2/ephemerals --header "Content-Type: application/json" --data-binary '{"type": "push", "push": {"type": "messaging_extension_reply","package_name": "com.pushbullet.android","source_user_iden": "$user_iden","target_device_iden": "$dev_id", "conversation_iden": "$phone_number","message": "$sms" } }'` $user_iden and $dev_id are obtained through seperate calls to the /v2/users/me and /v2/device objects, respectively. $phone_number and $sms are user-defined at the command line. – Sophie Jul 30 '15 at 00:42
  • Yeah it should work to yourself. If you're still not having any luck, you should try sending an SMS message from the Pushbullet app while watching the stream: https://docs.pushbullet.com/#stream That way you can compare your message to the valid one that appears on the stream. – Chris Pushbullet Jul 31 '15 at 18:48
  • I have been working on an Excel VBA script to send messages through Pushbullet and I had the same problem: JSON seems to be OK, but the messages didn't come through. Pushbullet was ON on my phone, but OFF on my computer and once opening Pushbullet on my computer the issue resolved and I was able to send messages. – Marius Katinas Apr 24 '17 at 20:43