Trying to connect two calls with Twilio using the first call sid to connect to the next call - twilio

I am new to Laravel 9 and usage of Twilio API.
I am trying to create a scenario where a customer_care will call the first participant and after talking with the participant it will call the second participant with whom the first participant will communicate. While calling the second participant, the first participant's call will be kept on hold and after the second participant receives the call both their call will be merged so that they are connected to the same call.
I have written two separate function one for the first call and the other for the second call . The first function is:
public function twiliocall1(Request $request) {
$returnArray = array();
$status = "Error";
$msg = "Call not started";
$validator = Validator::make($request->all(), [
'participants' => 'required|array',
'twilloNo' => 'required',
'contact_id' => 'required'
]);
if ($validator->fails()) {
return response()->json(['error' => $validator->errors()], 401);
}
$twilloNo = $request->input('twilloNo');
$participants = $request->input('participants');
$sid = "xxxxx";
$token = "yyyy";
$client = new Client($sid, $token);
if (!empty($participants)) {
foreach($participants as $participant) {
$call1 = $client->account->calls->create(
$participant,
$twilloNo,
array("url" => "https://lbwr.operative.dev/multitenant/public/files/conference.php")
);
}
$status = "Success";
$msg = "Call started";
echo($call1->sid);
}
$returnArray['status'] = $status;
$returnArray['msg'] = $msg;
return response()->json($returnArray, 200);
}
The second function is:
public function twiliocall2(Request $request) {
$returnArray = array();
$status = "Error";
$msg = "Call not started";
$validator = Validator::make($request->all(), [
'participants' => 'required|array',
'twilloNo' => 'required',
'contact_id' => 'required',
'sid' => 'required'
]);
if ($validator->fails()) {
return response()->json(['error' => $validator->errors()], 401);
}
$twilloNo = $request->input('twilloNo');
$participants = $request->input('participants');
$parent_sid = $request->input('sid');
$sid = "xxxx";
$token = "yyyy";
$client = new Client($sid, $token);
foreach($participants as $participant) {
$call = $client->account->calls->get($parent_sid);
$call->update(
$participant,
$twilloNo,
array(
"Url" => "http://demo.twilio.com/docs/voice.xml",
"Method" => "POST"));
}
$status = "Success";
$msg = "Call started";
$returnArray['status'] = $status;
$returnArray['msg'] = $msg;
return response()->json($returnArray, 200);
}
$sid and $token value is same for both the function. I am trying to call the first function using the following json array:
{
"participants":["12345"],
"twilloNo":"68564",
"contact_id":"1234"
}
And on hitting I am getting the sid of this call which I am passing as a json array element to the function twiliocall2() as:
{
"participants":["+7777"],
"twilloNo":"111122",
"contact_id":"1023",
"sid":"fdthgfjkhbklnj"
}
All the values are changed.
The problem arises when I am calling the second function with this Json array as it throws an error:
Twilio\Exceptions\TwilioException: Unknown subresource get in file /opt/lampp/htdocs/multitenant/vendor/twilio/sdk/src/Twilio/Rest/Api/V2010/Account/CallList.php
Kindly suggest if possible where am I going wrong. I am not getting any idea how to implement this scenario any further.

Related

Trying to setup an conference call using Twilio API in laravel 9 but facing issue while creating the conference instance

The code in the Controller is as follows which parse an JSON and creates a Conference object where the error is occouring:
public function twilioConferenceCall(Request $request) {
$returnArray = array();
$status = "Error";
$msg = "Call not started";
$validator = Validator::make($request->all(), [
'participants' => 'required|array',
'fromNo' => 'required|numeric',
'twilloNo' => 'required',
'contact_id' => 'required'
]);
if ($validator->fails()) {
return response()->json(['error' => $validator->errors()], 401);
}
$fromNo = $request->input('fromNo');
$twilloNo = $request->input('twilloNo');
$sid = "someid";
$token = "sometoken";
$client = new Client($sid, $token);
$conference = $client->conferences->create([
'friendlyName' => 'Test Conference Call'
]);
$conference_sid = $conference->sid;
if ($fromNo != "" && !empty($request->participants)) {
foreach($request->participants as $participant) {
$concall = $client->account->calls->create(
$participant,
$twilloNo,
array(
"url" => "http://twimlets.com/conference?Name=Test%20Conference%20Call&ConferenceSid=".$conference_sid."&Moderators%5B0%5D=".$fromNo
)
);
$status = "Success";
$msg = "Started call to " . $participant;
}
}
$returnArray['status'] = $status;
$returnArray['msg'] = $msg;
return response()->json($returnArray, 200);
}
The JSON which am I sending is as follows:
{
"participants":["+1234","+9999"],
"fromNo":"+5678",
"twilloNo":"119988",
"contact_id":"1057"
}
The error which it throws is:
Error: Call to undefined method Twilio\Rest\Api\V2010\Account\ConferenceList::create()
I have included the following headers in my file
use Twilio\autoload;
use Twilio\Rest\Client;
Kindly suggest where am I doing wrong.

Youtube Data API v3 - problems with quota usage

In my application I want to use quota that is user based (requests/second/user). I'am sending random (for test queries) quotaUser with all of my requests. But, in the console, I see an increase in the overall use of the quota (project console view). The transfer of the quotaUser parameter does not affect anything. Perhaps I did not correctly understand the documentation on the use of quotas. Is it possible to execute more than 1,000,000 requests per day from multiple application users?
Here is my code:
$client = new Google_Client();
$client->setAuthConfigFile(SITE_PATH . '/vendor/access_data.json');
$client->setScopes('https://www.googleapis.com/auth/youtube');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . '/test', FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);
$youtube = new Google_Service_YouTube($client);
if (isset($_GET['code']))
{
$userId = rand(1000, 100000);
$client->authenticate($_GET['code']);
$token = $client->getAccessToken();
$client->setAccessToken($token);
$channelsResponse = $youtube->channels->listChannels('snippet,contentDetails,statistics,status,brandingSettings', array(
'mine' => 'true',
'quotaUser' => $userId
));
$videoResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
'playlistId' => $channelsResponse['items'][0]['contentDetails']['relatedPlaylists']['uploads'],
'maxResults'=> '50',
'quotaUser' => $userId
));
$videos = [];
foreach($videoResponse['items'] as $video)
{
$videos[] = $video['snippet']['resourceId']['videoId'];
}
while(isset($videoResponse['nextPageToken']))
{
$videoResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
'playlistId' => $channelsResponse['items'][0]['contentDetails']['relatedPlaylists']['uploads'],
'maxResults'=> '50',
'pageToken' => $videoResponse['nextPageToken']
));
foreach($videoResponse['items'] as $video)
{
$videos[] = $video['snippet']['resourceId']['videoId'];
}
}
print_r($videos);
}

laravel fcm push notification with brozot/Laravel-FCM not working on ios but working fine with android

Even ios can get notification from fcm console.
Controller function:
public function push(Request $request)
{
$validator = Validator::make($request->all(), [
'title' = > 'required',
'body' = > 'required',
'token' = > 'required',
'type' = > 'required',
'id' = > 'required',
]);
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
$title = $request['title'];
$body = $request['body'];
$type = $request['type'];
$id = $request['id'];
$dataarray = array(
"id" = >$id,
"type" = >$type,
'title' = >$title,
'body' = >$body,
'image' = >'321451_v2.jpg',
);
$token = $request['token'];
return $push = Push::sendpush($title, $body, $dataarray, $token);
}
push model function :
public static function sendpush($title, $body, $dataarray, $token)
{
$optionBuiler = new OptionsBuilder();
$optionBuiler->setTimeToLive(60 * 20);
$notificationBuilder = new PayloadNotificationBuilder($title);
$notificationBuilder->setBody($body)
->setSound('');
$dataBuilder = new PayloadDataBuilder();
$dataBuilder->addData($dataarray);
$option = $optionBuiler->build();
$notification = $notificationBuilder->build();
$data = $dataBuilder->build();
$token = $token;
$downstreamResponse = FCM::sendTo($token, $option, $notification, $data);
return new JsonResponse(array('status' = >'1', 'sucess' = >$downstreamResponse->numberSuccess(), 'fail' = > $downstreamResponse->numberFailure(), 'msg' = >$downstreamResponse->tokensWithError()), 200);
}
Response:
{
"status": "1",
"sucess" : 0,
"fail" : 1,
"msg" : []
}
If you are trying using ios simulator it will not work. You need to use some tools like testflight or real device to test.
can we check push notification in simulator?
You have to set up a certificate for Ios follow this link to see more details
Configuring APNs with FCM
or see this Set up a Firebase Cloud Messaging client app on iOS

PHP Codeigniter url routing: token for registering process in the url

Right after the registration process there is a link shown to set password:
http://website.de/main/complete/token/MTkyYTBiY2EyNWE2ZDYwOTdhN2U4NjJkMjZmYzYyNTA
The problem is I can't display this page to complete the registration process.
Does anyone have a clue why? I'm already looking since 2 hours for it...
Controller function:
public function complete()
{
$token = base64_decode($this->uri->segment(4));
$cleanToken = $this->security->xss_clean($token);
$user_info = $this->user_model->isTokenValid($cleanToken); //either false or array();
if(!$user_info){
$this->session->set_flashdata('flash_message', 'Token is invalid or expired');
redirect(base_url().'login');
}
$data = array(
'firstName'=> $user_info->first_name,
'email'=>$user_info->email,
'user_id'=>$user_info->id,
'token'=>$this->base64url_encode($token)
);
$this->form_validation->set_rules('password', 'Password', 'required|min_length[5]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|matches[password]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('header');
$this->load->view('complete', $data);
$this->load->view('footer');
}else{
$this->load->library('password');
$post = $this->input->post(NULL, TRUE);
$cleanPost = $this->security->xss_clean($post);
$hashed = $this->password->create_hash($cleanPost['password']);
$cleanPost['password'] = $hashed;
unset($cleanPost['passconf']);
$userInfo = $this->user_model->updateUserInfo($cleanPost);
if(!$userInfo){
$this->session->set_flashdata('flash_message', 'There was a problem updating your record');
redirect(base_url().'login');
}
unset($userInfo->password);
foreach($userInfo as $key=>$val){
$this->session->set_userdata($key, $val);
}
redirect(base_url());
}
}
public function register()
{
$this->form_validation->set_rules('firstname', 'First Name', 'required');
$this->form_validation->set_rules('lastname', 'Last Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run() == FALSE) {
$this->load->view('header');
$this->load->view('register');
$this->load->view('footer');
}else{
if($this->user_model->isDuplicate($this->input->post('email'))){
$this->session->set_flashdata('flash_message', 'User email already exists');
redirect(base_url().'login');
}else{
$clean = $this->security->xss_clean($this->input->post(NULL, TRUE));
$id = $this->user_model->insertUser($clean);
$token = $this->user_model->insertToken($id);
$qstring = $this->base64url_encode($token);
$url = base_url() . 'complete/token/' . $qstring;
$link = '' . $url . '';
$message = '';
$message .= '<strong>You have signed up with our website</strong><br>';
$message .= '<strong>Please click:</strong> ' . $link;
echo $message; //send this in email
exit;
};
}
}
Model functions:
public function insertToken($user_id)
{
$token = substr(sha1(rand()), 0, 30);
$date = date('Y-m-d');
$string = array(
'token'=> $token,
'user_id'=>$user_id,
'created'=>$date
);
$query = $this->db->insert_string('tokens',$string);
$this->db->query($query);
return $token . $user_id;
}
public function isTokenValid($token)
{
$tkn = substr($token,0,30);
$uid = substr($token,30);
$q = $this->db->get_where('tokens', array(
'tokens.token' => $tkn,
'tokens.user_id' => $uid), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
$created = $row->created;
$createdTS = strtotime($created);
$today = date('Y-m-d');
$todayTS = strtotime($today);
if($createdTS != $todayTS){
return false;
}
$user_info = $this->getUserInfo($row->user_id);
return $user_info;
}else{
return false;
}
}
what you want to do is, after user successfully registered, show him/her a url to complete the registration right?
As far as I understood the problem, you can do like this,
in your controller,
$message = '';
$message .= '<strong>You have signed up with our website</strong><br>';
$message .= '<strong>Please click:</strong> ' . $link;
$data = array(
'message' => $message
);
$this->load->view('header');
$this->load->view('complete', $data);
$this->load->view('footer');
And create a view called complete.php
<html>
<head>
<title></title>
</head>
<body>
<h1><?php echo $message;?></h1>
</body>
</html>
Adding Dynamic Data to the View - https://www.codeigniter.com/userguide2/general/views.html

CallStatus URL is not being called

I am using the following code to initiate calls. If the call is not answered, I want to capture this event (unanswered call) and call an alternative phone number. Currently with the below code, if a phone is not answered once, it is calling 3 times and then giving up without calling the StatusCallback URL at all. What am I doing wrong? (I can call the URL from browser with parameters.)
Thx a lot.
// Piece of PHP code making the call
$this->info('Calling providers:');
$account_sid = ...
$auth_token = ...
$client = new \Services_Twilio($account_sid, $auth_token);
try {
$this->info ('Calling phone... ');
// Make a call
$call = $client->account->calls->create('+448008021203', '+'.$phone_to_call, 'http://xyz/order_msg.html', array(
'Method' => 'GET',
"StatusCallback" => "http://xyz/phone_events?alt_phone=".$alternate_phone,
"StatusCallbackMethod" => "GET",
"StatusCallbackEvent" => array("completed"),
'Record' => 'false',
));
$this->info('Called with :' . $call);
}
}catch (\Exception $e) {
$this->error('Exception :'.$e->getMessage());
}
// StatusCallback URL = http://xyz/phone_events PHP-Laravel code
$status =Input::get('CallStatus');
$alternate_phone =Input::get('alt_phone');
if (! empty($alternate_phone) && ! empty($status)) {
if ($status != "completed" || $status != "queued") {
/* start the next call */
$account_sid = ...;
$auth_token = ...;
$client = new \Services_Twilio($account_sid, $auth_token);
try {
$client->account->calls->create('+448008021203', '+'.$alt_phone, 'http://xyz/order_msg.html', array(
'Method' => 'GET',
"StatusCallback" => "http://xyz/phone_events,
"StatusCallbackMethod" => "GET",
"StatusCallbackEvent" => array("completed"),
'Record' => 'false',
));
} catch (\Exception $e) {
$log->error('Phone Events Error: ' . $e);
}
}
}

Resources