问题描述
public function mobileCallTwimlResponse(Request $request): VoiceResponse
{
$from = $request->From ?? config('services.twilio.number');
$to = $request->To;
$dial = $this->twimlResponse->dial(null,['callerId' => $from]);
if (!$to) {
$this->twimlResponse->say(' "Congratulations! You have made your first call! Good bye."');
} elseif (is_string($to)) {
$dial->number($to);
} else {
$dial->client($from);
}
return $this->twimlResponse;
}
public function receiveCall(Request $request)
{
$account_sid = config('services.twilio.accountSid');
$auth_token = config('services.twilio.authToken');
$twilio_client = new Client($account_sid,$auth_token);
$from = $request->From ?? config('services.twilio.number');
$to = $request->To;
$call = null;
$url = route('mobile_receive_call');
if (!$to) {
$call = $twilio_client->calls->create("client:customer",$from,array(
"url" => $url,));
} elseif (is_numeric($to)) {
$call = $twilio_client->calls->create($to,));
} else {
$to = "client:" . $to;
$call = $twilio_client->calls->create($to,));
}
return $call->sid;
}
public function incomingCall()
{
return $this->twimlResponse->say('incoming call responding');
}
我有上面的代码来接听我的移动应用程序的来电,但我无法弄清楚 twilio 如何将来电通知发送到我的移动应用程序以及用户通过哪个移动应用程序响应呼叫。我在令牌创建 API 中设置了推送凭据
private $accesstoken;
public function __construct(Accesstoken $accesstoken)
{
$this->accesstoken = $accesstoken;
}
public function token($applicationSid,$identity = 'customer',$page_url = null): string
{
$forPage = $page_url;
/*if ($forPage === route('dashboard',[],false)) {
$this->accesstoken->setIdentity('support_agent');
} else {
$this->accesstoken->setIdentity('customer');
}*/
$this->accesstoken->setIdentity($identity);
// Create Voice grant
$voiceGrant = new VoiceGrant();
$voiceGrant->setoutgoingApplicationSid($applicationSid);
// Optional: add to allow incoming calls
$voiceGrant->setIncomingallow(true);
$voiceGrant->setPushCredentialSid(config('services.twilio.push_credential_sid'));
// Add grant to token
$this->accesstoken->addGrant($voiceGrant);
// render token to string
return $this->accesstoken->toJWT();
}
但是 FCM 如何触发 Twilio 通知?它将自行处理,或者他们是否需要在调用传入呼叫网络挂钩的服务器端编写代码,并通过该代码将传入呼叫通知发送到移动应用程序?
解决方法
Android/Ios 应用的令牌:
public function __construct(VoiceCallTokenService $callTokenService)
{
$this->callTokenService = $callTokenService;
}
public function tokenForMobile(): JsonResponse
{
$user = auth()->user();
$identity = TwilioHelper::substrPhoneNumber($user->twilio_number);
$this->callTokenService->setPushCredentialSid(config('services.twilio.ios_push_credential_sid'));
$token = $this->callTokenService->token(config('services.twilio.applicationSidForMobile'),$identity);
return response()->json(['voice_call_token' => $token]);
}
Helper SubStrPhoneNumber 方法:
public static function substrPhoneNumber($phone)
{
if ($phone[0] === "+") {
$phone = substr($phone,1);
}
return $phone;
}
In 使用 $to 作为调用标识:
$to = $request->To; //+923222333322
但是这个数字包含+号,通过排除+号它开始对我有用。即
$to = "923222333322"