Extending twilio plugin to work with WordPress REST API

僤鯓⒐⒋嵵緔 提交于 2020-01-06 07:23:05

问题


I've worked through the twilio tutorials regarding sending and receiving SMS with WordPress. I integrated them into a test install I have and then merged them into one. (The receive one is pretty short, although it's not a full "receive" more than a blind response).

Then I came across themebound's twilio-core and so I had a look at that and quickly I got a fatal error because they both use the twilio helper library. For testing, I just deactivated the first one and activated the second, which leads me into my first question:

Both of these use the same library, and have both used require_once. Each loaded it into their own plugin folder. The original name of the library is twilio-php-master, one renames it twilio the other twilio-php. Not that it matters at all, since they're in separate locations. The fatal error is as a result of the inner workings having the same function names.

  1. How can I test for the existence of the other plugins helper library and use that in place of the one that I have installed?

  2. What about the order of how WordPress loads the plugins? It's likely if mine loads first... Well, I can't even get that far because it crashes just trying to have both activated.

    --- we're now leading into the next question ---

    As a result, I'm probably going to go with the twilio-core version because it seems more featured and is available on github, even if the author isn't overly active (there's a pull request that's months old and no discussion about it).

    I want to extend the functionality of one of the sending plugins (either one) with respect to the receipt of the message from twilio. At the moment, both examples use classes for the sending and the receive that I'm using is not. As such I have just added it to the end of the main plugin file (the one with the plugin metadata). (a quick note, this is not the cause of the above fatal error, this happened before I started merging the send and receive).

    Because the receive is involved with the REST API and not initiated by a user action on the system (ie someone in the admin area accessing the class through the admin panel), I'm not sure if it's appropriate that a) I put it there, and b) use the send function inside the class when further processing the receipt. I have an end goal of analysing the incoming message and forwarding it back out through twilio or other application or even just recording it in wordpress itself.

  3. Should I keep the receive functionality/plugin separate from the sending one?

    And this leads on to the hardest question for me:

  4. How would I extend either plugin to make the send function available to my receive plugin? (this is where part of my confusion comes from) -->> Because both plugins only operate in the admin area, and the REST API isn't an actual user operating in the front-end, how can I call those functions in the admin area? Will is "just be available"? Do I have to replicate them on the public side? and then if so, is it necessary to have it in the admin area as well?

edit: With respect to one of the comments below, I have tested and twl_send_sms is available once the helper is loaded. What I will do is determine a way to see if the helper is loaded (a function exists test will probably suffice) and if so, require or not require my version as appropriate.

From the receive message I am now able to craft a separate forward of a new message. But how can I pass the callback function the parameters of the initial inbound message? eg, how do I populate $sms_in with the POST data?

function register_receive_message_route() {
  register_rest_route( 'sms/v1', '/receiver_sms', array(
    'methods' => 'POST',
    'callback' => 'trigger_receive_sms',
  ) );
}

function trigger_receive_sms($sms_in = '') {
  /* we have three things to do:
   *  1: send the reply to twilio,
   *  2: craft a reply,
   *  3: save the data message to the database
   */

  echo header('content-type: text/xml');

  echo ('<?xml version="1.0" encoding="UTF-8"?>');
  echo ('<Response>');
  echo ('  <Message>Thanks, someone will be in contact shortly.</Message>');
  echo ('</Response>');

    $args = array(
        'number_to' => '+xxxxxxxxxxx',
        'message' => "Test Worked!\n$sms_in",
        );
    twl_send_sms( $args );

回答1:


Twilio developer evangelist here.

It sounds to me as though your send functionality (whatever you can get out of twilio-core) is separate to your receive functionality. So I would likely split those up as two plugins and follow the instructions from the post you mentioned on how to write something that receives and responds to SMS messages. That way, twilio-core can use the Twilio library it bundles and your receive plugin won't need to use a library (it just needs to respond with TwiML, which is just XML).

I'm not sure I understand the last part of question 4 though, you can't really interact yourself with the receive endpoint of the plugin because all it will do is return XML to you. That's for Twilio to interact with.

Edit

In answer to your updated question, when your callback is triggered by a POST request it is passed a WP_REST_Request object. This object contains the POST body and the parameters can be accessed by array access.

function trigger_receive_sms($request) {
  echo $request['Body'];
}

In good news, if you just plan to send two messages then you can do so entirely with TwiML. You just need to use two <Message>s:

echo ('<?xml version="1.0" encoding="UTF-8"?>');
echo ('<Response>');
echo ('  <Message>Thanks, someone will be in contact shortly.</Message>');
echo ('  <Message to="YOUR_OTHER_NUMBER">It worked!</Message>');
echo ('</Response>');

This way you don't need to worry about that other library.



来源:https://stackoverflow.com/questions/46059351/extending-twilio-plugin-to-work-with-wordpress-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!