Sending and receiving SMS with mySMS using just bash & curl.
(Breaking the rules here - don't have a question - but I found this a challenge to make work, with few good examples out there, so here is an example. If you can improve on it - I'm certainly interested to learn something new!).
mysms.com provides a free Android only app (iPhone don't open up access to messaging), and their API allows you to send and receive SMS on that android device, receiving API calls over the web. So my use-case scenario is I want my service to be able to send out appointment reminders, and receive any replies. You need an API key - just email [email protected] and I got my API key pretty quickly.
To send SMS
- Install the app from Play store and assign a password in the app
- Email support and get an API key
- code as below...
Mobile numbers need to be in full msisdn format - so country code, omit initial zero, then mobile number, so i'm running this in New Zealand which is country code +64, and mobiles are all 021, 022 or 027...
apikey="insert your api key here"
from="6421xxxxx"
pwd="you set the password in the android app"
to="6421yyyyyyy"
smsurl="https://api.mysms.com/json/message/send?api_key=$apikey&msisdn=$from&password=$pwd&recipient=$to&message=Testmessage1."
curl -v "$smsurl"
So pretty simple - think i need to do some work to cope with URL encoding of the message, but as a proof of concept this should be easy to get up and running.
Receive SMS
You need to generate an auth key to be able to access the messages and mark as read or delete. (unsure how long this is value for.)
# GET auth token
curl -d '{ "apiKey": "'$apikey'", "msisdn": "'$from'", "password": "'$pwd'", "checkKey": "false" }' -H "Content-Type: application/json" -X POST "https://api.mysms.com/json/user/login" -o /tmp/sms.0
authtoken=$(gawk '{ i=index($0,"authToken"); x=substr($0,i+12); j=index(x,"\""); print substr(x,0,j-1); }' /tmp/sms.0)
echo "AUTH token = $authtoken"
# GET list of unread messages
curl -d '{ "apiKey": "'$apikey'", "authToken": "'$authtoken'" }' -H "Content-Type: application/json" -X POST "https://api.mysms.com/json/user/message/conversations/get" -o /tmp/sms.1
# Mark a message as read
# This one sets a message from 02123123 as having been read.
# Looks like there is a better way with MessageIDs
curl -d '{ "apiKey": "'$apikey'", "authToken": "'$authtoken'", "address": "+642123123" }' -H "Content-Type: application/json" -X POST "https://api.mysms.com/json/user/message/conversations/read" -o /tmp/sms.2
# Deleting a message once my end has retrieved it works better for me
# so deleting a message is...
# This will delete MessageId 1
curl -d '{ "apiKey": "'$apikey'", "authToken": "'$authtoken'", "MessageId": "1" }' -H "Content-Type: application/json" -X POST "https://api.mysms.com/json/user/message/conversations/del" -o /tmp/sms.3
A little update on this, i found this a better way to send, as it takes care of the urlencoding for pesky characters:
and for receiving, once you have found a conversation with a new unread message, you need this to get the full message content: