I'm trying to send bulk sms through twilio Api. Is there any method to pass the array of all phone numbers in a single API request.
Twilio developer evangelist here.
Yes there is now! It's known as the passthrough API (as it allows you to pass through many different messaging systems and send bulk messages. It's part of the Notify API and you can use it to send bulk SMS messages. You need to set up a messaging service and a notify service in your console, then you can use the following code:
<?php
// NOTE: This example uses the next generation Twilio helper library - for more
// information on how to download and install this version, visit
// https://www.twilio.com/docs/libraries/php
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
// Your Account SID and Auth Token from https://www.twilio.com/console
$accountSid = "your_account_sid";
$authToken = "your_auth_token";
// your notify service sid
$serviceSid = "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
// Initialize the client
$client = new Client($accountSid, $authToken);
// Create a notification
$notification = $client
->notify->services($serviceSid)
->notifications->create([
"toBinding" => [
'{"binding_type":"sms", "address":"+15555555555"}',
'{"binding_type":"sms", "address":"+12345678912"}'
],
"body" => "Hello Bob"
]);
Checkout the documentation on sending multiple messages with the Notify passthrough API for all the details.
In case someone else had troubles with preparing the toBinding parameter from a PHP array() as I had, here is an example for that:
<?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
]);
?>
First of all, you need to configure your twilio number properly for notification. Then your to use below code to send bulk SMS.
$message = 'Any text message';
$to = array();
foreach ($users as $user) {
$to[] = '{"binding_type":"sms", "address":"'.$user->phone_number.'"}';
}
$sid = 'TWILIO_ACCOUNT_SID';
$token = 'TWILIO_AUTH_TOKEN';
$services_id = 'TWILIO_SERVICE_ID';
$twilio = new Client($sid, $token);
$notification = $twilio
->notify->services($services_id)
->notifications->create([
"toBinding" => $to,
"body" => $message
]);
Related
I am trying to send sms using twilio but not getting any response or unable to send sms
I am using following code,where i am wrong ?
require __DIR__ . 'Twilio/autoload.php';
use Twilio\Rest\Client;
$sid = "xxxxxxxxxxxxxxxxxx";
$token = "xxxxxxxxxxxxxxxxxxx";
$twilio = new Client($sid, $token);
$message = $twilio->messages
->create("+91xxxxxxx", // to verified twilio number
array(
"body" => "This is the ship that made the Kessel Run in fourteen parsecs?",
"from" => "+xxxxxxx"
)
);
print($message->sid);
Following reasons may possible in this case.
Make sure that you have CURL module enable
var_dump($message);
Try to print Twilio object $twilio to make sure that library is correctly loaded .
Turn on php error by adding top of the script
error_reporting(E_ALL);
ini_set('display_errors','1');
I am working on an app that reads and updates values in a Google Spreadsheet using Google Sheets API. I am able to read using my developer key, however attempting to write returns this error:
"Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential."
Read (works fine):
$client = new Google_Client();
$client->setApplicationName("XXX");
$client->setDeveloperKey("XXX");
$service = new Google_Service_Sheets($client);
$spreadsheetId = "XXX";
$range = 'promocodes';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
Write code (error):
$client = new Google_Client();
$client->setApplicationName("XXX");
$client->setDeveloperKey("XXX");
$service = new Google_Service_Sheets($client);
$spreadsheetId = "XXX";
$range = 'promocodes!C4';
$values = [1];
$body = new Google_Service_Sheets_ValueRange([
'values' => $values
]);
$params = [
'valueInputOption' => $valueInputOption
];
$result = $service->spreadsheets_values->update($spreadsheetId, $range,
$body, $params);
printf("Cells updated.", $result->getUpdatedCells());
As I understand it, the Google API will allow you to read without an access token (using a developer key for credentials) however you can not update or add information without an oauth2 authentication method which involves sending credentials to google, receiving back a code from them, using that code to get an access token, then using that access token as your credentials to add or update information.
I am using twilio to send sms to mobile, when i run the script it sends an SMS like it should to my mobile but the problem is i don't know the status of the sent SMS, how do i get that?
What i tried so far is below
<?php
require __DIR__ . '/vendor/autoload.php';
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';
// In production, these should be environment variables. E.g.:
// $auth_token = $_ENV["TWILIO_ACCOUNT_SID"]
// A Twilio number you own with SMS capabilities
$twilio_number = "+0987654321";
$client = new Client($account_sid, $auth_token);
$client->messages->create(
// Where to send a text message (your cell phone?)
'+1234567890',
array(
'from' => $twilio_number,
'body' => 'I sent this message in under 10 minutes!'
)
);
after doing proper research, and having all kind of issues with the webhook, here is what i settled on
$phoneString = $phoneNumber->asNorthAmericanDialingString();
$mi = $client->messages->create(
// the number you'd like to send the message to
$phoneString ,
array (
// A Twilio phone number you purchased at twilio.com/console
'from' => Config::getCurrentConfig()->smsConfig->fromNumber ,
// the body of the text message you'd like to send
'body' => $message ,
'ProvideFeedback' => "true"
)
);
$msg = $mi->fetch();
$sid = $msg->sid;
$status = $msg->status;
if ($status == 'sent' || $status == 'delivered') { // f'ing twilio, random status
self::getClassLogger()->debug("Sent text message[$sid], status is [$status]");
return new TextMessageStatus($sid , $status);
} else {
// do whatever is appropriate for your case
}
I am trying to upload using client on twilio and trying buy a twilio number.
Also, I need to get a dynamic voice URL in order to record a conference call dynamically.
<?php
require_once 'vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "*********************";
$token = "*******************";
$client = new Client($sid, $token);
$numbers = $client->availablePhoneNumbers('US')->local->read(
array("areaCode" => "424")
);
$twilioNumber = $numbers[0]->phoneNumber;
$newNumber = $client->incomingPhoneNumbers->create(
[
"voiceUrl" => "host url",
"voiceMethod" => "GET"
]
);
if ($newNumber) {
return $twilioNumber;
} else {
return 0;
}
//
In the voiceUrl parameter i am passing a conference call connect code hosted link but its not getting updated to twilio account dynamically.
You can use like
require 'Services/Twilio.php';
$account_id = ACCOUNT_SID;
$auth_token = AUTH_TOKEN;
$number = $_REQUEST['id'];
$client = new Services_Twilio($account_id, $auth_token);
try{
$number= $client->account->incoming_phone_numbers->create(array('PhoneNumber' =>'+'.$number));
$number_sid=$number->sid;
$number1 = $client->account->incoming_phone_numbers->get($number_sid);
$number1->update(array("VoiceUrl" => "http://11.11.1.111/test/twilio_call_response.php","SmsUrl"=>"http://11.11.1.111/test/incomingsms.php","VoiceFallbackUrl"=>"http://11.11.1.111/test/fall_backurl.php"));
$phone_number=str_replace('+','',$numbers);
$allocate='1';
}catch(Exception $e){
echo $err = "Error purchasing number: {$e->getMessage()}";
}
echo $phone_number;
I am trying to make a twitter application. I have gotten my application to a point where users log in through Twitter's user verification system. I am also able to send status updates using my application. I have this function to send status updates:
function sendTweet($tx){
$consumer_key = 'MY CONSUMER KEY';
$consumer_secret = 'MY CONSUMER SECRET';
$Twitter = new EpiTwitter($consumer_key, $consumer_secret);
$Twitter->setToken($_SESSION['OT'],$_SESSION['OTS']);
$text=$tx;
$status=$Twitter->post_statusesUpdate(array('status' => $text));
$status->response;
}
This sendTweet($tx) function works like a charm but now what i need to do is get a list of followers of a user and then send Direct Messages to them. How can I do this using oauth and PHP?
function sendDirectMessage($user,$message)
{ $consumer_key = 'YOUR CONSUMER KEY';
$consumer_secret = 'YOUR SECRET';
$Twitter = new EpiTwitter($consumer_key, $consumer_secret);
$Twitter->setToken($_SESSION['OT'],$_SESSION['OTS']);
$myArray = array('user' => $user, 'text' => $message);
$resp = $Twitter->post_direct_messagesNew( $myArray);
$resp->response;
}