Sending an SMS through Twilio letting it chose the From number with a MessagingServiceSid doesn't work

北战南征 提交于 2019-12-11 05:37:12

问题


I'm trying to send an SMS using twilio, without specifying a specific From number, instead letting Copilot chose the best number for me (as documented in https://www.twilio.com/docs/api/rest/sending-messages#post-parameters-conditional).

However, when sending a request, the Twilio REST API complains that I did not specify a From number. Specifying a From number, as expected, works. But I'd like to avoid this to be more flexible in case I need more numbers down the road. Am I misunderstanding the documentation ?

I used HTTPie to test the API. The full request/response :

$ http --print=HBhb --form POST 'https://$ACCOUNT_SID:$ACCOUNT_TOKEN@api.twilio.com/2010-04-01/Accounts/$ACCOUNT_SID/SMS/Messages.json' MessagingServiceSid=$SERVICE_SID To=$PHONE Body=Hello

POST /2010-04-01/Accounts/$ACCOUNT_SID/SMS/Messages.json HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Authorization: Basic REDACTED
Connection: keep-alive
Content-Length: 83
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Host: api.twilio.com
User-Agent: HTTPie/0.9.2

MessagingServiceSid=$SERVICE_SID&To=$PHONE&Body=Hello

HTTP/1.1 400 BAD REQUEST
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Accept, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since
Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: ETag
Connection: keep-alive
Content-Length: 136
Content-Type: application/json
Date: Thu, 28 Jul 2016 14:54:38 GMT
Twilio-Request-Duration: 0.056
Twilio-Request-Id: RQ5b9acf9d222947668fa9ada38d5beaba
X-Powered-By: AT-5000
X-Shenanigans: none

{
    "code": 21603, 
    "message": "A 'From' phone number is required.", 
    "more_info": "https://www.twilio.com/docs/errors/21603", 
    "status": 400
}

回答1:


I can see that you have a number in the MessageService, however it looks like you are using the old deprecated endpoint that does not support sending with the MessageService.

You have: /2010-04-01/Accounts/$ACCOUNT_SID/SMS/Messages.json

You should be using: /2010-04-01/Accounts/$ACCOUNT_SID/Messages.json

Give that a try and let me know how it goes.



来源:https://stackoverflow.com/questions/38640341/sending-an-sms-through-twilio-letting-it-chose-the-from-number-with-a-messagings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!