Twilio - how to send an SMS based on body of incoming message?

回眸只為那壹抹淺笑 提交于 2021-01-28 09:24:37

问题


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:

  • 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

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/


回答1:


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;



回答2:


Twilio developer evangelist here.

When responding to an incoming SMS message with TwiML if you use a <Message> with no attributes, then the response will be sent back to the original number.

However, you can also direct Twilio to send messages to other numbers using the to attribute. You can also send more than one message by returning multiple <Message> elements.

Adding those two things together means that you can do something like the following:

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

Let me know if that helps at all.



来源:https://stackoverflow.com/questions/46258103/twilio-how-to-send-an-sms-based-on-body-of-incoming-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!