Twilio - 如何根据传入消息的正文发送短信? [英] Twilio - how to send an SMS based on body of incoming message?

查看:21
本文介绍了Twilio - 如何根据传入消息的正文发送短信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网站上使用

网页(HTTP Response)将作为回复(Outbound SMS)发回给用户的内容.因此,print $response; 打印将作为回复发送给 Inbound SMS 的作者的消息内容.

如果您想向其他用户发送消息作为对该消息的反应,您需要添加更多代码来创建新消息.

您的 alert-response.php 既可以回复发件人,也可以向其他管理员发送消息:

message('感谢您的照顾.');//现在创建一条消息告诉其他管理员有人//正在处理它$client = new Client($AccountSid, $AuthToken);$sms = $client->account->messages->create(到,大批('来自' =>"+zzzzzzzzzz",'身体' =>看起来 $name 正在处理这个服务器警报!"););}else if( $body == 'HELP' ){//回复回复 HELP 的管理员$response->message('好的,我会告诉别人你需要帮助');//现在创建一条消息告诉其他管理员有人//正在处理它$client = new Client($AccountSid, $AuthToken);$sms = $client->account->messages->create(到,大批('来自' =>"+zzzzzzzzzz",'身体' =>看起来 $name 需要帮助!!");}//请记住,响应会发送给发送请求的人//首先是短信,而不是其他管理员.打印 $response;

I'm using the Twilio PHP API on my site. The goal is for members of our gaming clan to fill out a form which includes their name and the issue at hand. A text will then be sent to a predetermined list of admins with access to fix the server.

This part is working great. I can fill in the form on my site and it sends the text without issue.

<?php
require_once "autoload.php";
use Twilio\Rest\Client;

$AccountSid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$AuthToken = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy";

$client = new Client($AccountSid, $AuthToken);

$sms = $client->account->messages->create(
  $_REQUEST["to"],
    array(
      'from' => "+zzzzzzzzzz", 
      'body' => "Help!". $_REQUEST["name"]. " says "
                . $_REQUEST["message"]."
                . Reply GOTIT to alert other techs."
    )
);

I want the admins to be able to be able to reply "GOTIT" to alert other admins that someone is already working on the problem. When my Twilio number receives the "GOTIT" text, I want it to send a predetermined SMS to a predetermined list of admins (nothing dynamic is required here).

I have configured the webhook to point to my alert-response.php file (below).

So far, the only Twilio documentation I can find is regarding replying to the sender of a message (I want to reply to a specified list of users)
https:// www.twilio.com/docs/guides/how-to-receive-and-reply-in-php#what-is-a-webhook

Does anybody have any starting points for me? I've tried this, but it hasn't been fruitful (alert-response.php):

<?php

require_once "autoload.php";
use Twilio\Rest\Client;

// make an associative array of senders we know,
// indexed by phone number
$people = array(
    "+zzzzzzzzzz"=>"Tech 1",
    "+zzzzzzzzzz"=>"Tech 2",
    "+zzzzzzzzzz"=>"Tech 3",
);

// if the sender is known, then greet them by name
// otherwise, consider them just another monkey
if(!$name = $people[$_REQUEST['From']]) {
    $name = "unknown";
}

$body = $_REQUEST['body'];

if( $body == 'GOTIT' ){
    $response->message('$name GOTIT message. Reply HELP for help.');
}else if( $body == 'HELP' ){
    $response->message('$name HELP message.');
}
print $response;

Based on a Frankenstein of the following two help docs:

Thanks in advance for any assistance!


Updated:

Here's an updated alert-response.php based on what you've shown me. I don't get any errors in the debugger after a few small changes, but I'm not getting any SMS replies either. Any ideas on that?

Note: The following code reference is missing since wepaste.com no longer exists: (Also, I can't get the PHP code to format properly so I can actually post it here, so I guess I'll use some third party clipboard website? Hopefully that's not against the rules?) http://www.wepaste.com/46258103/

解决方案

It seems that you are very close to the answer.

When Twilio receives an SMS (Inbound SMS), it can call a specific URL endpoint in you server (HTTP Request).

The content that webpage (HTTP Response) will be send back to user as a reply (Outbound SMS). Thus, print $response; prints the content of the message that will be sent as a reply to author of the Inbound SMS.

If you want to message other users as a reaction to that message, you need to add more code to create a new message.

Your alert-response.php can both reply to the sender and message the other admins:

<?php

require_once "autoload.php";
use Twilio\Rest\Client;

// make an associative array of senders we know, indexed by phone number
$people = array(
    "+zzzzzzzzzz"=>"Tech 1",
    "+zzzzzzzzzz"=>"Tech 2",
    "+zzzzzzzzzz"=>"Tech 3",
);

// if the sender is known, then greet them by name
// otherwise, consider them just another monkey
if(!$name = $people[$_REQUEST['From']]) {
    $name = "unknown";
}

$body = $_REQUEST['body'];

if( $body == 'GOTIT' ){
    // response to admin that send GOTIT
    $response->message('Thanks for taking care of it.');

    // now creates a message to tell other admins that someone
    // is taking care of it
    $client = new Client($AccountSid, $AuthToken);
    $sms = $client->account->messages->create(
         TO,
         array(
        'from' => "+zzzzzzzzzz", 
        'body' => "Looks like $name is taking care of this server alert!"
    );
);

}else if( $body == 'HELP' ){
    // response to the admin that replied with HELP
    $response->message('Ok. I will tell others that you need help');


    // now creates a message to tell other admins that someone
    // is taking care of it
    $client = new Client($AccountSid, $AuthToken);
    $sms = $client->account->messages->create(
         TO,
         array(
        'from' => "+zzzzzzzzzz", 
        'body' => "Looks like $name needs help!!"
    );
}

// keep in mind that response is sent to the person that sent the
// SMS in first place, not to the other admins.
print $response;

这篇关于Twilio - 如何根据传入消息的正文发送短信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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