How to send sms messages using twilio and windows scheduler? - php

I would like to schedule when I send sms messages using Twilio. I currently can schedule when to run php scripts using Windows Scheduler, however, whenever I try to send messages using twilio, the script doesn't execute.
PHP script that sends sms text message via using twilio
<?php
// Required if your environment does not handle autoloading
include '../vendor/autoload.php';
// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$sid = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
$token = 'XXXXXXXXXXXXXXXXXXXXXXXXXX';
$client = new Client($sid, $token);
// Use the client to do fun stuff like send text messages!
$client->messages->create(
// the number you'd like to send the message to
'+19894837813',
array(
// A Twilio phone number you purchased at twilio.com/console
'from' => '+19823837823',
// the body of the text message you'd like to send
'body' => 'Hey Jenny! Good luck on the bar exam!'
)
);
?>
VB script that references batch file
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\xampp\htdocs\Twilio\php\script.bat" & Chr(34), 0
Set WinScriptHost = Nothing
Batch file that calles php script
"C:\xampp\php\php.exe" -f "C:\xampp\htdocs\Twilio\php\index.php"

Related

make and receive call betwen browser and phone to discuss like a simple call

<?php
// Include the bundled autoload from the Twilio PHP Helper Library
require __DIR__ . '/vendor/autoload.php';
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = '8XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
// In production, these should be environment variables. E.g.:
// $auth_token = $_ENV["TWILIO_ACCOUNT_SID"]
// A Twilio number you own with Voice capabilities
$twilio_number = "+12244073476";
// Where to make a voice call (your cell phone?)
$to_number = '+22678733599';
$client = new Client($account_sid, $auth_token);
try {
$client->account->calls->create(
$to_number,
$twilio_number,
array(
// two url can't function because the first url will be erase by the value of the second
"url" => "http://demo.twilio.com/docs/voice.xml"
)
);
// echo "Started call : " . $client->sid;
} catch (Exception $e) {
echo "Error : " . $e->getMessage();
}
?>
When I start the call to the phone number from twilio number, it's okay. But it is impossible to make communication.
How can I make communication after answering the call?
Twilio developer evangelist here.
Your title says you want to connect a browser to a phone. However, your code only shows use of the REST API.
To create calls from a browser to a phone you need to use the Twilio Voice SDK. I would recommend that you go through the instructions to run the Voice SDK quick start application. That will explain how making a call from a browser to a phone works and give you an example application to run and investigate. The quick start application is fully documented.

How to send SMS to a PHP array with Twilio API

Using the Twilio API, I've got my PHP functioning to send to one phone number, and can successfully send. We're looking to send to multiple numbers from one request and to do so, I've set up an array of numbers to iterate through, however, I keep getting a 500 error when I attempt to send the message by hitting the URL. Below is the file I'm working with.
Running PHP 7.2 on a Linux server. I'm running CentOS 7.7 and Apache 2.4.43 if that matters at all.
// Require the bundled autoload file - the path may need to change
// based on where you downloaded and unzipped the SDK
require __DIR__ . '/twilio-php-master/src/Twilio/autoload.php';
// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$sid = 'XXXXXXXX';
$token = 'XXXXXXXX';
$client = new Client($sid, $token);
$a = array('+15555555555', '+15555555556');
$bodyTxt = “This is a test of sending the text message to multiple phone numbers.”
// Use the client to do fun stuff like send text messages!
foreach ($a as $v) {
$message = $twilio->messages
$client->messages->create($v, // to
[
"body" => $bodyTxt,
"from" => "+15555555557",
]
);
print($message->sid);
}
);
I'm not super familiar with PHP as I'm mostly in marketing, but I'm deputizing as developer in these crazy times because I know just enough to be dangerous. I'm thinking it is something with the foreach section, as that's the only piece that has changed from the single send.
Any help is appreciated!
Figured it out thanks to the help from #LuisE! I went through and figured out where I was missing the semicolons after the array, the $bodyTxt, and the $message = $twilio->messages.
// Require the bundled autoload file - the path may need to change
// based on where you downloaded and unzipped the SDK
require __DIR__ . '/twilio-php-master/src/Twilio/autoload.php';
// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$sid = 'XXXXXXXXXX';
$token = 'XXXXXXXXX';
$client = new Client($sid, $token);
$a = array('+15555555555', '+15555555556');
$bodyTxt = 'This is a test of sending the text message to multiple phone numbers.';
// Use the client to do fun stuff like send text messages!
foreach ($a as $v) {
$message = $twilio->messages;
$client->messages->create($v, // to
[
"body" => $bodyTxt,
"from" => "+15555555557",
]
);
print($message->sid);
}

How to set up twilio taskrouter outbound call?

I am trying to set up outgoing calls through Twilio task router. I am creating tasks through PHP with the all the necessary attributes (instruction, to , from, post_work_activity_sid ) but the created task doesn't set up a call between the twilio client and the external phone number. I was hoping that tasks created by the program would create a conference call between worker(browser) and external client. I keep getting an error that is shown below. I have a assignment php on my application server which de-queues calls to my workers (Browser clients). Currently, incoming calls from external number to browser clients through task router is working as expected. However, outbound calls generates a task and a reservation is assigned but Twilio is not able to dequeue the call to a worker.
Is there a way to create a task for voice call such that the task is created using Twiml Enqueue verb? Or is there a better way of handling outbound calls using the Twilio taskrouter so that calls are assigned successfully to the workers using Browser client ?
As per this thread: Can outbound calls be made through Twilio TaskRouter, I tried using instruction call.I have also gone through the documentation and another stack overflow post about assignment callback URL but it's not clear and am not sure what I could be potentially doing wrong.
Error message:
The dequeue instruction can only be issued on a task that was created using the TwiML verb
<?php
require_once('TwilioVendor/autoload.php'); // Loads the library
use Twilio\Rest\Client;
$sid = "ACxxxxxxxxxxxxxxxxxxxxxxx";
$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
try{
$twilio = new Client($sid, $token);
$task = $twilio->taskrouter->v1-
>workspaces("WSxxxxxxxxxxxxxxxxxxxxxxxxxxxx")->tasks-
>create(array("attributes" => json_encode(array(
//"instruction"=>"accept",
//"instruction"=>"conference",
"instruction"=>"call",
"to"=> "client:Bob",
"from"=> "+61123456789",
"post_work_activity_sid"=> "WAxxxxxxxxxxxxxxxxxxxx"
)),
"workflowSid" => "WWxxxxxxxxxxxxxxxxxx"
)
);
}catch(Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
print($task->sid);
**Assignment Callback code**
<?php
$assignment_instruction = [
'instruction' => 'call','to'=> 'client:Bob',
'from' => '+61xxxxx','url'=>'CRM REST END POINT'
];
header('Content-Type: application/json');
echo json_encode($assignment_instruction);
**CRM REST END POINT TWIML**
<?php
require __DIR__ . '/vendor/autoload.php';
require_once 'TwilioVendor/autoload.php';
use Twilio\Twiml;
$reservationSid= $_REQUEST['rsid']
header('Content-Type: text/xml');
?>
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say voice="woman">You will now be connected to the customer</Say>
<Dial>
<Queue reservationSid="<?$reservationSid?>"/>
</Dial>
</Response>
Twilio developer evangelist here.
TaskRouter will only generate calls to your workers when a task is created by the <Enqueue> TwiML verb. Creating a task with the REST API, even if you add call attributes, will not generate a call when you use the dequeue or call instruction.
Instead, you will need to manage the task and call yourself. When your worker is sent the reservation and accepts it, you should use the REST API to create the call, connect it to your browser client and then dial out to the end user.

convert text to voice using twilio in php

I want to convert text to voice message and send to users phone no
currently i am using making call api with TwiMLTM
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('/path/to/twilio-php/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "xxx";
$token = "{{ auth_token }}";
$client = new Services_Twilio($sid, $token);
$call = $client->account->calls->create("+343443", "+3444", "http://demo.twilio.com/docs/voice.xml", array(
"SendDigits" => "1234#",
"Method" => "GET"
));
echo $call->sid;
it is working,but this make call to user,but we need to voice message
Note : message is coming from textarea
Twilio Evangelist here.
This is a little tricky. Twilio allows you to make phone calls, so if the dialled number is answered by voicemail, you can leave a message. If it is answered by a human being, then you'll need to interact with them. It is possible to use the if_machine parameter when creating a call to detect an answering machine. However you cannot 'send' a voice mail as if sending an SMS or an email. You need to make the call, and decide how to handle that depending on who/what answers.
You could try sending the message as an SMS however, which would deliver the exact text to the user:
$sms = $client->account->messages->sendMessage("+343443", "+3444", $message_text);
However, if the number you are sending to is not able to receive an SMS, then you need to make the call and interact with either a human or answering machine.
Best of luck!

Send message from webserver to mobile chat app (like WhatsApp, Viber or Kik) on my smartphone?

I would like to send notifications from my webserver to my smartphone, preferably through one of the popular mobile chat apps like WhatsApp, Viber or Kik.
Is there any known documentation or API or something, that describes how to send a message to these clients, for example using PHP?
Note that I only need to be able to send notifications to my own smartphone, so requiring specific info to identify my particular client (like cellphone number or something) is fine.
There are many web services that allows you to send and receive SMS/notifications. PHP itself doesn't support this on it's own. You can use a service like Twilio to do this. You can send messages to your own smartphone, or even a friend's.
An example:
<?php
require "Services/Twilio.php";
// Step 2: set our AccountSid and AuthToken from www.twilio.com/user/account
$AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$AuthToken = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
// Step 3: instantiate a new Twilio Rest Client
$client = new Services_Twilio($AccountSid, $AuthToken);
// Step 4: make an array of people we know, to send them a message.
// Feel free to change/add your own phone number and name here.
$people = array(
"+14158675309" => "Curious George",
"+14158675310" => "Boots",
"+14158675311" => "Virgil",
);
// Step 5: Loop over all our friends. $number is a phone number above, and
// $name is the name next to it
foreach ($people as $number => $name) {
$sms = $client->account->sms_messages->create(
// Step 6: Change the 'From' number below to be a valid Twilio number
// that you've purchased, or the (deprecated) Sandbox number
"YYY-YYY-YYYY",
// the number we are sending to - Any phone number
$number,
// the sms body
"Hey $name, Monkey Party at 6PM. Bring Bananas!"
);
// Display a confirmation message on the screen
echo "Sent message to $name";
}
See the documentation here.
Hope this helps!

Categories