Is there any way to get messages we send/received by using phone number in twilio? - 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

Related

Error: HTTP 400 Unable to create record: Twilio could not find a Channel with the specified From address

I use the credentials that Twilio provides me in the dashboard:
Checking that they are the live credentials:
I also check that the number I want to use is the one that is confirmed in sandbox.
I have the following code snippet:
$twilio_number = "+51XXXXXX618";
$client = new \Twilio\Rest\Client(
'account_sid',
'auth_token'
);
$message = $client->messages->create(
"whatsapp:+51XXXXXX148",
[
// "from" => "whatsapp:+14155238886",
"from" => "whatsapp:".$twilio_number,
"body" => "Prueba de envio de msj"
]
);
And I get the following error (code in php):
Twilio developer evangelist here.
In your code you appear to be trying to send a message from the number that starts +51.
That number is a participant in your sandbox, but that means you can send messages to that number, not from it. When you are using the Twilio WhatsApp sandbox you can only send messages from your sandbox number, the one starting +1415.
To move on from the sandbox, you need to follow these instructions to register a number with WhatsApp that you can use for your business.

Twillio Notify Bulk Sms With Custom Number

I want to send bulk sms using twillio notify in php having a custom text in place of the number ("From") but dont seem to know how to go about it. Am using a messaging service. I would like to show the custom text instead of my sending number when the message is sent. Below is my code for sending the message
<?php
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
$accountSid = "your_account_sid";
$authToken = "your_auth_token";
$serviceSid = "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$client = new Client($accountSid, $authToken);
$recipients = array($num1, $num2, ...); // Your array of phone numbers
$binding = array();
foreach ($recipients as $recipient) {
$binding[] = '{"binding_type":"sms", "address":"+1'.$recipient.'"}'; // +1 is used for US country code. You should use your own country code.
}
$notification = $client
->notify->services($service_sid)
->notifications->create([
"toBinding" => $binding,
"body" => $text
]);
?>
You might be able to send them from Twilio verified phone numbers, but in-general that's not good practice, as your "From" number will get flagged as spam by phone companies
Further than that, you won't be able to change the 'From' attribute, as Twilio doesn't want you fraudulently impersonating other people's phone numbers.
If you can't afford a short code, use a messaging service and buy a bunch of numbers to accommodate your volume.
Twilio developer evangelist here.
When you are using a messaging service, you can setup an alphanumeric sender ID as part of the copilot features for the service. Open up your messaging service settings and add an alphanumeric sender as shown in the screen shot below:

Re-send a Twilio SMS Message

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

Twilio check if phone number has been blacklisted

I am currently integrating into the twilio rest api and need to perform a check on a users phone number to determine if that user has blacklisted themselves or not. I have little experience with this api and scouring through the documentation and google has turned up nothing.
In our application we are going to have a notification center and if the user has blacklisted themselves I do not want to give them the ability to turn on their SMS notifications. Potentially a user could have SMS notifications on but twilio would block any messages. I know there is the ability to get a status code back from twilio when an SMS is queued that shows the user is blacklisted (https://www.twilio.com/docs/api/rest/message). However, I will not be sending messages on the notifications screen and need a direct way (if at all possible) to check twilio to determine if a number is blacklisted. Any help is much appreciated. Let me know if anymore information will be of help.
Megan from Twilio.
I'd be curious to see if you ever tried your own workaround. But I wanted to note for others in a similar situation how you could grab the blacklist error and then do whatever you may want with it.
In Ruby it would look something like this:
require 'rubygems'
require 'twilio-ruby'
account_sid = 'YOUR_ACCOUNT_SID'
auth_token = 'YOUR_AUTH_TOKEN'
#client = Twilio::REST::Client.new account_sid, auth_token
begin
#message = #client.messages.create(
from: 'TWILIO_NUMBER',
to: 'USER_NUMBER',
body: 'Howdy!'
)
rescue Twilio::REST::RestError => e
if e.code == 21610
# User is blacklisted
# Store info however you choose
puts e.message
end
end
We check for blacklisting specifically using the code '21610'. For more information about errors you can visit the reference page.
Hope this helps!
Twilio recommends developers to store the opt-out/in statuses in their side. I have stored it in DB. There are 2 ways to collect the unsubscribed users list.
1) Use SMS webhooks. You can find how to configure your Twilio number to receive webhook events here
#PostMapping(value = "/twilio", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
produces = MediaType.APPLICATION_ATOM_XML_VALUE)
public String twilioConsumer(TwilioEventDTO twilioEventDTO) {
// twilioEventDTO.getBody() => returns the body of the SMS user replied.
twilioService.consume(twilioEventDTO);
return new MessagingResponse.Builder().build().toXml();
}
2) Since I implemented webhooks later, I had to collect already unsubscribed users. When you send sms to the number that has been opted-out, Twilio API throws an exception with the status number of 21610. You can catch it and store the number in DB.
try {
Message result = Message.creator(
new PhoneNumber(toPhoneNumber),
new PhoneNumber(fromPhoneNumber),
messageBody)
.create();
response = result.getStatus().name();
} catch (ApiException e) {
if (e.getCode().equals(21610))
updateSubscription(toPhoneNumber, false);
logger.warn("Error on sending SMS: {}", e.getMessage());
}
P.S.: examples written in Java - Spring Boot framework.

Twilio sms not coming in mobile

I have implemented test twilio sms application. I have downloaded three files from https://github.com/benedmunds/CodeIgniter-Twilio
I have used my account_sid, auth_token in twilio.php file which is in config folder. I have also used 'from' number as +15005550006 in that file.
I have used following codes in my controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Twiliosms extends TL_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->library('twilio');
$from = '+15005550006';
$to = '+91xxxxxxxxxx';
$message = 'This is a test...';
$response = $this->twilio->sms($from, $to, $message);
//echo "<pre>";print_r($response);echo "</pre>";
if($response->IsError)
echo 'Error: ' . $response->ErrorMessage;
else
//echo 'Sent message to ' . $to;
echo 'Sent message';
}
}
Now when I run the controller file in the browser(not in my machine but in server), it runs successfully and it shows "Sent message".
But no sms is received. Please help.
My name is Jarod and I work at Twilio. There are multiple reasons this could be failing so lets walk through them one at a time.
1. Sending an sms using Twilio test credentials will not result in an sms being sent.
Test credentials are used for testing and debugging your application. They are "fake" numbers, that do not send any data via carrier, but instead return preset error codes and responses in order to test response-handling in your application. As you experienced this specific test credential +15005550006 will always return NO ERROR, whereas +15005550001 will always respond with "This phone number is invalid".
2. You can only send SMS from a Twilio number or a verified phone number.
You can always send an SMS from a number you bought on Twilio.com, but in order to send an SMS from any other number you must first verify that phone number with Twilio. Read more about the difference here.
These are the most common reasons for not receiving and actual SMS but in the case of the Indian phone numbers it could very well be one of these rare limitations.
If none of these work we probably need to look into your specific account to find out what's happening, in which case you should reach out to support#twilio.com.
Hope this helps.
I banged my head against the walls for three hours and tried to work out with the number Twilio gave me at the time of sign up . But It always gave me the following error
{ "code": 21212, "message": "The 'From' number +234234234 is not a
valid phone number or shortcode.", "more_info":
"https://www.twilio.com/docs/errors/21212", "status": 400 }
At the end i got the exact point from where I got my test phone number for sms / call
Get your test phone number from Here .
You will have to make the from number a number you have verified with Twilio that you own: you can't just pick any number and use it. Do you own +15005550006, and have you verified that number in the Twilio admin console?

Resources