This question relates to Twilio and the API for it (C#).
I do not have a website nor a web application, just a need to send SMS notifications to my own private mobile when something happens on a server (NOT a webserver). I have already, successfully, sent SMS to my mobile from a C# dll. I want to check the status of the message. All the references imply website/web application use cases, and getting the status using a URL.
How do I do this in C#? Is there an example of the suggestion made here? No PHP or other web servery technology.
The solution I found, was ridiculously simple. The suggestion (made here) was to hang on to the message id. I discovered the GetSMSMessage method on TwilioRestClient. So querying message status some time after sending will give you the updated status.
By the way, the one thing you'll want to watch out for is that if you query the API too soon, you might get a status of "queued" or "sending" rather than "sent" or "failed", so be sure that your logic is set up to handle these other possible responses.
Also good to keep in mind that if you try to send more than 1 message per-second via a Twilio phone number, Twilio will queue the excess messages and release them at a rate of 1 message per-second per-number. This comes into play because if you have a burst where you send 10 messages in 1 second, but then you go to retrieve the status of the messages after 5 seconds half of the messages will be "sent" and half will be "queued".
If you dont have a callback url. Just call the method
TwilioClient.Init(accountSid, authToken);
var message = MessageResource.Create(
body: "Join Earth's mightiest heroes. Like Kevin Bacon.",
from: new Twilio.Types.PhoneNumber(fromNumber),
to: new Twilio.Types.PhoneNumber(toNumber)
);
Console.WriteLine(message.Sid);
//Get the status from twilio
TwilioClient.Init(accountSid, authToken);
var verificationCheck = MessageResource.Fetch(message.Sid);
Console.WriteLine(verificationCheck.Status);
where Sid is the id you get back from Twilio.
Related
I am working on an appointment confirmation system where I need to bind the SMS response 'Y' or 'N' to some meta data and send that meta data to my API via the Twilio webhook.
I am using.NET SDK from Twilio and trying to add attributes to the SMS message (MessageResource) which their documentation states:
Attributes:
STRING PII MTL: 30 DAYS
"The JSON string that stores application-specific data. If attributes have not been set, {} is returned."
link to docs: https://www.twilio.com/docs/chat/rest/message-resource#message-properties
which is more like metadata which is what I need but when I try finding any 'attributes' property in my C# code with their SDK I can't find it.
Here's my code:
var twilioMessage = MessageResource.Create(
body: msg,
from: _configuration["Twilio:FromPhoneNo"],
to: new PhoneNumber(phoneNumber),
attempt:1,
attributes: "{"id":"test"}" //this property doesn't work
);
var attributes = twilioMessage.attributes; //this doesn't work either
Any help will be highly appreciated.
Twilio developer evangelist here.
I'm afraid you are looking at the wrong documentation for sending SMS messages there. This documentation is for sending a chat (app to app) message. The documentation for sending an SMS is here.
Sadly, SMS messages do not have the ability to bind custom attributes to them. It is simply something that doesn't exist in the protocol.
You are sending an appointment confirmation and expecting a "Y" or "N" response. I am guessing you are trying to add the ID of the appointment in the case that you send confirmations for more than one appointment to the same phone number and you need to work out which appointment the response is about. There's a further issue here in that, as an end user, you cannot reply to a specific SMS messages. SMS is purely chronological.
There are 3 ways I can think of to work around this:
Only send one confirmation at a time. If you are still waiting for a response to a confirmation, don't send another one until you get that response. This might not work if users don't respond to confirmations.
Have the user include the ID of the appointment in the response. This is flaky because it relies on the users getting things right and you want to make things as simple as you can for them
When you need to send more than one confirmation at a time, use different Twilio numbers to send the confirmations. Then you can tie the confirmation response to the appointment by inspecting the Twilio number that the user sent the message to.
Number 3 is the most sound way of doing this. It shouldn't require you to have too many extra numbers, just as many as you might have concurrent appointment confirmations.
Let me know if this helps at all.
I recently upgraded the Twilio C# DLL we've been using to handle SMS messages. We are now on Twilio.DLL v5.16.2. I have a webhook set up to call a custom URL on our website. That part is working, because I can log the Request coming in (message SID and body and all that good stuff). At the end, we have code to instantiate a MessagingResponse object to send back a confirmation that the message was received. That's not working anymore. I don't get a SMS message on my phone nor do I see an Outgoing API record on the SMS logs page when I'm logged into my project on Twilio.com (whereas I do see the test message I've sent to our number as an Incoming message).
There is no error or exception either.
Any idea how to debug/solve this?
string responseTxt = "TESTING: Got it. Thanks.";
var smsResponse = new MessagingResponse();
var smsMsg = smsResponse.Message(responseTxt);
response.Write(smsMsg);
Twilio developer evangelist here.
I'm not a C# developer, so forgive me if I'm wrong. Also, I don't know what happened with this change.
Anyway, from what I can see, the message body when using the MessagingResponse's Message method appears to be a named argument now. Changing your code to the following should work for the latest version of the DLL.
var smsMsg = smsResponse.Message(body: responseTxt);
Let me know if that helps.
I'm not sure there's much to update as the exact same scenario still stands. But I can reiterate to see if this provides any additional clues.
We have a Twilio SMS service set up. There is a webhook assigned to our number that maps to an HTTP Handler on an ASP.NET website. The code in the handler logs in our database the fact that an SMS message was received and various bits of info (from the Request.Form variables supplied by Twilio, From number, body of the message, num of media, etc.).
Then, the code "replies" to the original SMS sender using the MessagingResponse object. See original code snippet. Really couldn't be much simpler.
Note, this is and has all been working quite well, using the Twilio c# helper library 5.6.
I upgraded the Twilio library to the latest rev (5.25.1) and its dependencies. Copied the new DLLs to our dev webserver. Now the reply message, using the MessagingResponse object, never occurs. The webhook is still called as I can see the new record in the database logging the message details. Just no response. No error, no exception. Just nothing.
If I rollback to our old version of the Twilio library (and its dependencies, thank goodness for Git!), it works again.
I am trying make phonecall via Twilio using c# i am using the below code to make call.code executes successfully but i wont receive call.In my twilio page it shows staus as completed and durations.
const string accountSid = "ACe200aeb6115e1f3038a012e1103bd874";
const string authToken = "6XXXXXX";
TwilioClient.Init(accountSid, authToken);
var to = new PhoneNumber("+12025688652");
var from = new PhoneNumber("+919500553163");
var call = CallResource.Create(to,
from,
url: new Uri("https://demo.twilio.com/welcome/voice/"));
Console.WriteLine(call.Sid);
Twilio developer evangelist here.
I believe that you are actually making that call successfully. You are not experiencing the call yourself because of the parameters you have used.
You are making the call from your verified number. This is your real phone number, but when you set it to be the from address that just means Twilio will use it as the caller ID for the call.
For the to address you are using your Twilio number that you have bought within your account. When you make calls to that number, Twilio makes a webhook (HTTP) request to the URL that is set as the voice response in the number's settings. Currently, that is still set to the default, so Twilio retrieves this URL: https://demo.twilio.com/welcome/voice/. If you click through, you will see that this just reads out a message.
The URL you use when you create the call is also the default URL.
So what is happening with the call is that Twilio is using your caller ID to dial your Twilio number and when the call is answered, both sides of the call are reading out that message to each other.
If you want to at least make your own phone ring, then I'd start by reversing the to and from numbers in your API call. If you do that, then you should receive a call on your phone and when you answer it you will hear the demo message. Then, once you've done that you can start writing your own TwiML responses, possibly using Twimlbins to get started or by building your own application.
Let me know if this helps.
When i send a text to a customer using a messaging service id. The from number is null in the returned MessageResource. Code below.
Is the only way to determine the From by making an additional call out using the message sid? I need the from used for analytics and so I can use the same number to contact an account in case the customer uses multiple contact numbers.
return await MessageResource.CreateAsync
(
to: new PhoneNumber(toNumber),
messagingServiceSid: messengerSid,
body: message
);
Twilio developer evangelist here.
When you use a messaging service to send messages from various numbers then you cannot know at the time of message creation what the number used will be. When you make the request to send the message, Twilio queues that message up to be delivered by the messaging service. If you inspect the status of the message object you receive from the API call you show in your question you will see it is "accepted". At this point the messaging service won't have decided which number to use.
So, you can either make a second request to the API using the message SID that is returned to find out the From number. Or, you could set a StatusCallback URL which would receive a request once the message was sent, including the From number.
Let me know if that helps at all.
Just started working on email sending implementation using SendGridApiClient. Have this line that sends an email
dynamic response = await _sendGrid.client.mail.send.post(requestBody: mail.Get());
Response can provide StatusCode Accepted and nothing more. Was wondering how can I check was email delivered or stuck ?
The SendGrid API is asynchronous because the length of time it takes to process delivery of the email is non-trivial and dependent on factors like the receiving server.
The best way to keep an eye on events like delivered, bounced, etc in real-time is to implement the Event Webhook.
Take a look at this answer: Can my ASP.Net Code get confirmation from sendgrid that an email has been sent?