【问题标题】:How to do a callback with user's number when user replies STOP via SMS using Twilio?当用户使用 Twilio 通过 SMS 回复 STOP 时,如何使用用户号码进行回调?
【发布时间】:2018-09-28 20:48:08
【问题描述】:

当用户通过 SMS 回复 STOP 时,我想知道如何使用来自 Twilio 的回调来获取用户的号码或一些数据以更新数据库以在我的服务器中选择退出。

我正在使用 PHP。

我该如何解决这个问题?

【问题讨论】:

  • 到目前为止你有什么尝试?
  • @Demon 我关注了这个twilio.com/blog/2016/08/receive-sms-php-twilio.html我的服务器回复了我的手机,我也做了“停止”取消订阅并做了“开始”来重置,反正我之前不知道如何检测“停止”进行回调并更新数据库。
  • @Ivan 它(基本上)将在您的处理程序中为if($_POST['Body'] === 'STOP') { // do something }。 (比这稍微复杂一些 - 您需要使用 trim() 处理额外的空格,使其不区分大小写等,但这是一般的想法......)
  • @ceejayoz 我也是这么想的,但是如何获取电话号码,我需要用户提供任何类型的数据,以便在选择退出之前在数据库中查找匹配。
  • 如果$number 给您发短信STOP,请完全停止发送给它。您应该有一个选择退出号码的数据库,在发送任何内容之前检查该号码。 Twilio 有效负载的完整内容是 well documented

标签: php twilio twilio-api twilio-php


【解决方案1】:

我设法将$_POST['Body'] 与一些过滤函数进行了比较,这是我的完整代码:

// You need to include Twilio library path here... for example require_once '/path/to/twilio-lib/autoload.php';
use Twilio\Twiml;

global $wpdb;

$response = new Twiml;


    if (!empty($_POST))
    {
        $number = $_POST['From'];
        $body = $_POST['Body'];
        $default = "Hi " .$number. ", I'm a bot, please do not reply until you will get an update in some day. Have a nice day!";   


        $result = preg_replace("/[^A-Za-z0-9]/u", " ", $body); 
        $result = trim($result); 
        $result = strtolower($result); 



        //words to match and $result must be lowercase to define.
        if ($result == 'stop' || $result == 'stopall' || $result == 'cancel' || || $result == 'end') || $result == 'quit' || $result == 'unsubscribe'{
         //Your function to update database with $number

        }
        else if ($result == 'start' || $result == 'yes' || $result == 'unstop'){
            $response->message('Thanks for re-subscribing');
           //Your function to update database, again with $number
        }   
         else {
            $response->message($default);
        }

        print $response;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 2018-06-29
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 2022-10-12
    • 2018-04-11
    相关资源
    最近更新 更多