Re-send a Twilio SMS Message - twilio

I store sent messages in a log table with their unique SIDs. With a periodic console task I iterate over the records having status = undelivered and request the status of it and if it's still undelivered, I'd like to re-send that very message. I don't want a new one as the message contains a verification code and we store only hash of it. Is it possible to re-send the old message having its SID?

Twilio Developer Evangelist here,
There is not a way to automatically resend an undelivered message using the API. You can work around this by grabbing the messages by SID and sending the undelivered ones again to the same number with the same body. When you use the REST API to get a message by SID, you have access to all of the data from that message, including the exact message body.
A quick example using the Twilio PHP Library would look like this:
<?php
$client = new Services_Twilio($AccountSid, $AuthToken);
$messageSIDs = array("Insert", "Array of", "Message SIDs", "here");
foreach ($messageSIDs as $sid) {
$msg = $client->account->messages->get($sid);
if ($msg->status == "undelivered") {
echo "Resending undelivered message for SID: $sid\n";
$sms = $client->account->messages->sendMessage(
// The number we are sending from.
$msg->from,
// The number we are sending to.
$msg->to,
// The sms body.
$msg->body
);
}
}
You can see all of the data that the REST API gives you about messages here

Related

Why can't I programmatically send a templated WhatsApp message using Twilio?

My code:
const client = require('twilio')(accountSid, authToken);
const issueResolution = 'Hi Malcolm, were we able to solve the issue that you were facing?';
client.messages
.create({
from: senderNumber,
body: issueResolution,
to: receiverNumber,
})
.then(message => {
console.log(JSON.stringify(message , null, 2));
});
The body of the message conforms to the "sample_issue_resolution" WhatsApp template shown in the Twilio web console (at https://console.twilio.com/us1/develop/sms/senders/whatsapp-templates).
I can successfully use the above code to send a message to a number which has opted in to my Twilio WhatsApp sandbox. If I try to send a message to another number which has not opted in to the sandbox, there's no error message, but the message is never received. I get the same results when I use one of my custom approved templates. What could be going wrong / how can I debug this?
I'd assume that this message is not matching any of your templates. There could be multiple reasons, such as a different whitespace character or a typo that differentiates both strings.
The Message Log should tell you why a message couldn't be delivered.

While adding Alphanumeric Sender ID I always got error in php

I had purchased a USA number and I am trying to send a programable message to Hong Kong. While I send with a simple Twilio number it works fine and message recieved.
But when I try to add alphanumeric Sender ID it always show error message. The error message is: Message: [HTTP 400] Unable to create record: The 'From' number infoSMS is not a valid phone number, shortcode, or alphanumeric sender ID.
What I had tried is
public function send_sms(){
$msg_to = $_REQUEST['msg_to'];
$msg_body = $_REQUEST['msg_body'];
$twilio = new Client("ACba7f3a6866a23aed021056d3ceaexxxx", "4d7e47e92a8351b7365ed1e3e83dxxxx");
$message = $twilio->messages->create(
$msg_to, // to
[
"body" => $msg_body,
"from" => "infoSMS";
]
);
if($message->sid) {
$this->session->set_flashdata('ok_message', '- SMS successfully sent!');
redirect(AURL . 'Sms/sent_sms_list');
}else {
$this->session->set_flashdata('err_message', '- Error in sending sms please try again!');
redirect(SURL . 'Sms/create_sms');
}
}
I had read the documentation: https://www.twilio.com/docs/sms/send-messages#use-an-alphanumeric-sender-id
What I am doing wrong? Thanks in advance.
Twilio developer evangelist here.
This was a bit hard to track down, but it turns out that there are a few generic alphanumeric sender IDs that may cause delivery failures. I tried to send an SMS using "infoSMS" as the sender ID and was unable to do so as well.
On some of our SMS guideline pages, there is this note about alphanumeric sender IDs:
Generic Alpha Sender IDs like InfoSMS, INFO, Verify, Notify etc should be avoided as they will result in SMS failures
I recommend you try using a different alphanumeric sender ID that more closely represents your business or service.

Is there any way to get messages we send/received by using phone number in twilio?

I would like to make my client to check whether end client is received text or not and what reply he/she has sent? I always going through twilio to see whether client received sms or not? Is there any way to check it from twilio?
Twilio developer evangelist here.
You can get both incoming messages to your Twilio numbers and reports on the status of messages after you send them from Twilio using webhooks.
When you send a message you can include a StatusCallback parameter. The parameter should be a URL in your application.
$client->messages
->create(
$to,
array(
"from" => $from,
"body" => "Let's grab lunch at Milliways tomorrow!",
"statusCallback" => "https://example.com/messageStatus"
)
);
Twilio will send a POST request to the statusCallback URL each time your message status changes to one of the following: queued, failed, sent, delivered, or undelivered. It will include the original message's SID, so you can tie these webhooks back to the message you sent.
Similarly, you can get these webhook notifications for incoming messages to your Twilio numbers. For this you need to set up the incoming webhook URL to the number in your Twilio console. Set it to a URL in your application and you will receive a webhook when someone sends a message to your Twilio number. Check out this quickstart guide on receiving messages to your Twilio number with PHP.
Let me know if that helps at all.
[edit]
Thanks for the comment where you made it clear that this is after the fact, not at the time of sending.
In this case, you can absolutely list the messages by the phone number that sent them. A message resource includes a Status attribute that lists the current message state in the Twilio system, anything from "accepted" and "queued" to "sending", "sent", "delivered", "undelivered" and "failed". You can see more about these statuses in the documentation.
To get the list of messages sent from a number you can use the following code:
use Twilio\Rest\Client;
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$client = new Client($sid, $token);
// Loop over the list of messages and echo a property for each one
foreach ($client->messages->read(array("from" => $FROM_NUMBER) as $message) {
echo $message->status . " - " . $message->to . " = " . $message->body;
}
You can pull data for specific messages https://www.twilio.com/docs/api/messaging/message#delivery-related-errors
or you can simply pull all the logs

Is user Custom parameters allowed with twilio?

I am using Twilio service to send messages to different types of users in my application. Now the message logs show all messages listing properly, but can I add an optional parameter to identify a different set of messages sent to different users.
Is user Custom parameters allowed with Twilio message logs to identify/filter Messages based on this parameter?
If my way of thinking is wrong, is there any solution to categorize or identify a specific group of messages sent to a particular group of people?
for ($i = 0; $i < count($mobile_numbers); $i++)
{
$mobile_number = $mobile_numbers[$i];
$otp_message_result = json_decode(send_text_message($mobile_number, $custom_message));
$msg_sid = $otp_message_result -> sid;
$msg_status = $otp_message_result -> status;
}
Twilio developer evangelist here.
There are no custom fields available within the Twilio API for saving your own data to.
If you want to find all the messages sent to one telephone number then you can filter by that number, like this:
use Twilio\Rest\Client;
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "your_account_sid";
$token = "your_auth_token";
$client = new Client($sid, $token);
$phone_number = PHONE_NUMBER;
// Loop over the list of messages and echo a property for each one
foreach ($client->messages->read(array("To" => $phone_number)) as $message) {
echo $message->body;
}
If you need to be any more specific, then I recommend you save the SIDs of the messages that you sent to each user to your own database.

Getting notified in another browser when a chat message is sent in Twilio

I am sending messages in using Twilio using this code:
string channelId = <channelId>;
string serviceSid = <servicesid>;
IpMessagingClient ipMessagingClient = new IpMessagingClient(<accountid>, <token>);
var msgResult = ipMessagingClient.CreateMessage(serviceSid, channelId, "sender", "message");
msg.Body = "body"
msg.DateSent = DateTime.Now;
msg.Sender = "sender"
return Json(new
{
Success = msgResult.Sid != null,
NewMessage = msg
});
Message is sent just fine and I can retrieve the messages of the channel. What I am trying to achieve now is other open browser and in the channel gets notified of new messages sent so I can update the UI to display the new messages without manual refreshing the browser.
Twilio developer evangelist here.
It looks like you are using the REST API to send messages to the Programmable Chat API.
To get using the client versions, the JS client in particular, I would take a read through the Programmable Chat documentation, go download the JS SDK and take a look at the code for the reference chat demo. All of those bits should give you a good grounding in building chat for the client side.
Let me know if that helps at all.

Resources