Twilio - 基于传入消息正文发送短信

Twilio - 基于传入消息正文发送短信

问题描述:

我在我的网站上使用Twilio PHP API。我们的目标是让我们的游戏部族成员填写一份表格,其中包括他们的姓名和手头的问题。然后文本将被发送到预先确定的管理员列表,并有权修复服务器。Twilio - 基于传入消息正文发送短信

这部分工作很好。我可以填写我的网站上的表格,并发送文本没有问题。

<?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." 
     ) 
    ); 

我希望管理员能够能够回答“GOTIT”,以提醒有人已经对这个问题工作的其他管理员。当我的Twilio号码收到“GOTIT”文本时,我希望它将预先确定的SMS发送到预先确定的管理员列表(此处不需要任何动态)。

我已经配置了网络挂接指向我的“警报的response.php”(下)

到目前为止,唯一的Twilio文档,我能找到的是关于回复邮件的发送者的文件(我想以回复指定的用户列表)
-https://www.twilio.com/docs/guides/how-to-receive-and-reply-in-php#what-is-a-webhook

有没有人有我的起点?我已经试过这一点,但它一直没有卓有成效(警报-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('Looks like $name is taking care of this server alert! Reply HELP if you need a hand.'); 
}else if($body == 'HELP'){ 
    $response->message('On second thought, maybe $name could use a hand with this problem.'); 
} 
print $response; 

基于以下两个帮助文档的怪人: - HTTPS://www.twilio.com/docs/quickstart/php/sms /回复短信 - https://www.twilio.com/docs/guides/how-to-receive-and-reply-in-php#custom-responses-来电短信消息

在此先感谢您的帮助!


更新时间:

下面是基于你已经证明我什么更新的警报,response.php。我在调试器中没有发现任何错误,但是我没有收到任何SMS答复。对此有何看法?

(另外,我不能让PHP代码,所以我其实可以在这里发布正确的格式,所以我想我会使用一些第三方网站剪贴板?但愿这不是违反规定?)

http://www.wepaste.com/46258103/

+0

我相信你在语义上有点失落。这听起来像你不想回复发件人,而是发送一系列新消息。 因此,您的webhook文件(alert-response.php)应该以与您的第一块代码类似的方式工作。 –

+0

这使得更多的意义。仍然不确定我将如何去检查传入文本的正文以发送我的答复。 –

看来你很接近答案。

当Twilio收到短信(Inbound SMS)时,它可以调用服务器(HTTP Request)中的特定URL端点。 Twilio webhook

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

如果您想要将其他用户作为该消息的反应消息,则需要添加更多代码以创建新消息。

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 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; 
+0

无法弄清楚如何附加我的更改作为对评论的回复?所以我将它们添加为原始帖子的编辑..你认为你可以看一看,并告诉我哪里出了问题?看起来好像我没有收到您提供的脚本的消息。你的语法是有道理的。非常感谢您的帮助。 –

+0

@DarrenR请注意,我写的代码不会运行。有一系列缺失的变量。例如,'$ response'永远不会被创建。尽管如此,你最初的问题的答案仍然是上面的问题。 您的后续问题是另一回事:您需要定义'$ response'。 (来自Twilio Documentation:'$ response = new Twiml;')。请务必检查您的服务器日志中是否有其他错误! –

Twilio开发者传道这里。

responding to an incoming SMS message with TwiML如果您使用的<Message>没有属性,那么响应将被发送回原始数字。

但是,您还可以使用to attribute指示Twilio将消息发送到其他号码。您也可以通过返回多个<Message>元素发送多条消息。

添加这两个东西放在一起意味着你可以做类似如下:

<?php 
require_once './vendor/autoload.php'; 
use Twilio\Twiml; 

$people = array(
    "+zzzzzzzzzz"=>"Tech 1", 
    "+zzzzzzzzzz"=>"Tech 2", 
    "+zzzzzzzzzz"=>"Tech 3", 
); 

if(!$name = $people[$_REQUEST['From']]) { 
    $name = "unknown"; 
} 

$response = new Twiml(); 
foreach ($people as $number => $techName) { 
    $response->message('Looks like $name is taking care of this server alert!', ['to' => $number])); 
} 

echo $response; 

让我知道这是否有助于在所有。