How to get Twilio Recording file in PHP - php

I am using Twilio application. I want to get the path of my recording. I am using this code:
$client = new Services_Twilio($sid, $token, $version);
$calls = $client->account->calls->getIterator(0, 50, array(
));
$url = $call->subresource_uris->recordings;
in the variable $url /2010-04-01/Accounts/<acount id>/Calls/callerid/Recordings.json
But when i paste this url "https://api.twilio.com/2010-04-01/Accounts/<acount id>/Calls/callerid/Recordings.json" it gives me alert popup to enter user name and password.
Basically i want this file to save on my system

Twilio evangelist here.
The URL you are using is requesting a list of Recording resources from Twililo, not the actual audio files. Each resource contains a property called Url that represents that actual audio recording file.
If you want to download the audio, you will need to loop through that list of recordings and make a new HTTP GET request to the Url specified in the Url property.
Hope that helps.

Related

How to create a Slack message containing an uploaded image?

I'd like to create a message in the #general channel of my Slackspace from within a PHP script. The message should contain text and an image which was created locally on-the-fly.
I've already created an App, generated an bearer token and have managed to create an text-only message also as an image-upload.
But i didn't know how to create both in one message, as the procedure above creates two messages, one with text and another one with the image.
There are two different approaches on how to post a Slack message with an image.
A. Upload image directly with message
You can upload an image directly to Slack and share it in a channel. Within that request you can also add a comment that will appear as message above the images. This is the easiest approach, however you comment is limited to one string.
API method: files.upload with these arguments:
channels: ID of one or multiple channel for the image to appear in
initial_comment: Your message
B. Post message with image block / attachment
Alternatively you can add an image to your normal message either as image block or secondary attachment. This only works with a public URL to your image file, so you first need to upload your image to an image hoster (which can be your Slack workspace) to get the public URL.
In our example we will use Slack as image hoster, but you can use any image hoster (e.g. Imgur) even your own webserver, as long as you get a public URL for your image file.
Step 1 - Upload image to Slack
API method: files.upload with no special arguments, but make sure to get the file ID from the response. Don't include the channels argument or the image will be posted visible into those channel.
Step 2 - Create public URL
Next you have to mark the uploaded file as public. Only then will it be accessible through its public_url property
API method: files.sharedPublicURL with the file ID as argument.
Next you need to construct the direct image link from the link to website / permalink_public property of the file.
The website link you get from permalink_public has the format:
https://slack-files.com/{team_id}-{file_id}-{pub_secret}
The direct link to the image has the format:
https://files.slack.com/files-pri/{team_id}-{file_id}/{filename}?pub_secret={pub_secret}
So you just need to extract the pub_secret from permalink_public and you should be able to construct the direct link to the image. The other parameters you can get from your file object.
Step 3 - Send message
Finally compose your message with the image URL either as Image Block or in a secondary attachment and submit it using a method of your choice.
API method: chat.PostMessage or any other method for sending message incl. incoming webhooks.
Answer to OP
If you need to stick with webhooks as it appears from your comments and have no access to the Slack API I would suggest uploading the image to an image hoster (e.g. Imgur) and then use approach B.
See also
Slack bot send an image
Can I upload an image as attachment with Slack API?
How to use the permalink_public URL of an uploaded image to include it in a message?
After much tinkering I found that while I could not use the API to create a message and upload an image simultaneously, I can first upload an image and then, with the timestamp returned, use update message to add text to the original message with the image upload.
This is the flow:
1- Use files_upload method to upload an image to my channel (using the channel name)
response = client.files_upload(
channels=my_channel_name,
file=image_path,
initial_comment='My initial comment'
)
2- Get the response from the files_upload and extract the channel id and timestamp of the message.
channel_id = response['file']['groups'][0]
ts = response['file']['shares']['private'][channel_id][0]['ts']
3- Use chat update to add text or rich content to the message with the uploaded image:
response = client.chat_update(
channel=channel_id,
text="My Message",
ts=ts,
blocks=blocks_list
)
For those who might still need this.. this gist helped me. Its a quick and easier way using GuzzleHttp.
use GuzzleHttp\Client;
/**
* Notes:
*
* Tested with guzzlehttp/guzzle version 7.3
* See https://api.slack.com/methods/files.upload for details on how to generate a token
*
*/
$fileName = '';
$filePath = '';
$slacktoken = ''; // See https://api.slack.com/tokens; The token will need file.read and file.write permissions
$client = new Client();
$apiUrl = 'https://slack.com/api/files.upload';
$client = new Client();
$request = $client->post( $apiUrl, [
'headers' => ['Authorization' => 'auth_trusted_header'],
'multipart' => [
[
'name' => 'token',
'contents' => $slacktoken,
],
[
'name' => 'file',
'contents' => fopen($filePath, 'r'),
'filename' => $fileName
],
[
'name' => 'channels',
'contents' => '#general'
],
[
'name' => 'initial_comment',
'contents' => 'File Uploaded'
]
]
]);
var_dump($request->getBody()->getContents());

Get Google drive video embed code using api

Hello everyone,
I was working on a project where i need to display a video specifically from my google drive on a webpage. I don't want to manually select the video and copy its embed code, is there a way we can obtain the embed code for a video using google api (PHP preferred) ?
So that i can use this code within an iframe and display the video.
Please also suggest if there are other ways.
Thanks in advance,
You can start with Google Drive API https://developers.google.com/drive/v3/web/quickstart/php and this segment of code i hope can help you.
<?php
// New Google Client, see all settings at link in description
$client = new Google_Client();
// New Google Drive Service
$service = new Google_Service_Drive($client);
// Params with filter of MIME type for search only videos mp4 (you can change this)
$optParams = [
'q' => "mimeType='video/mp4'",
'pageSize' => 10,
'fields' => 'nextPageToken, files(id, name, webViewLink)'
];
// Get files from our request
$files = $service->files->listFiles($optParams);
// Print an iframe for each video
foreach($files->files as $file){
// Now I need to make a little detail about the next lines of code so look at [Info]
$src = str_replace('/view', '/preview', $file->webViewLink);
echo '<iframe width="500" height="200" target="_parent" src="'. $src .'"></iframe>'
}
?>
[Info]
The objects returned from $service->files->listFiles($optParams) are Google_Service_Drive_DriveFile objects and each of these have a series of properties. From v3 of Google API a properties called embedLink was removed and there isn't an alternative for this property from v2(Migrate to Google Drive API v3) so as you seen in my code i use webViewLink property that return a URL to a view of file like this:
https://drive.google.com/file/d/123456789/view
But if you use this URL inside an <iframe> the browser notice to you an error:
Refused to display 'https://drive.google.com/file/d/123456789/view' in a frame because it set 'X-Frame-Options' to 'sameorigin'.`
I'm not holding you too much on this issue and there are a lot of question about this. So we need to request a preview of file and not a view an i do that with
str_replace('/view', '/preview', $file->webViewLink);
on the URL returned by the request. Now you can use this URL in a <iframe>

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.

How to make browser-phone call with twilio

i am currently stacked with twilio client call where i have to call a user from browser. Lets see the scenario.
start snippet:
include 'twilio/Services/Twilio/Capability.php';
include 'twilio/Services/Twilio.php';
$accountSid = 'ACxxxxxxxxxxxxxxx';
$authToken = 'xxxxxxxxxxxxxxxxx';
$token = new Services_Twilio_Capability($accountSid, $authToken);
$token->allowClientOutgoing('APXXXXXXXXXXX');
$client = new Services_Twilio($accountSid, $authToken);
$call = $client->account->calls->create("twilio Number", "client number", "https://www.mysite.com/twilio/callback", array());
And my call back goes like this:
function callback(){
$xml = "<Response><Say>Placing your call now.</Say><Dial callerId='twilio verified number' timeout='40' record='true'></Dial></Response>";
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" . $xml;
exit();
}
I dont know what exactly the $from, $to parameters with in the calls->create() function takes. If we are initially initiating the call to $to then, it should work.
My requirement is to call a client to client number from my browser. Yes, i see the referencees here but stacked again.
Twilio evangelist here.
Looks like your current code is using the REST library to initiate the call. This send a message to Twilio to initiate a phone call from Twilio to some phone. All of that happens on the server, not in the browser.
In your case it sounds like you want to actually make a phone call right from the browser. To do that you need to use Twilio Client, which is a JavaScript framework that lets you create an audio connection from a browser to Twilio, and the connection is made using WebRTC (or Flash if needed). Its the job of the Twilio Client SDK to figure out how to create and manage the actual audio connection between your browser and Twilio.
http://www.twilio.com/client
https://www.twilio.com/docs/tutorials/walkthrough/browser-calls/php/laravel
This is different than initiating a call using the REST API. When you initiate an outbound call using the REST API, the API does not actually manage the audio connection. Your using the API simply to tell Twilio to initiate a phone call between Twilio and a phone (or to send a text message), or to ask Twilio for information (like getting list of send SMS messages). The management of the actual audio connection is handled by Twilios internal systems.
The first part of your PHP code is correct you need to create a Twilio Capability token. That token is used by our JavaScript library. This tutorial shows the JavaScript code that you can use to make a call from your browser to Twilio:
https://www.twilio.com/docs/tutorials/walkthrough/browser-calls/php/laravel
Once the browser connects to Twilio, Twilio will make a request to the URL you've set on your TwiML App (the string starting with "AP" that you specified when you created the capability token). You can return whatever TwiML you like from this URL, including using the <Dial> very to tell Twilio to bridge the Twilio Client connection into a <Conference>.
Hope that helps.
Devin
This will be helpful. If you look through the code, there is some api (i think callFromBrowser()) which does the trick.

Upload video to youtube using php client library v3

I am trying to upload a video to youtube using client library v3.
The v3 library is experimental and does not have much documentation
(samples provided does not include youtube)
I have properly authenticated user with oauth 2.0. And when I have access token, I am trying with this code.
if ($client->getAccessToken()) {
$snippet = new Google_VideoSnippet();
$snippet -> setTitle = "Demo title";
$snippet -> setDescriptio = "Demo descrition";
$snippet -> setTags = array("tag1","tag2");
$snippet -> setMimeType = 'video/quicktime';
$video = new Google_Video();
$video -> setSnippet($snippet);
// Not sure what to do now....
$_SESSION['access_token'] = $client->getAccessToken();
}
From the docs,
I need to supply a part parameter
The part parameter serves two purposes in this operation. It identifies the properties that the write operation will set as well as the properties that the API response will include.
The part names that you can include in the parameter value are snippet, contentDetails, player, statistics, status, and topicDetails. However, not all of those parts contain properties that can be set when setting or updating a video's metadata. For example, the statistics object encapsulates statistics that YouTube calculates for a video and does not contain values that you can set or modify. If the parameter value specifies a part that does not contain mutable values, that part will still be included in the API response.
But it lacks documentation except a python example which I am not able to understand.
(the example is at the bottom of the link , I provided)
Please, dont give example/links to zend library, it uses auth-sub which I dont want.
I want to use oauth 2.0.
The code to upload a video looks like this.
$youtubeService->videos->insert($part, Google_Video $postBody, $optParams = array());
'part' is what you want the request to return. In this case that could just be status, which return information about the status of the upload.
The release of the Google PHP client library might be old, so you'll want to checkout the source at https://code.google.com/p/google-api-php-client/

Categories