I'm using twilio service in my rails app . I followed this tutorial. But after i run the app the message is sent to my twilio account , but i did not received any message to my mobile, other than the code in the link. I just added this code in my routes.rb:
Sms::Application.routes.draw do
get "send/sms"
root 'send#sms'
end
Send controllers code :
class SendController < ApplicationController
def sms
require 'rubygems'
require 'twilio-ruby'
#account_sid = 'AC6'
#auth_token = '2bf'
#client = Twilio::REST::Client.new(#account_sid, #auth_token)
#client.account.sms.messages.create( :from => '(954) 740-8336',:to => '+919884667438',:body => 'Hey there!')
end
end
and my sms action html file contails this :
Your message was successfully sent .
Twilio employee here.
On further inspection into your code, I noticed you're trying to send an SMS to an Indian number. The telephone networks have a lot of restrictions in India, which all companies must abide by.
The reason you are not receiving your SMS messages is because all Indian numbers are automatically opted-in to a Do Not Call Registry. You will need to opt-out your number in order to receive SMS in India.
For more information about sending / receiving SMS messages in India, check out our extensive FAQ section
You can get around this problem quickly by sending SMS to another non-Indian number, of course.
Related
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.
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
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.
I’m building a queue. I have one phone line, intended for users calling customer support, configured with a Voice URL of example.com/caller. That address returns TwiML that queues the call:
post '/caller' do
response = Twilio::TwiML::Response.new do |r|
r.Say "Welcome to Support, please hold!"
r.Enqueue 'Support Queue'
end
response.text
end
According to the official call queue example I need another phone line for customer support agents to call, that returns TwiML that dequeues a user in the queue. Is it possible for agents instead dequeue users using the browser client instead? Why doesn't this code work?
get '/'
capability = Twilio::Util::Capability.new account_sid, auth_token
# the TwiML app friendly-named 'Agent' has a voice URL set to example.com/agent
capability.allow_client_outgoing 'Agent'
token = capability.generate
erb :support, locals: { token: token }
end
post '/agent' do
response = Twilio::TwiML::Response.new do |r|
r.Dial do |d|
d.Queue 'Support Queue'
end
end
response.text
end
The actual error is that when I make a call from my browser using Twilio.Device.connect() the call hangs up immediately instead of connecting to the user in the queue.
Twilio evangelist here.
I think you need to change the value of the allow_client_outgoing parameter to a TwiML Application SID, which you can create in your account portal or via the REST API. This SID is what lets Twilio know what URL you want us to request when the instance of Client connects to Twilio.
Also, we recently released a new product called TaskRouter which will automatically route queued calls to an available agent, so you not longer have to have an agent dial into the Queue in order to get calls from it.
Hope that helps.
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?