Twilio has a page dedicated for automation testing: https://www.twilio.com/docs/sms/tutorials/automate-testing
However in this particular use case I need to test that a message is actually sent/received based off another request being sent.
For example our application sents out a notification text 2 hours after /api/some-endpoint
gets a POST request for example. Normally we set this up to send to an actual phone to verify......but is there any way to hit twilio's API maybe we some ID that verifies that particular message was at least sent?
Or is this not really possible with twilio's API?
Thanks
Twilio offers two features for your use case - webhook or Twilio REST API. The webhook will give you a real-time notification for the delivery.
Option 1 - Webhook
Twilio will call the webhook when the message is delivered. This is explained in Track Delivery Status of Messages:
string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");
TwilioClient.Init(accountSid, authToken);
var message = MessageResource.Create(
body: "McAvoy or Stewart? These timelines can get so confusing.",
from: new Twilio.Types.PhoneNumber("+15017122661"),
statusCallback: new Uri("https://example.com/your_status_callback_endpoint"),
to: new Twilio.Types.PhoneNumber("+15558675310")
);
Console.WriteLine(message. Sid);
The payload sent to "https://example.com/your_status_callback_endpoint" should have the same properties as what is shown below for the Twilio REST API response. You will need to create that endpoint and it will need to be publicly reachable on the Internet (or use Ngrok or Visual Studio Dev Tunnels to create a tunnel from the Internet to your local machine).
Option 2 - Twilio REST API
Use the Twilio REST API to fetch a MessageResource:
string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");
TwilioClient.Init(accountSid, authToken);
var message = MessageResource.Fetch(pathSid: "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
Console.WriteLine(message.To);
Fetching the MessageResource will return a json object that includes these keys:
{
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"api_version": "2010-04-01",
"body": "testing",
"date_created": "Fri, 24 May 2019 17:18:27 +0000",
"date_sent": "Fri, 24 May 2019 17:18:28 +0000",
"date_updated": "Fri, 24 May 2019 17:18:28 +0000",
"direction": "outbound-api",
"error_code": 30007,
"error_message": "Carrier violation",
"from": "+12019235161",
"messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"num_media": "0",
"num_segments": "1",
"price": "-0.00750",
"price_unit": "USD",
"sid": "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"status": "sent",
"subresource_uris": {
"media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMb7c0a2ce80504485a6f653a7110836f5/Media.json",
"feedback": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMb7c0a2ce80504485a6f653a7110836f5/Feedback.json"
},
"tags": {
"campaign_name": "Spring Sale 2022",
"message_type": "cart_abandoned"
},
"to": "+18182008801",
"uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMb7c0a2ce80504485a6f653a7110836f5.json"
}
The date_sent, error_code, error_message, and status give the data points you asked about.
Of the two choices, I suggest Option 1. Webhooks unlock the power of so much of the Twilio platform. You could use webhooks to give you a real-time deliverability dashboard.
PS - If your texts are not being delivered make sure you have completed the US A2P 10DLC onboarding:
Here at Twilio, our goal is to help businesses build world-class customer experiences, which requires preserving communication channels like SMS well into the future by protecting consumers from unwanted messages. That’s why we support this requirement by the carriers and will move to a fully verified 10DLC ecosystem. After August 31, 2023, Twilio will completely block all unregistered U.S.-bound 10DLC messages.
Hope this helps!