Twilio Pass conference name using session codeigniter - php

Hi guys I am having a problem with twilio currently setting an assignment to a worker and i need to pass the worker to the conference. My problem is that i cant use session to retrieve the id in the session and the id will be the conference name of the conference to be able to have a unique conference name for a worker.
This is my callback in twilio
This is my code to get the task. And the id will be passed on forward_queue_conference.
public function assignment()
{
id = $this->session->userdata('user_id');
$TaskAttributes = $_POST['TaskAttributes'];
$json = json_decode($TaskAttributes, true);
$this->Mytwilio->SetAssignment($json['from'], AFTERTALK, HTTP_BASE_URL."agent/call_controls/forward_queue_conference?data=".$id);
}
This is my code on forward_queue_conference to retrieve the pass data
public function forward_queue_conference()
{
roomName = $_GET['data'];
$this->Mytwilio->CallerToQueue($roomName);
}
MyTwilio is a library that i made for twilio functions.
function CallerToQueue($roomName)
{
$response = new Services_Twilio_Twiml;
$dial = $response->dial();
$dial->conference($roomName, array(
'startConferenceOnEnter' => 'true',
'endConferenceOnExit' => 'true',
'muted' => 'false',
'record' => 'record-from-start',
'waitUrl' => 'http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient',
));
print $response;
}
And this is my whole process my problem is that i cant get the session data to become the conference room.

Twilio developer evangelist here.
When Twilio makes a callback to a URL on your website, it does not share the same session as your logged in user. It is therefore impossible to get the current user ID from the session.
However, your workers have an ID in the Twilio system. And that ID is sent as part of the parameters for the webhook. So, I recommend using the WorkerSid as the conference room instead of your own ID. Or alternatively, you could map between your worker ID and the user ID in your system.

Related

Use token from "sign in with apple" to query apple music api

Context
I am trying to make webservice that fetches the name and email from an users Apple account and place a Song or Artist in his library.
For adding a Song to the library I found this apple-music-api. library. To make requests on behalf of a user you need to request a user token with Apple MusicKit JS library.
For fetching the name and email of the user I use this oauth2 client that uses the signin with Apple functionality.
Problem
A Using the apple music kit... I can not query any user profile data. At least I cannot seem to find an example nor any documentation of this. Is there a possibility to get the user email and name using this route?
B Using the Sign in with Apple oauth flow I receive an access token which contains the name and email. But I cannot use the token to query the apple music api. It seems their scopes are limited to the name and email...and no scope for the music api or related seems to exist. Is there a possibility to get an user token that can be used on the music api?
C Are there any other possibilities to accomplish this without requiring the user to sign in twice on apple (once for the email and once for pushing the Song to his library)
What I tried for option B
// $leeway is needed for clock skew
Firebase\JWT\JWT::$leeway = 60;
$provider = new League\OAuth2\Client\Provider\Apple([
'clientId' => 'com.myapp.www',
'teamId' => 'team.id', // 1A234BFK46 https://developer.apple.com/account/#/membership/ (Team ID)
'keyFileId' => 'key.id', // 1ABC6523AA https://developer.apple.com/account/resources/authkeys/list (Key ID)
'keyFilePath' => dirname(__FILE__) . '/AuthKey_key.id.p8', // __DIR__ . '/AuthKey_1ABC6523AA.p8' -> Download key above
'redirectUri' => PLUGIN_URL . 'callback-apple-music.php',
]);
if (isset($_POST['code'])) {
if (empty($_POST['state']) || !isset($_COOKIE['apple-oauth2state']) || ($_POST['state'] !== $_SESSION['apple-oauth2state'])) {
unset($_COOKIE['apple-oauth2state']);
exit('Invalid state');
} else {
try {
// Try to get an access token (using the authorization code grant) via signin_with_apple
/** #var AppleAccessToken $token */
$token = $provider->getAccessToken('authorization_code', [
'code' => $_POST['code']
]);
$access_token = $token->getToken();
// create an client for api.music.apple
$tokenGenerator = new PouleR\AppleMusicAPI\AppleMusicAPITokenGenerator();
$jwtToken = $tokenGenerator->generateDeveloperToken(
'team.id',
'key.id',
dirname(__FILE__) .'/AuthKey_key.id.p8'
);
// create a developer token again
$curl = new \Symfony\Component\HttpClient\CurlHttpClient();
$client = new PouleR\AppleMusicAPI\APIClient($curl);
$client->setDeveloperToken($jwtToken);
$api = new PouleR\AppleMusicAPI\AppleMusicAPI($client);
$api->setMusicUserToken($access_token);
// This endpoint needs authorisation
$result = $api->getAllLibraryPlaylists(); //https://api.music.apple.com/v1/me/library/playlists?offset=0&limit=25
echo '<pre>';
print_r($result);
echo '</pre>';
// wp_redirect($redirect_url);
exit;
} catch (Exception $e) {
echo '<pre>';
print_r($e);
echo '</pre>';
}
}
}
The problem with the question is that these are three questions - and not telling which client.
Most commonly "login with" is only good for creating local accounts without much typing.
And it is quite likely intentional, that the oAuth2 scope is extremely limited for this purpose.
And I've looked it up ...one needs a "Music User Token":
https://developer.apple.com/documentation/applemusicapi/getting_keys_and_creating_tokens
And this token needs to be passed as HTTP header: 'Music-User-Token: [music user token]'.
Which means, that the user token may either originate from an iOS device (you'd need to expose eg. a REST API, so that it can be posted and then used by PHP as HTTP header, on the server-side): https://developer.apple.com/documentation/storekit/skcloudservicecontroller/2909079-requestusertoken (this only requires a login to your own API).
When running Apple MusicKit JS on the cient-side (browser), two logins may not be evitable:
https://developer.apple.com/documentation/musickitjs/musickit/musickitinstance/2992701-authorize
It makes no sense to use both of these flows within the same method(which also ignores the principle of single responsibility).

Laravel Echo private channel with Laravel echo server | Redis

Scenario: A customer can refer a client to another customer.
Each referral needs to be stored in a DB table row. The customer receiving the referral should see a notification for the event.
Create a new referral and dispatch the event:
$totalRefers = [];
foreach ($array as $to) {
$refer = new ClientReferral;
$refer->user_id = $user_id;
$refer->by = $by;
$refer->to = $to;
$refer->save();
array_push($totalRefers, $refer);
ReferralSent::dispatch($refer); // Here is the Event
}
return response()->json([
'status' => 'Success',
'message' => 'Client referred successfully to selected professionals.',
'data' => $totalRefers
], 200);
The event broadcastOn() method:
public function broadcastOn() {
return new PrivateChannel('referral.' . $this->referral->id);
}
The channel:
Broadcast::channel('referral.{id}', function ($user, $id) {
// let's say it's true for the time being
return true;
});
And the request is an Ajax POST so in the success callback:
console.log('referral created');
res.data.forEach(function(entry) {
// window.custom.userId is the authenticated user ID:
if (entry.refer_to == window.custom.userId) {
window.Echo.private('referral.' + entry.id).listen('ReferralSent', ({data}) => {
console.log('You have received a new referral');
});
}
});
Now the issue with the current code is that the receiver cannot subscribe to this channel because the event is created dynamically, and I cannot make the channel name static because the referral came in at run time.
How can a receiver subscribe and listen to dynamic events?
With this logic, I want to get that specific referral and its data to show it in the notification tray in HTML.
How can I achieve this?
The event shown in the question broadcasts to a channel for that specific referral entity only. However, the receiver that subscribes to this channel should receive events for all referral entities referred to them.
Instead of creating the channel context for the referral entity itself, publish to a channel designated for the user that receives the referral. I'm guessing that $referral->to contains that user's ID:
public function broadcastOn()
{
return new PrivateChannel('referral.' . $this->referral->to);
}
Update the channel to authorize the current user based on the ID of the user that receives the referral:
Broadcast::channel('referral.{refereeId}', function ($user, $refereeId) {
return $user->id == $refereeId;
});
And, on the client-side, listen on the same channel:
window.Echo.private('referral.' + window.custom.userId)
.listen(e => console.log(e.referral));
Because we're not listening for a specific referral ID anymore, we can initialize the Echo subscriber during the page load instead of in the AJAX response callback.
Broadcast events are designed to be useful for real-time actions outside the normal request/response cycle (including AJAX requests). In the case of this question, we want to start Echo listeners for every customer when the page loads—not after a specific request—so that they can receive notifications any time another customer refers a client to them.
The flow looks like this:
Customer 1 and Customer 2 both open the app, which starts Echo on the client-side.
Customer 1 creates a referral for Customer 2.
Customer 1's browser sends an AJAX request to save the referral.
Laravel publishes the event to Customer 2's channel.
Customer 2's browser receives the event through Echo, which is listening on that channel.
The code you wrote to handle that event creates a notification in Customer 2's browser.

Notification callback is not being received

Im using this laravel package "https://github.com/kanazaca/easypay" to create a MB reference using the Easypay API.
I have this method to create the reference:
public function generateReference()
{
$amount = Session::get('total');
$payment_info = [
't_value' => $amount,
'o_obs' => '',
't_key' => 1
];
$easypay = new EasyPay($payment_info);
$reference = $easypay->createReference();
Session::put('entity', $reference['ep_entity']);
Session::put('reference', $reference['ep_reference']);
Session::put('value', $reference['ep_value']);
}
And it works fine with this code I get some reference codes which can be payed using MB or credit-card.
Then, when a payment is made, easypay will call a "Notification URL".
that we should configure on easypay's backoffice under "URL Configurations".
Because when the easypay service receives the payment they will call the URL that we provided. So I defined a url in the easypay´s backoffice and created a route in the project:
Route::get('/easypay/notification-callback', [
'uses' => 'PaymentController#receiveNotifications',
'as' =>'mb.notifications'
]);
In the api backoffice there is a button that simulates a payment, after this button is clicked nothing happens and if I manually access "http://....ngrok.io/easypay/notification-callback" it appears an empty array:
[]
But in the documentation (https://docs.easypay.pt/workflow/payment-notification) says that when Easypay calls this endpoint, it comes with a few parameters: "ep_cin", "ep_user" and "ep_doc" that will be necessary in the process. So it should not appear an empty array.
Do you know what can be the issue? Im a beginner uing API´s so Im not understnading what the issue can be.
PaymentController receiveNotifications() method:
public function receiveNotifications(Request $request)
{
dd($request->all());
//$easypay = new EasyPay($payment_info);
//$xml = $easypay->processPaymentInfo();
//return \Response::make($xml, '200')->header('Content-Type', 'text/xml'); //must return in text/xml for easypay
}
receiveNotifications() method with log:
public function receiveNotifications(Request $request)
{
//dd($request->all());
Log::info('Showing info: ' .var_export($request->all(),true));
$payment_info = [
'ep_cin' => $request->ep_cin,
'ep_user' => $request->ep_user,
'ep_doc' => $request->ep_doc
];
Log::info('Showing info: ' .var_export($payment_info,true));
//dd($payment_info);
$easypay = new EasyPay($payment_info);
$xml = $easypay->processPaymentInfo();
return \Response::make($xml, '200')->header('Content-Type', 'text/xml'); //must return in text/xml for easypay
}
The session is saved in the session file of the user that visits your website that initiates the payment.
The receiveNotifications would call data from the session file that belongs to the payment gateway server if you were doing anything there. The data isn't matched because the two don't know about each other.
Also, you might not have a Session::save() somewhere in your request handling which writes the session data to file.
Store the reference in a database. Create a model for storing this data, so you can then query that model for the correct reference ID to verify/do stuff.
When the request comes back from the payment gateway use the variables ep_cin, ep_user and ep_doc to get the data from the model.
When you manually request that data you are requesting it with a GET request, which doesn't send the above data along.
The request made by the payment provider will get the result of the DD but that's logged nowhere, so nothing happens.
Log your data for requests triggered by remote api's to see what happens.

Twilio get conference SID when connecting to devices

Is there a way to get the conference SID when connecting the calls in PHP?
$twilio->account->calls->create(
$from,
to,
$twimlURL
);
Can't I get the conference ID after this call action?
Or maybe set the conference SID on the Twiml. Is this possible?
Twilio developer evangelist here.
When you create a call through the REST API like that, it is not yet associated with a conference. I assume the $twimlURL that you send returns <Dial><Conference>some conference name</Conference></Dial> and that is the time that the call is associated with the conference.
You can get the Conference SID by listing conferences using the REST API and filtering by the FriendlyName (the name you used in the TwiML) and by the status in-progress. Like this:
use Twilio\Rest\Client;
$sid = "your_account_sid";
$token = "your_auth_token";
$client = new Client($sid, $token);
$conferences = $client->conferences->read(
array("status" => "in-progress", "friendlyName" => "MyRoom")
);
foreach ($conferences as $conference) {
echo $conference->sid;
}
Let me know if that helps at all.
In Case someone is looking for a C# version
TwilioClient.Init(credentials.AccountSID, credentials.AuthToken);
ResourceSet<ConferenceResource> conferencesResources = await ConferenceResource.ReadAsync(status: "in-progress", friendlyName: "<your conference room name>");
ConferenceResource conference = conferencesResources.FirstOrDefault();
Console.WriteLine(conference.Sid);

Display Facebook ADs Statistics on webpage using Facebook Ads API in PHP

Does anyone know how to fetch all facebook ads statistics and display on webpage using Facebook Ads Api-PHP SDK. I am using this API and I am getting campaign details like name of campaign, id, status. but not able to get impressions,clicks, spent.
What I am doing let me share with you:
1) I am getting access token by authorizing user
2) After getting access token, I am using below code
$account = new AdAccount('act_XXXXXXXXXXXXXXX');
$account->read();
$fields = array(
AdCampaignFields::ID,
AdCampaignFields::NAME,
AdCampaignFields::OBJECTIVE,
);
$params = array(AdCampaignFields::STATUS => array(AdCampaign::STATUS_ACTIVE,AdCampaign::STATUS_PAUSED,),);
$campaigns = $account->getAdCampaigns($fields, $params);
/* Added By Jigar */
$campaign = new AdCampaign('XXXXXXXXXXXXXXXX');
$compainDetails = $campaign->read($fields);
3) then printing the array
echo "<pre>";
print_r($compainDetails);
exit;
If anyone know any suggestion in above code, please share. All code is in PHP. Dose anyone have any tutorial that fetch all above required data then share it
You could try to use the facebook insights api instead of $campaign->read. Here's an example:
https://developers.facebook.com/docs/marketing-api/insights/v2.5#create-async-jobs
What you have to do to get impressions, click and spent is to add these fields to the $fields param. In your case, the complete code should look like the following:
use FacebookAds\Object\Campaign;
use FacebookAds\Object\Values\InsightsLevels;
use FacebookAds\Object\Values\InsightsFields;
$campaign = new Campaign();
$fields = array(
InsightsFields::IMPRESSIONS,
InsightsFields::UNIQUE_CLICKS,
InsightsFields::CALL_TO_ACTION_CLICKS,
InsightsFields::INLINE_LINK_CLICKS,
InsightsFields::SOCIAL_CLICKS,
InsightsFields::UNIQUE_SOCIAL_CLICKS,
InsightsFields::SPEND,
);
$params = array(
'level' => InsightsLevels::CAMPAIGN,
);
$async_job = $campaign->getInsightsAsync($fields, $params);
$async_job->read();
I don't know what exactly the "click" param means for you, but if you take a look at all these click params, I'm sure you'll find it or you'll know how to calculate it.
For a complete list of fields available on insights objects, have a look at: https://github.com/facebook/facebook-php-ads-sdk/blob/master/src/FacebookAds/Object/Fields/InsightsFields.php
Hope that helps.
Regards, Benjamin

Categories