想要通过twilioapi向用户发送类似WhatsApp的确认调用,该调用将包含随机确认代码


want to send whats app like confirmation call to user through twilio api which will contain random confirmation code

我正在使用twilio php库向用户发送短信和语音消息。我可以在这里提供的帮助下向用户发送消息'https://twilio-php.readthedocs.org/en/latest/'在"发送短信"部分,但无法理解如何发送确认电话,因为在我的情况下,我正在向用户发送验证码。

例如,我想发送语音,说这个代码是"123456"。但这个代码在任何时候都是随机的。你能帮我一下吗。

我已经购买了可以发送短信、彩信和语音的计划和电话号码。

这里是Twilio开发人员的传道者。

与其使用Twilio的普通SMS API来做这件事,我可以推荐你看看Authy吗?Authy是Twilio的一部分,提供电话验证和双因素身份验证服务。

Twilio网站上有一个很棒的教程,介绍如何使用PHP和Authy在您的网站上实现帐户验证。我建议你通读一下,看看它是否符合你的需求。

如果这有帮助,请告诉我。

三个小时后,我找到了解决方案。我希望这将帮助其他人,并将节省他们宝贵的时间。以下是完整的流程。

首先,你需要创建TWIML应用程序,然后将该应用程序分配给你的电话号码。现在创建一个函数来进行类似的twilio调用

   function make_call{
               $account_sid = ""; // Your Twilio account sid
                $auth_token = ""; // Your Twilio account token
               $twilio_number = 'your twilio number';
               $to_number = 'number of user to which you want to make call';
                $client = new Services_Twilio($account_sid,$auth_token);
                //this is the url which twilio will hit after making a call, to get xml response which the computer will speak( if you want to send addition parameters you can do this by adding query string ). We are making here get request.
                $url = "http://test.com/say_words/?user_id=$logged_user_id";
                try 
                {
                    $call = $client->account->calls->create($twilio_number, $to_number, $url, array('Method' => 'GET'));
                    // print $call;
                    // return $call;
                    // print_r($call->sid);
                    // print_r($call);
                    $error['call_id'] = $call->sid; 
                    $error['success'] = 'You will receive a call shortly on your mobile number.';
                    // die();
                } 
                catch(Exception $e)
                {
                    // $err = urlencode($e->getMessage());
                    // print_r($err);
                    // die();
                    $error['errors'] = 'Error while makin call. Please try again.'; 
                }
             }

现在,这是twilio将为xml响应调用的另一个函数。如果你不创建这个函数,那么twilio会打电话,但不会说话。所以创建一个类似的函数

 public function say_words()
{
 $newsXML = new SimpleXMLElement("<Response></Response>");
    $newsIntro = $newsXML->addChild('Say', "Your confirmation code is $formatted");
    $newsIntro->addAttribute('loop', '3');
    $newsIntro->addAttribute('voice', 'woman');
    Header('Content-type: text/xml');
    echo $newsXML->asXML();
    die();
 }
 Twilio need this kind of xml response 
 <?xml version="1.0"?>
 <Response>
    <Say loop="3" voice="woman">Your confirmation code is 7 0 2 8 4 6</Say>
 </Response>

您可以在此处查看其他xml响应https://www.twilio.com/docs/api/twiml