使用Sending和mySMS接收短信,只需使用bash & curl.
(打破这里的规则-没有问题-但我发现这是一个挑战,使工作,很少有好的例子,所以这里有一个例子。如果你能改进的话--我当然有兴趣学点新的东西!)
mysms.com提供了一个免费的Android应用程序(iPhone不打开消息传递的访问权限),他们的API允许您在该android设备上发送和接收短信,通过网络接收API调用。因此,我的用例场景是,我希望我的服务能够发送约会提醒,并收到任何回复。你需要一个API密钥-只是电子邮件dev@mysms.com和我得到我的API密钥很快。
To发送短信
移动号码必须是完整的msisdn格式-所以国家代码,省略初始零,然后移动号码,所以我运行在新西兰,是国家代码+64,手机都是021,022或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"所以非常简单--认为我需要做一些工作来处理消息的URL编码,但作为概念的证明,这应该很容易启动和运行。
Receive短信
您需要生成一个auth键,以便能够访问这些消息并将其标记为“读”或“删除”。(不确定这是价值多长时间。)
# 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发布于 2021-06-06 17:58:49
关于这一点,我发现这是一种更好的发送方式,因为它处理了讨厌字符的urlencoding:
curl -G 'https://api.mysms.com/json/message/send' \
--data-urlencode "api_key=$apikey" \
--data-urlencode "password=$pwd" \
--data-urlencode "msisdn=$mno" \
--data-urlencode "recipient=$to" \
--data-urlencode "message=$msg"对于接收,一旦您找到了一条新的未读消息的对话,您需要这样做才能获得完整的消息内容:
curl -d '{ "apiKey": "'$apikey'", "authToken": "'$authtoken'", "address": "'$m'" }' -H "Content-Type: application/json" -X POST "https://api.mysms.com/json/user/message/get/by/conversation" https://serverfault.com/questions/1014322
复制相似问题