We are using Twilio Programmable Messaging for sending SMS, in our Kotlin backend service with Twilio SDK.
Gradle dependency
implementation("com.twilio.sdk:twilio:7.51.0")
Also, we are tracking the delivery status of the sent SMS messages. For that, we need to set a status callback before sending an SMS.
val creator = Message.creator(to, from, textMessage)
.setStatusCallback(webhook.smsStatus)
Receiving message status
Our other service is set up for receiving a status callback.
#PostMapping(path = ["events"], consumes = [MediaType.APPLICATION_FORM_URLENCODED_VALUE])
#ResponseStatus(NO_CONTENT)
fun receiveSmsStatus(status: SmsStatus) {
communicationsService.processSmsStatus(status.MessageSid, status.MessageStatus)
}
Everything is working fine and we successfully receive and process SMS delivery statuses.
But every 100th status callback contains payload with nullable MessageSid and MessageStatus. At the same time, it contains payload with From and To phone numbers.
Why might this happen? And is it okay for MessageSid and MessageStatus to be NULL? I thought these fields are required.
I suggest opening a ticket with Twilio support via the Twilio Console (upper right corner under ? - Submit a ticket). If you can associate the outbound SMS SID returned when calling the /Messages resource with the associated statusCallback (say by matching the number the SMS was sent to since the MessageSID is null), that may help Twilio support identify the issue.
Screenshots in the ticket will also be useful for them.
Related
Using twilio and java to send sms messages. When a customer completes an action, I send them an sms with a custom status callback URL programmatically and I add a query parameter called id. i.e. api.example.com/twilio/webhook?id=123
I don't have any issues listening for the webhook when the customer replies to the sms.
After the customer replies, I want to use id to perform a lookup of an object in my database. The problem is the id parameter is nowhere to be found on the webhook response.
Is it possible to retrieve that same id from the query parameter via Messages API or other means?
Or will I need to store the messageSid from the webhook somewhere in my database and associate it with the corresponding id?
Bummed there isn't an option to add custom metadata to a message.
Many thanks in advance.
I am creating a program that asks the user something like "How are you doing today":
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Message message = Message.creator(new PhoneNumber("+000000"), // To
// number
new PhoneNumber("0000000"), // From number
"How are you doing today?" // SMS body
).create();
System.out.println(message.getSid());
And then then the program will listen for whatever response the user texts back from their phone.
Now, Twilio says this about receiving SMS:
You can associate that phone number with an SMS URL. When someone sends a text message to that phone number, Twilio makes an HTTP request to your URL with the body of the message and the sender's phone number. You can then respond to the SMS by returning a reply message in the HTTP response to Twilio.
Now, I understand that when the user texts back, Twilio makes an HTTP request to my program, like so:
However, in the tutorial, they create an HTTP tunnel using ngrok to allow the HTTP request to go through. My application is supposed to be able to run in anyone's computer without prior configuration. How would you recommend I achieve this?
I'm afraid that without exposing your application to the Net you won't be able to use that particular API.
What you can try to do instead is polling / fetching:
When you send an SMS or MMS message via the REST API, using the
<Message> verb in TwiML, or someone sends a message to one of your
Twilio numbers Twilio creates a Message instance resource. The
Messages list resource represents the set of messages sent from and
received by an account.
Retrieving sent and received messages from history can be achieved by
querying the Messages list resource.
A short example to start with is available here.
I am trying to send a GCM message to my device, and for some reason, on the server, the getErrorCodeName() is returning InvalidRegisration.
I basically implemented the example from google, and registered, and sent the registration ID to the log, and wholesale copied it from the log to the code on the server where I am trying to do the send.
Any idea on what could be wrong?
Result result = sender.send(message,"foo", 1);
System.out.println("Message sent: "+result.getErrorCodeName());
I have double checked to see that logcat is not truncating the value being printed out, and it isn't. I logged the length of the registration id and matched it with the length of the string/regid I'm using on the server when I am sending the notification.
Not sure what's going on.
The send() method has to follow this format:
send (Message message, String registrationId, int retries)
Looking at your snippet of code, you will need to revise the second parameter.
If you had just replaced the actual registration ID for privacy reasons, I would suggest checking that you are sending the registration ID completely. As mentioned here, it could have been truncated or altered in transit from your client device to your server.
Messages are not delivered to numbers but Twilio is charging me and giving me status sent only with my code.
private val client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
def sendSMS(smsTo: String) = {
val params = Map("To" -> smsTo, "From" -> twilioNumber, "Body" -> "status?")
val messageFactory = client.getAccount.getSmsFactory
messageFactory.create(params)
}
But when I am using API Explorer its giving status delivered.
Is it my code's fault or Twilio?
I want my SMS delivered to users.
Twilio Evangelist here here.
When you send the SMS message using the REST API (the Java/Scala helper library here) you are getting the status sent because Twilio has sent the message to the carrier. The carrier will typically update Twilio to say the message was delivered.
There should be nothing wrong with your code at all. What you can do is capture the return value from:
messageFactory.create(params)
Which will contain a message ID (messageSID below). You can then fetch this message from the server to get an update on its status with something like:
var params = Map("SID" -> messageSID)
client.getAccount.getSmsMessage(params)
The full list of status are here. But it looks like everything you have should be spot on. Hope this helps.
I am using the Twilio java wrapper provided on the website and started making some tests. I am able to send text messages that are successfully received. However, I would like to make sure that the messages have been sent successfully and that no problem has been encountered by Twilio (bad phone number or whatever reason).
I understand that when you make a REST request to Twilio to send a SMS, Twilio responds with the Status. How could I get this response?
Here is the explanation page I found: http://www.twilio.com/docs/howto/sms-notifications-and-alerts
If you specify a StatusCallback URL when you make the request to send an SMS, we will make a request to the callback URL you provided after the message has finished processing, with the parameters either SmsStatus=sent or SmsStatus=failed. You can use this information to do more processing on the SMS message. There's more information here: http://www.twilio.com/docs/api/rest/sending-sms#post-parameters-optional
Alternately, if you hang on to the SMS Message Sid, you should be able to query the API for the message and get the status in the response. So if the sid is SM123, making a GET request to https://api.twilio.com/2010-04-01/Accounts/AC123/SMS/Messages/SM123.json should return a object with the status of the SMS Message.
I recall that the response comes to your url and can be matched up by an ID. In the REST post to SMSMessages you can specify a statuscallback url where Twilio will post a status message to your url.
When you receive that post to your site, you can record it or take any other action you need, such as retrying or using another mode of communication.
In 2020, with the Java SDK, you can now create a MessageFetcher with that SID and then call fetch to ask for the Message instance until its getStatus returns "delivered", "undelivered", or "failed".