简体   繁体   English

Laravel Twilio 将用户回复输入存储到数据库以响应发送短信订单 ID

[英]Laravel Twilio Store User Reply Input to Database in Response to Send SMS Order ID

I need help in Twilio getting reply responses and storing them in the database in response to sending SMS after user input in response to the message.我在 Twilio 中需要帮助获取回复响应并将它们存储在数据库中以响应用户输入响应消息后发送 SMS。 I am sending an SMS to the user about his/her order schedule and asking him/her to confirm delivery by inputting Ok and when he/she confirm, I am getting his response successfully.我正在向用户发送一条关于他/她的订单时间表的短信,并要求他/她通过输入确定来确认交货,当他/她确认时,我成功收到了他的回复。

The question or the issue I am facing is that I want to know that when he/she confirm the delivery, I want to update the database record in response to the delivery SMS we sent to him/her earlier.我面临的问题或问题是我想知道当他/她确认交付时,我想更新数据库记录以响应我们之前发送给他/她的交付短信。 My issue will be resolved if I am able to get order_id from the first message and send it in replytoSMS function.如果我能够从第一条消息中获取 order_id 并将其回复到 SMS function,我的问题就会得到解决。

My Code to send SMS and webhook (replytoSMS) are given below.下面给出了我发送短信和 webhook (replytoSMS) 的代码。

<?php

 public function initiateSMS(Request $request) {
  try {
  foreach ( $request->get('items') as $item ) {
    $order_id = $item['order_id'];
    $phone    = $item['phone_number'];
    $order     = Orders::where('id', $order_id)->first();

    $phone_number = $this->client->lookups->v1->phoneNumbers($phone)->fetch();

  if($phone_number) {
   
      $template_value = $order->message; 

      $sms = $this->client->messages->create($phone, [
        'from' => $this->from, 
        'body' => $template_value,
        "method" => 'POST',
        "statusCallbackMethod" => 'POST',
        "statusCallback" => 'https://example.com/simply/public/api/notification/statusMessageBack?order_id='.$order_id.'',
        ]);

       // print($sms->sid);
  }
} // foreach loop end
  if($sms){
    return Response::json(['success' => 'sms initiated successfully!']);
  }
} catch (Exception $e) {
  return Response::json(['Error' => $e->getMessage()]);
} catch (RestException $rest) {
  return Response::json(['Error' => $rest->getMessage()]);
}
}

function statusMessageBack(){
 header('Content-type: text/xml');
 header('Cache-Control: no-cache');
 $response = new MessagingResponse();
 $order_id = $_REQUEST['order_id'];
 $user_phone = $_REQUEST['To'];
 $MessageSid = (!empty($_REQUEST['MessageSid'])) ? $_REQUEST['MessageSid'] : '';
 $MessageStatus = (!empty($_REQUEST['MessageStatus'])) ? $_REQUEST['MessageStatus'] :              '';
  if($MessageStatus && $MessageStatus == "delivered"){

  $notification = Notification::create([                            
  "response_code" => $MessageSid,
  "type" => $category,
  "table_ref"  => "orders",
  "table_ref_pk"  => $order_id, 
  "response_status" => "",
  "medium" => $user_phone,
  "status" => $MessageStatus,
  "sender_id" => $sender_id
 ]);
  
}
 print $response; exit;
}

 public function replyToSMS(){
 header('Content-type: text/xml');
 header('Cache-Control: no-cache');
 $response = new MessagingResponse();
 $MessageSid = (!empty($_REQUEST['MessageSid'])) ? $_REQUEST['MessageSid'] : '';
 $MessageStatus = (!empty($_REQUEST['MessageStatus'])) ? $_REQUEST['MessageStatus'] : '';

$body = $_REQUEST['Body'];
$order_id = $_REQUEST['order_id'];
$from_phone = $_REQUEST['From'];

if (strtolower($body) == 'ok' || strtolower($body) == 'yes' || strtolower($body) == 'confirm') {
  $response->message('Your delivery has been confirmed. Thank you', [
    'callbackUrl' => "https://example.com/api/notification/reply_status?order_id='.$order_id.'",
    'callbackMethod' => "POST"
    ]);
    $notification = Notification::where('order_id', $order_id)->update(array("response_status" => "confirmed"));
 } else {
   $response->message('Sorry');
   $notification = Notification::where('order_id', $order_id)->update(array("response_status" => "call store"));
 }
  print $response;

 }

function reply_status(){
header('Content-type: text/xml');
header('Cache-Control: no-cache');
$response = new MessagingResponse();
echo $order_id = $_REQUEST['order_id'];
$user_phone = $_REQUEST['To'];
$MessageSid = (!empty($_REQUEST['MessageSid'])) ? $_REQUEST['MessageSid'] : '';
$MessageStatus = (!empty($_REQUEST['MessageStatus'])) ? $_REQUEST['MessageStatus'] : '';
if($MessageStatus && $MessageStatus == "delivered"){

}
  print $response; exit;
}

The SMS protocol does not any concept of additional metadata with SMS messages, nor does it have the concept of replying to a specific message (you can test this by opening your phone's SMS application and trying to respond to any message from someone that wasn't the last one you received). SMS 协议没有任何关于 SMS 消息的附加元数据的概念,也没有回复特定消息的概念(您可以通过打开手机的 SMS 应用程序并尝试回复来自不是某人的任何消息来测试这一点你收到的最后一个)。

You can only really tell what someone may be responding to chronologically.你只能真正说出某人可能按时间顺序回应的内容。 That is, if you have sent them a message, then their response is related to that last message that you sent.也就是说,如果您向他们发送了一条消息,那么他们的回复将与您发送的最后一条消息相关。

This is an issue if you intend to send more than one message asking for a response at the same time.如果您打算同时发送多个请求回复的消息,这就是一个问题。 In this situation, we recommend you use two different From numbers to send the message and store that From number against the order in your system.在这种情况下,我们建议您使用两个不同的From号码发送消息,并将该From号码与您的系统中的订单相对应。 That way you can tell which message was being responded to by matching up the user's phone number and the phone number that you used to send the message.这样您就可以通过匹配用户的电话号码和您用来发送消息的电话号码来判断正在回复哪条消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM