retrieve the smsstatus after sending sms Twilio api - twilio

i m using Twilio sms api to send message form my Asp.Net application
var delivery = twilioRestClient.SendSmsMessage("+123456", mobilenumber,
string.Format("Your New Password is: {0}", genpass));
the variable delivery contains object of twiliosendsms which also contain sms status currently the status is 'queued' when the message was sent i want to know how to check if the status is changed to Sending,sent,failed,received

Twilio evangelist here.
You can use the StatusCallback to have Twilio tell you when your message was sent.
Hope that helps.

Related

How to receive webhook response to incoming messages on Twilio SMS Service

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

Twilio API get DateSent from StatusCallback URL?

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

Pass variables from slack webhook to Autopilot

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)
)

Getting MessageID of outgoing message on TwiML reply to incomming message

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.

SMS parameters through twilio

I'm new to twilio and have the following situation. I want to send an SMS to someone for an appointment confirmation. They'll text back CONFIRM or CANCEL etc to the message.
Is there any way to pass an internal database id in the SMS to them and get it back in their response so i know what they're confirming or canceling? I'd rather it not be shown to the end user receiving the text.
Thanks!
Twilio evangelist here.
Unfortunately there isn't any way to transparently include metadata in an SMS and have that metadata returned to you when they reply, but there ways to address your scenario.
If you are sending the user a text message you already have their phone number and you can use that as a unique ID. When the user replies to your message, Twilio will as part of our HTTP request to your application, pass you the users phone number. You can use that number to locate them in your database and mark them as confirming or canceling.
Hope that helps.

Resources