之前如何发送自定义消息,代理会话发送实际消息

时间:2019-07-10 06:17:59

标签: php proxy twilio

当客户回复代理服务保留号码时,代理会命中OutOfSessionCallbackUrl(如果会话未激活)。 该网址将出现在下面的我的代码中。

 public function response()
 {
    $to = $_POST['To'];
    $from = $_POST['From'];
    $from = substr($from, 2);
    $body = $_POST['Body'];
    $twilio = new Client($this->sid, $this->token);

    $response=$this->db->get_where('contact_management as cm 
    ,proxy_service as ps',
   array('mobile'=>$from,'company_mobile'=>$to,'sc.sms_template_id<>'=>0))
    ->row_array();
    $number = trim($response['country_code'].$response['mobile_number']);

    //Here I'm sending a response
        header("content-type:application/json");
        ?>
        {
        "uniqueName": "<?php echo rand();?>",
        "ttl":"64800",
        "mode": "voice-and-message",
        "participantIdentifier":"<?php echo $number;?>"
        }
        <?php
}

这将在SMS发件人和返回的号码(公司)之间建立会话,并将发件人的消息发送给公司。我想在Twilio代理向公司发送实际消息之前发送自定义消息。

谢谢。

2 个答案:

答案 0 :(得分:0)

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

当前您正在使用the out of session callback来创建会话,但是要在转发传入消息之前先发送一条消息。

为此,您将无法使用JSON进行响应来创建会话。相反,您应该create the session manually using the session API。创建会话后,您可以通过creating an interaction on the participant发送您要向其发送消息的初始消息。然后,您可以使用相同的API继续转发原始消息。最后,您仍然需要响应网络挂钩。由于您已手动处理所有消息发送,因此您可以返回一个空的TwiML <Response/>,以表示您不需要Twilio参与进一步的工作。

让我知道是否有帮助。

答案 1 :(得分:0)

这里是完整的描述。 我在代理服务中添加了保留的Twilio号,并设置了代理服务OutOfSessionCallbackUrl。当此URL到达我的代码时,魔术就发生了,

   public function response()
   {
    $to = $_POST['To'];
    $from = $_POST['From'];
    $twilio = new Client($this->sid, $this->token);
    $response=$this->db->get_where('contact_management ,proxy_service,
     array('mobile'=>$from,'company_mobile'=>$to))->row_array();

        $service_sid=$response['service_sid'];

        $session = $twilio->proxy->v1->services($service_sid)->sessions
            ->create(array("uniqueName" => rand(),"ttl"=>"64800"));
        $session_sid = $session->sid;

        $participant1 = $twilio->proxy->v1->services($service_sid)
        ->sessions($session_sid)->participants->create($_POST['From'], // identifier
             array("friendlyName" => $response['f_name'],"proxyIdentifier"=>$to));
            $from_id = $participant1->proxyIdentifier;

        $participant2 = $twilio->proxy->v1->services($service_sid)
        ->sessions($session_sid)->participants
        ->create($response['country_code'].$response['mobile_number'], // identifier
           array("friendlyName" => $response['first_name']));
        $to_id = $participant2->proxyIdentifier;
        $to_sid = $participant2->sid;


        $body   =   $response['campaign_name']."\n";
        $body   .=  $_POST['Body'];
        $message_interaction = $twilio->proxy->v1->services($service_sid)
                                     ->sessions($session_sid)
                                     ->participants($to_sid)
                                     ->messageInteractions
                                     ->create(array("body" => $body));


        header("content-type:text/xml");
        ?>
        <Response />
        <?php
     }
相关问题