I am trying to build a local database storing Twilio SMS message events. There would be two tables: A Messages table with message info (To,From,Body,etc.), and a StatusRecords table containing status events (Status,ErrorCode,ErrorMessage, etc.). Both tables would be keyed on the MessageID.
I can get the MessageID and message info when I send a message (as a client) using the REST API in the returned response. That's fine.
I have a java servlet running that is called with status updates when Twilio updates the message delivery status. I then update the status in my database using the MessageID as the key. That's fine as well.
I have another servlet running that receives reply messages. I pick up the reply MessageID and add this to the database. This servlet also replies to the incomming message in the http GET response parameter using the TwilML API.
The problem is: How do I get the MessageID of the reply to the incomming message? The reply takes place as a result of a response to the http GET. I don't think I will hear from Twilio again after responding.
I assume that Twilio will update the status on the reply message as it is delivered or not delivered--and I will receive this update in my status update servlet. I could probably try to match the phone number of the status update to get the reply MessageID but this seems sort of crude.
Is there a more elegant way to do this?
Twilio evangelist here.
The <Message> verb takes an action parameter which lets you specify a URL for Twilio to request once it processes the Message noun. This request will include the Message SID.
If you wanted to associate the SID of the incoming message with the one sent by Twilio, you could pass the incoming message SID as part of the URL you set for the action parameter:
http://example.com/message?id=XXXXXXXXXX
Hope that helps.
Related
I'm using the Twilio API to create a Service that will send SMS. The Service should have its own unique webhook endpoint to receive replies (incoming SMS).
var service = ServiceResource.Create(
friendlyName: Campaign.SMSCampaignDisplayName,
usecase: "marketing",
stickySender: true,
useInboundWebhookOnNumber: false,
inboundRequestUrl: new Uri($"<my site>/Admin/twilio/receive/{Campaign.SMSCampaignGuid}")
);
var message = MessageResource.Create(
body: Campaign.SMSCampaignBody,
from: new PhoneNumber(fromNumber),
to: new PhoneNumber(Campaign.SMSCampaignTo),
messagingServiceSid: service.Sid
);
The Service is created and the SMS is successfully sent. In the Twilio console I can see the service has the correct webhook URL:
But when I respond to the SMS, the webhook is not sent. In fact, the incoming message no longer appears in the Twilio console's Monitor > Messaging logs. Note that webhooks and incoming SMS logging was working fine when I was not using a Service and the incoming SMS webhook was defined on the sender phone number.
What am I missing, or is this some issue with Services? Do I need to add a Sender to the Service even though it's sending fine? How do I get the phoneNumberSid?
Edit
I'm dumb, I could have just tested adding the phone number to the Service manually. That indeed did work, but I need to do it programmatically. I'm not having much luck finding the API/endpoint to get the correct phoneNumberSid for this call, does it not exist?
You can use the IncomingPhoneNumber resource, as documented below, to get the Phone Number SID.
List all IncomingPhoneNumber resources for your account
I am using Twilio and I want to get the DateSent parameter of a message. When the message is initially sent it has null values for both DateCreated and DateSent. So I pass a StatusCallback URL to receive any status updates on that message. However, I am able to get only the new statuses the message gets such as sent, or delivered and not the new date as an event. In the docs I couldn't find anything about this either (https://www.twilio.com/docs/sms/send-messages#monitor-the-status-of-your-message). What is the way to get this information without continuously polling the API?
The date created is included as part of the response from the API. If you find that is not the case can you please post example code.
When the sms is actually sent you can pull the message resource using the api or sdk via the sid https://www.twilio.com/docs/sms/api/message-resource#fetch-a-message-resource
I would like to know if it's possible to use the user_name with Autopilot from the incoming post requests from Slack.
I've tried checking the documents but I'm not able to find any reference to handling incoming requests for Autopilot. Perhaps it isn't supported but does anyone know if it's possible?
Twilio developer evangelist here.
The documentation for handling incoming requests is here: https://www.twilio.com/docs/autopilot/actions/autopilot-request.
You should receive a UserIdentifier with the request which identifies the user in the platform the request came from. In the case of incoming phone calls or sms messages the identifier is the user's phone number. I haven't tested, but I assume that this would either be a user name from Slack, or an identifier you can use with the Slack API to find the user name.
Let me know if that helps at all.
I ended up being able to use the response data from Autopilot and found I was able to get the useridentifier from the json response as such:
user_name = request.form['UserIdentifier']
This has then allowed me to send direct messages via the incoming webhooks application in slack as using the above user name as the value for the channel key as follows.
payload={'text':"Ok, I'm working on it. I'll let you know when your request is ready.", "channel":user_name}
webhook_url= 'https://hooks.slack.com/services/<slack id>/<slack id>'
response = requests.post(
webhook_url, data=json.dumps(payload),
headers={'Content-Type': 'application/json'})
if response.status_code != 200:
raise ValueError(
'Request to slack returned an error %s, the response is:\n%s'
% (response.status_code, response.text)
)
When I set my SMS Url for a given phone number and then send a text to that phone number, the request somehow fails. Is there a way for me to inspect the error response (404/500/403 etc) to see any exception details from my twilio dashboard?
Hi Twilio Customer Support here,
Have you viewed the app monitor?
https://www.twilio.com/user/account/developer-tools/app-monitor
It contains all of the errors that your account has recorded, you can drill down on each error to see the request body etc.
Initially I have been using the TwiML response to send response to my text messages. But since my requirement has changed and I need to send more than 10 messages in response to my text hence I am now using the Twilio API to send the message out. So to fix this now I am trying to send nothing as the TwiML response for my Text Messages and instead using the Twilio API to send the actual messages out. I also have a TwiML bin URL attached to my short code as a Message Fall Back URL in case Twilio is not able to connect to my REST API URL. So now when a Text message is received by Twilio it connects to my REST API and the response is sent. But I am getting the response from the Message FallBack URL first and then the actual response for my text message via the Twilio API. Is there anyway to avoid this? I can remove the Message Fall back URL but then what would happen in the actual case when Twilio is not able to connect to my server?
This is how I have added the code:
var twilioResponse = new Twilio.TwiML.TwilioResponse();
...... have code that uses Twilio API to send out more than 10 messages.
...... Since the messages are going out via Twilio API hence returning NULL to the TwiML response
return Request.CreateResponse(HttpStatusCode.OK, twilioResponse.Element);
More Updates:
As I mentioned earlier I'm using the REST API to send an SMS. When a Text request comes Twilio server calls my URL supplied to the REST call.
Sometimes my server is little slow in responding - in which case twilio gets the fallback URL message and passes on the text message.
How do I increase the timeout ? This timeout is the time the server responding with TWIML.
Please help - any pointers are appreciated.
Twilio evangelist here.
My first suggestion would be to check your accounts App Monitor to see if Twilio is logging any errors. The Fallback URL should not be getting requested unless Twilio fails to get a valid response from the primary URL.
The default timeout for a Twilio request is 15 seconds, so if your server is taking longer than that to respond there may be a bigger issue happening with your server or network.
Hope that helps.