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. 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.
My Code to send SMS and webhook (replytoSMS) are given below.
<?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;
}