在 Twilio 回调中获取消息正文并在响应中使用它

Getting body of a message in a Twilio callback and using it in a response

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

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

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();
}

我缺少的是将正文传递给转发的邮件。我在回调结束时尝试了很多片段,但我真的只是猜测。

这里是 Twilio 开发人员布道者。

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'].

通过数组访问来访问它们

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

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

  // return TwiML
}

如果有帮助请告诉我。