Twilio - 根据传入消息的身份发送短信

时间:2017-09-16 20:17:54

标签: twilio twilio-api twilio-php

我在我的网站上使用Twilio PHP API。我们的目标是让我们的游戏部落成员填写一份表格,其中包括他们的名字和手头的问题。然后,文本将被发送到具有修复服务器访问权限的预定管理员列表。

这部分工作得很好。我可以在我的网站上填写表格,它会毫无问题地发送文本。

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

我希望管理员能够回复&#34; GOTIT&#34;提醒其他管理员某人已经在解决问题。当我的Twilio号码收到&#34; GOTIT&#34;文本,我希望它将预先确定的SMS发送到预先确定的管理员列表(此处不需要动态)。

我已将webhook配置为指向我的&#34; alert-response.php&#34;档案(下)

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

有人对我有任何起点吗?我试过这个,但它没有成果(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('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;

基于Frankenstein的以下两个帮助文档: - https://www.twilio.com/docs/quickstart/php/sms/replying-to-sms-messages - https://www.twilio.com/docs/guides/how-to-receive-and-reply-in-php#custom-responses-to-incoming-sms-messages

提前感谢您的任何帮助!

更新:

根据您向我展示的内容,这里有一个更新的alert-response.php。经过一些小改动后,我在调试器中没有收到任何错误,但我也没有得到任何短信回复。有什么想法吗?

(另外,我无法正确格式化PHP代码,所以我可以在这里发布,所以我想我会使用一些第三方剪贴板网站?希望不会反对规则?)

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

2 个答案:

答案 0 :(得分:1)

Twilio开发者传道者在这里。

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

但是,您也可以指示Twilio使用to attribute向其他号码发送消息。您还可以通过返回多个<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;

让我知道这是否有帮助。

答案 1 :(得分:0)

您似乎非常接近答案。

当Twilio收到短信(Inbound SMS)时,它可以调用您服务器中的特定URL端点(HTTP Request)。 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;