Sending optional parameters to Twilio Studio to trigger Flow using PHP SDK - php

I'm trying to send optional parameters to a Twilio Studio Flow Trigger using PHP. I followed the example shown in the Twilio Studio REST API docs and was successful triggering a new Flow using the (required) sender and recipient phone numbers.
use Twilio\Rest\Client;
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "my_auth_token";
$twilio = new Client($sid, $token);
$execution = $twilio->studio->v1->flows("FWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
->executions
->create("+15555559876", "+15555551234");
After getting the basic communications working, I now want to pass a couple parameters to the Flow. Unfortunately, I couldn't find any PHP examples that include optional parameters. (The docs mention how to access parameter values in the widgets with {{flow.data.parameterName}}, but not how to generate the request in PHP.)
The answer is probably easy and obvious but I can't figure it out and would appreciate any guidance.

Received an answer from Twilio Support.
$data = ["parameters" => ["foo" => "bar"]];
$flow = $twilio->studio->v2->flows("FWxxxxx");
$flow->executions->create($to, $from, $data);
Optional parameters are now passing through and can be used by widgets in the Studio Flow.

Related

DialogFlow - How to Batch Update Intents via API?

I'm looking for a basic example on how to utilize DialogFlow's batchUpdate, and how to utilize batchUpdateResponse to show an actual response once complete.
Have found no examples for DialogFlow V1 or V2 (at this point either would be helpful), the below is all I've managed to setup - looking for the missing arguments to be added:
$intentsClient->batchUpdateIntents($formattedParent, $languageCode, $test_3);
Currently using PHP https://github.com/googleapis/google-cloud-php/tree/83ae284c025f6e93b9ce835b987932c425b5a9de/Dialogflow but any language is fine here.
Ended up figuring this out through the use of https://developers.google.com/apis-explorer/ and the Google Client Library for PHP (https://github.com/googleapis/google-api-php-client).
Below is a basic example for updating the text on two intents at once, via PHP. Hopefully this helps someone in the future, am somewhat surprised at the general lack of helpful documentation and/or examples for using DialogFlow's API V2 (or even V1 for that matter). So many awesome things can be done by using this rather than their Dashboard to train your bot!
// Global variable pointing to the .json file downloaded with private key from DialogFlow
putenv('GOOGLE_APPLICATION_CREDENTIALS=directory-of-file/google-service-acount-key.json');
// Setup Google Client
require __DIR__.'/vendor/autoload.php';
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/cloud-platform');
$httpClient = $client->authorize();
// Setup array to update intent (minified)
$update_intent = array('intentBatchInline'=>array('intents'=>array(
0=>array('name'=>'projects/YOUR-PROJECT-NAME/agent/intents/FIRST-INTENT-ID','displayName'=>'FIRST-INTENT-NAME','messages'=>array(0=>array('text'=>array('text'=>array(0=>'FIRST-INTENT-TEXT-TO-UPDATE',),),),),),
1=>array('name'=>'projects/YOUR-PROJECT-NAME/agent/intents/SECOND-INTENT-ID','displayName'=>'SECOND-INTENT-NAME','messages'=>array(0=>array('text'=>array('text'=>array(0=>'SECOND-INTENT-TEXT-TO-UPDATE',),),),),),),),
);
// Post to DialogFlow API
$response = $httpClient->post('https://dialogflow.googleapis.com/v2/projects/PROJECT-NAME-HERE/agent/intents:batchUpdate', [
GuzzleHttp\RequestOptions::JSON => $test_batch_intent_1
]);
// Print out response for troubleshooting
print_r($response->getBody()->getContents());
echo "<br /><br />Here's to getting past DialogFlow API's hurdles! :)";
exit;
This is similar to my answer. Where I have given a complete example. Do check it out.
Stack Overflow answer.
And this is based out of NodeJs. As you told the language does not matter.
And do check out this documentation for different kinds of examples. This document covers even the batchUpdate functionality.
Please check these out:
GitHub repo
GitHub repo

HTTP Put via php client

I'm writing a small 'app' for a accounting site called Xena.biz
The system has an API that uses oAuth2 to connect to. All that I have sorted out - I can perfectly retrieve the information I need. Now I need to submit a PUT string back to Xena - here is where I can't figure it out.
The system is build on a file called XenaClient.php which contains all the authorization calls and all the requests.
Here is an example on how I retrieve information :
<?
require('XenaClient.php');
const CLIENT_ID = 'SECRET';
const CLIENT_SECRET = 'VERY SECRET';
$xenaclient = new XenaOAuth2Client(CLIENT_ID, CLIENT_SECRET);
$xenaclient->setAccessToken($_COOKIE["MaskedCookieName"]);
$ordertask = $xenaclient->fetch('https://my.xena.biz/Api/Fiscal/'.$_GET["fiscal"].'/OrderTask/'.$_GET["orderId"]);
var_dump($ordertask);
?>
But now I want to SEND information via PUT. According to the XenaClient.php script, I should use the command $xenaclient->fetch($url,$parameters);
So this is my shot
require('XenaClient.php');
const CLIENT_ID = 'SECRET';
const CLIENT_SECRET = 'VERY SECRET';
$xenaclient = new XenaOAuth2Client(CLIENT_ID, CLIENT_SECRET);
$xenaclient->setAccessToken($_COOKIE["MaskedCookieName"]);
$xenaclient->fetch('https://my.xena.biz/Api/Fiscal/96946/Order/243936250/Confirmation',array('ConfirmationDate'=>NULL,'ConfirmationReportLayoutId'=>261205291));
?>
This doesn't turn anything back, or doesn't create the wanted effect inside the accounting system. No help to get from Xena themselves, so this is why I ask you guys, hope you can help me.
Heres a few pointers:
The API resource: https://dev.xena.biz
XenaClient.php: https://github.com/EG-BRS/Xena.ExampleApp.PHP/blob/master/XenaClient.php
XenoOAuth2Client::fetch tells you, that the third parameter to the fetch function - although optional - determines the method of the request. Default ist GET (XenaOAuth2Client::HTTP_METHOD_GET), so if you want PUT, you should explicitly provide the appropriate PUT parameter (other methods see the XenoOAuth2Client class constants:
$xenaclient->fetch(
'https://my.xena.biz/Api/Fiscal/96946/Order/243936250/Confirmation',
array('ConfirmationReportLayoutId'=>261205291),
XenaOAuth2Client::HTTP_METHOD_PUT
);
most APIs consume json today, perhaps it's supposed to be json...
$xenaclient->fetch(
'https://my.xena.biz/Api/Fiscal/96946/Order/243936250/Confirmation',
json_encode(array('ConfirmationReportLayoutId'=>261205291)),
XenaOAuth2Client::HTTP_METHOD_PUT
);

Twilio sending message Using twilio library

Sending twilio message from my api returns this error
Twilio sending message The requested resource /2010-04-01/Accounts//Messages.json was not found
this is how I send a message. anyone incountered a problem?
$this->client = new \Services_Twilio($sid, $token);
return $this->client->account->messages->sendMessage($from ,$to, $message);
this is the documentation I followed
https://www.twilio.com/docs/api/rest/sending-sms
How do I create Messages.json
I used this https://github.com/twilio/twilio-php on laravel 4.2
Another Twilio developer evangelist here. Think I might be able to help.
The error message you got was this:
The requested resource /2010-04-01/Accounts//Messages.json was not found
The key point is the URL, particularly the double slash in the middle. That is where your Account Sid should be, thus leading to the 404 error.
In this case, I would double check how you are setting $sid. Make sure it is assigned before you try to create the Twilio client object.
Hi Twilio developer evangelist here.
Sorry to hear you're having trouble with your code.
You seem to not have posted your complete code, so I don't see where you actually require the library.
I have however written an example which worked for me.
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = 'AC';
$token = '01';
$client = new \Services_Twilio($sid, $token);
$message = $client->account->messages->sendMessage(
'+44', // From a valid Twilio number
'+44', // Text this number
"Hello monkey!"
);
I have removed some sensitive data on the code, but if you replace that with your token and numbers, you should be able to send a text message correctly.
Also, just in case you have the contents of the 'Services' folder elsewhere, make sure your paths are correct.
You have entered messages when it should be sms_messages.
CHANGE
$this->client->account->messages->sendMessage($from ,$to, $message);
INTO
$this->client->account->sms_messages->create($from ,$to, $message);
Also make sure that you are running this code inside a class otherwise you need to remove the $this-> from your code and use a local varible.

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!

Pass dynamic TwiML when making calls

How can I pass dynamic TwiML to the Twilio API when making calls?
$client = new Services_Twilio($sid, $token);
So instead of passing a URL to fetch the TwiML:
$call = $client->account->calls->create("+14158675309", "+14155551212", "http://demo.twilio.com/docs/voice.xml", array());
Could I dynamically generate the TwiML and pass it to the API?
$twiml = new Services_Twilio_Twiml();
$twiml->say( 'Hello Mark');
$call = $client->account->calls->create("+14158675309", "+14155551212", $twiml);
Twilio evangelist here.
Instead of specifying a static XML file in the create function, you can make this a PHP file and dynamically generate the response.
You still use create() to tell Twilio to initiate the phone call. When the call is answered, Twilio will request the URL you've specified to get the TwiML that tells it how to proceed with the call. So for example you change:
http://demo.twilio.com/docs/voice.xml
to
http://demo.twilio.com/docs/voice.php
And have the PHP generate the TwiML output:
$twiml = new Services_Twilio_Twiml();
$twiml->say( 'Hello Mark');
Here is the documentation for generating TwiML using the PHP helper library:
https://github.com/twilio/twilio-php#generating-twiml
Hope that helps.
Could I dynamically generate the TwiML and pass it to the API?
The answer is no, not without a URL. I realize this a delayed response but I have recently been searching for a way around this. The best method I have found of generating dynamic text for outgoing calls without a web server is here - bouncing it off of the twimlets url.

Categories