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 developer evangelist here.
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.
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']
.So, to get the body of a message sent you need to call for
$request['Body']
like this:Let me know if that helps at all.