Twillio Notify Bulk Sms With Custom Number - twilio

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:

Related

Conf call: Wait for first person to respond, then connect second person

Basic case: System will call person A. If person A picks up the phone it will call person B and the 2 will be connected.
I read several answers here such as https://stackoverflow.com/a/20976996/1907888 but it's still unclear.
Would the following work? It would call PERSON_A if person responds it will connect to conference then call PERSON_B and connect to same conference? Do I need to start the conference first?
$response = new VoiceResponse();
$dial = $response->dial('PERSON_A');
if($dial->conference('Room 1234')) {
$dial = $response->dial('PERSON_B');
$dial->conference('Room 1234');
}
Twilio developer evangelist here.
When you control calls with Twilio there are two mechanisms by which it works. There is the Twilio REST API which your application can use to make things happen, like start or change a call. Then there are webhooks, which are HTTP requests that Twilio makes to your application when things change in a call, like someone dialling your Twilio number, entering data on the phone, or an person answering an outbound call. You respond to webhooks with TwiML, a subset of XML, with instructions for what to do with the call next.
In this case, you want to place a call to person A to start with. For this, you will need the REST API to make that call. When person A answers the phone, Twilio will then make a webhook request to your application to find out what to do next. It is at this point that you can both call person B, again using the REST API, and place person A into a conference call, by responding with TwiML.
So, your initial outbound REST API call should look a bit like this:
use Twilio\Rest\Client;
// Find your Account Sid and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);
$call = $twilio->calls
->create($personANumber, // to
$yourTwilioNumber, // from
["url" => "http://example.com/conference.php"]
);
The URL you send when you place the call will be where Twilio sends the webhook request. So, in response to example.com/conference.php in this case, you will need to dial another person and respond with TwiML to direct person A into the conference call.
This time, instead of sending a URL, you can actually send TwiML in the REST API response. Something like this:
use Twilio\Rest\Client;
use Twilio\TwiML\VoiceResponse;
// Find your Account Sid and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);
$twiml = new VoiceResponse();
$dial = $twiml->dial();
$dial->conference("Conference Name");
$call = $twilio->calls
->create($personBNumber, // to
$yourTwilioNumber, // from
["twiml" => $twiml->toString()]
);
echo $twiml.toString();
In this case, I have used the same TwiML for both legs of the call, because they are both entering the same conference. You could respond with different TwiML based on what's happening.
Let me know if that helps at all.

Get phone number with twilio conference participant sid

How can I get a conference participant's phone number using the Twilio API? I am using the official Twilio PHP Library.
Hello Twilio Developer Evangelist here. 👋
What you can do is to fetch all active participants with a snippet like this (you can find more details in the docs for the conference participants.
<?php
// Update the path below to your autoload.php,
// see https://getcomposer.org/doc/01-basic-usage.md
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
// Find your Account Sid and Auth Token at twilio.com/console
// DANGER! This is insecure. See http://twil.io/secure
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$twilio = new Client($sid, $token);
$participants = $twilio->conferences("CFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
->participants
->read(array(), 20);
foreach ($participants as $record) {
print($record->callSid);
// "CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
Now, when you did this you'll see that the participant record does not include the phone number of the participant. But what it includes is the call_sid. The call sid helps you to identify the caller and retrieve call details.
There you have two options (I described something similar just recently here).
1. Persist the call information when you route callers into the conference
What you could do when you route people into the conference is to persist their numbers and call sids in your application. This way you could have a registry with all the people that entered your conference and access their phone numbers quickly when you need them.
2. Make another API call to get the call details and caller number
When you have the call SID what you can always do is to fetch the call details one by one.
<?php
$call = $twilio->calls("CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
->fetch();
print($call->from);
The first approach saves you API calls with the cost of maintaining your own persistence layer. The second approach makes more API calls but it is easier to set up.
As always, it depends on the situation.
I hope that helps. :)

Twilio Alphanumeric-Sender-ID

I searched through all the documentation of Twilio. All I found is getting started with Alphanumeric-Sender-ID: https://support.twilio.com/hc/en-us/articles/223181348-Getting-Started-with-Alphanumeric-Sender-ID-for-Twilio-Programmable-SMS and articles like this.
No any of the link explaining about where I can add it. Please guide.
Twilio developer evangelist here.
Once your account has alphanumeric sender IDs enabled, as described in the article you linked to, you can use them in one of two ways.
Either you set your alphanumeric sender ID as the From parameter when sending a message using the messages resource. This is explained in a bit more depth in this article about changing the sender ID for SMS messages or in this blog post I wrote about sending messages with alphanumeric sender IDs.
Or, you can create yourself a messaging service with Copilot and setup the alphanumeric sender ID there. This has the benefit of allowing you to setup a number pool to fall back on for sending to countries that don't support alphanumeric sender IDs, like the US.
From your Stack Overflow account it looks like you mainly program PHP for your back end. Here's a quick example:
use Twilio\Rest\Client;
// Your Account Sid and Auth Token from twilio.com/console
$sid = "your_account_sid";
$token = "your_auth_token";
$twilio = new Client($sid, $token);
$message = $twilio->messages
->create($toNumber,
array(
"body" => "This is a branded message",
"from" => "ALPHA"
)
);
Let me know if that helps at all.

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.

Resources