扩展 twilio 插件以使用 WordPress REST API [英] Extending twilio plugin to work with WordPress REST API

查看:32
本文介绍了扩展 twilio 插件以使用 WordPress REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了关于 发送使用 WordPress 接收 短信.我将它们集成到我的测试安装中,然后将它们合并为一个.(接收的时间很短,虽然它不是一个完整的接收"而不是盲目的响应).

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).

然后我遇到了 themebound 的 twilio-core,所以我看了看,很快我得到了一个致命错误,因为它们都使用 twilio 帮助程序库.为了测试,我只是停用了第一个并激活了第二个,这使我进入了我的第一个问题:

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:

这两个使用相同的库,并且都使用了require_once.每个将其加载到自己的插件文件夹中.该库的原始名称是 twilio-php-master,一个重命名为 twilio 另一个 twilio-php.这根本不重要,因为它们位于不同的位置.致命错误是由于内部工作具有相同的函数名称.

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. 如何测试其他插件帮助程序库是否存在并使用它代替我已安装的帮助程序库?

  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?

WordPress 加载插件的顺序如何?如果我的先加载很可能......好吧,我什至不能走那么远,因为它只是试图让两者都激活而崩溃.

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.

--- 我们现在进入下一个问题 ---

因此,我可能会使用 twilio-core 版本,因为它看起来更有特色并且可以在 github 上找到,即使作者不是过于活跃(有一个几个月前的拉取请求,没有关于它的讨论).

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).

关于从 twilio 接收消息,我想扩展其中一个发送插件(任一)的功能.目前,这两个示例都使用类进行发送,而我使用的接收则不是.因此,我刚刚将它添加到主插件文件(带有插件元数据的文件)的末尾.(快速说明,这不是上述致命错误的原因,这发生在我开始合并发送和接收之前).

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).

因为接收涉及 REST API,而不是由系统上的用户操作启动(即管理区域中的某人通过管理面板访问课程),我不确定 a) 我是否合适把它放在那里,并且 b) 在进一步处理收据时使用类内部的发送函数.我的最终目标是分析传入的消息并通过 twilio 或其他应用程序将其转发出去,甚至只是将其记录在 wordpress 本身中.

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.

我应该将接收功能/插件与发送功能/插件分开吗?

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

这引出了对我来说最难的问题:

And this leads on to the hardest question for me:

我将如何扩展这两个插件以使发送功能可用于我的接收插件? (这是我困惑的部分来源)-->> 因为这两个插件都只在管理区操作,REST API 不是实际操作在前端的用户,那么如何在管理区调用这些功能呢?会是刚好可用"吗?我是否必须在公共方面复制它们?那么如果是这样,是否有必要将它也放在管理区域中?

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?

关于下面的评论之一,我已经测试过,一旦加载了帮助程序,twl_send_sms 就可用.我要做的是确定一种方法来查看帮助程序是否已加载(函数存在测试可能就足够了),如果是,则根据需要需要或不需要我的版本.

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.

从收到的消息中,我现在可以制作新消息的单独转发.但是如何将初始入站消息的参数传递给回调函数呢?例如,如何使用 POST 数据填充 $sms_in?

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

推荐答案

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

Twilio developer evangelist here.

在我看来,您的发送功能(无论您可以从 twilio-core 中获得什么)与您的接收功能是分开的.因此,我可能会将它们拆分为两个插件,并按照 您提到的帖子 关于如何编写接收和响应 SMS 消息的内容.这样,twilio-core 可以使用它捆绑的 Twilio 库,并且您的接收插件不需要使用库(它只需要使用 TwiML 进行响应,这只是 XML).

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).

不过,我不确定我是否理解问题 4 的最后一部分,您无法真正与插件的接收端点进行交互,因为它所做的只是将 XML 返回给您.这是供 Twilio 与之交互的.

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.

编辑

为了回答您更新的问题,当您的回调由 POST 请求触发时,它会传递一个 WP_REST_Request 对象.该对象包含 POST 主体,参数可以通过数组访问.

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'];
}

好消息是,如果您只是打算发送两条消息,然后您就可以完全使用 TwiML 执行此操作.你只需要使用两个 s:

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.

这篇关于扩展 twilio 插件以使用 WordPress REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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