在 Twilio 回调中获取消息正文并在响应中使用它 [英] Getting body of a message in a Twilio callback and using it in a response

查看:39
本文介绍了在 Twilio 回调中获取消息正文并在响应中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 TwiML Bin 我找到了对 {{body}}{{from}} 的引用,但就回调而言,它们在 TwiML 中不起作用.我在回调中获取消息详细信息时遇到问题,而且我找不到合适的参考文档来记录它.

From the TwiML Bin I found this reference to {{body}} and {{from}} but they don't work in TwiML as far as callbacks go. I'm having trouble getting the message details in the callback and I haven't been able to find suitable reference documenting it.

这就是我所拥有的(并且已确认有效):

This is what I have (and this is confirmed working):

add_action( 'rest_api_init', 'register_receive_message_route');

/**
 * Register the receive message route.
 *
 */
function register_receive_message_route() {
  register_rest_route( 'srsms/v1', '/receive_sms', array(
    'methods' => 'POST',
    'callback' => 'trigger_receive_sms',
  ) );
}

/**
 * The Callback.
 *
 */
function trigger_receive_sms() {

  $y = "this works"; //$_POST['Body'], 0, 1592); << this doesn't

  echo ('<?xml version="1.0" encoding="UTF-8"?>');
  echo ('<Response>');
  echo ("  <Message to='+NUMBER'>xxx $y xxx</Message>");
  echo ('</Response>');

  die();
}

我缺少的是将正文传递给转发的消息.我在回电结束时尝试了很多片段,但我真的只是在这里猜测.

What I'm missing is passing the body to the forwarded message. I have tried quite a few snippets at the end of the call back, but I'm really just guessing here.

推荐答案

Twilio 开发人员布道者在这里.

Twilio developer evangelist here.

Twilio 向您的 URL 发出 POST 请求时,它将有关 SMS 的所有数据作为请求正文中的 URL 编码参数发送.您可以在此处查看文档中发送的所有参数:https://www.twilio.com/docs/api/twiml/sms/twilio_request#request-parameters.

When Twilio makes a POST request to your URL, it sends all the data about the SMS as URL encoded parameters in the body of the request. You can see all the parameters that are sent in the docs here: https://www.twilio.com/docs/api/twiml/sms/twilio_request#request-parameters.

当您收到对 WordPress URL 的请求时,回调函数会接收一个 WP_REST_Request 对象作为参数.此请求对象可以访问作为请求的一部分发送的所有参数,您可以使用 $request['paramName'] 通过数组访问来访问它们.

When you receive a request to your WordPress URL, the callback function receives a WP_REST_Request object as an argument. This request object has access to all the parameters sent as part of the request and you can access them via array access using $request['paramName'].

因此,要获取发送的消息正文,您需要像这样调用 $request['Body']:

So, to get the body of a message sent you need to call for $request['Body'] like this:

function trigger_receive_sms($request) {
  $body = $request['Body'];

  // return TwiML
}

让我知道这是否有帮助.

Let me know if that helps at all.

这篇关于在 Twilio 回调中获取消息正文并在响应中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆