php – 如何使用Twilio进行“传递”短信交换?

出于某种原因,我很难理解Twilio模型是如何工作的;因此,我试图通过猜测来编写解决方案(我讨厌做的事情.)我希望有人可以帮助解决困惑.

我已经设置了一个转发器,这样当有人向我的Twilio号码发送文本时,我会在手机上收到它.问题是,当我回复该文本时,它会转到Twilio而不是回到原始发件人.

我已经尝试将我的号码作为标签中的’from’字符串传递,但这被Twilio拒绝,因为它不是有效的Twilio号码.

<?PHP header('Content-Type: text/html'); ?>
<Response>

  <!-- ****** This gets rejected: ****** -->
  <!-- Message to="<?=$_REQUEST['PhoneNumber']?>" from="<?=$_REQUEST['From']?>" -->

  <Message to="<?=$_REQUEST['PhoneNumber']?>">
    <?=htmlspecialchars(substr($_REQUEST['From'] . $_REQUEST['Body'], 0, 1600))?>
  </Message>
</Response>

解决方法:

当从Twilio向您转发消息时,您需要知道发起该消息的电话号码.

当您发送消息时,您需要告诉Twilio将消息发送到何处.

因此,通过以下约定:消息以电话号码开头,然后是/,然后是实际消息,您可以将此代码用于webhook.

<?PHP
    header("content-type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>

<?PHP 

$myPhoneNumber = "+15557779999"; 

if ($_REQUEST['From'] == $myPhoneNumber) {
    $message = explode("/", htmlspecialchars(substr($_REQUEST['Body'], 0, 1600)));
    $theOtherPhoneNumber = $message[0];
    $theOtherMessage = $message[1];
    echo(
        "<Response>
          <Message to=\"{$theOtherPhoneNumber}\">
            {$theOtherMessage}
          </Message>
        </Response>"
    );  

} else {
    $message = htmlspecialchars(substr($_REQUEST['From'] ."/ " .$_REQUEST['Body'], 0, 1600));
    echo(
        "<Response>
          <Message to=\"{$myPhoneNumber}\">
            {$message}
          </Message>
        </Response>"
    );

}

?>

如您所见,代码会检查您的电话号码.如果Twilio收到的消息来自您的号码,则代码会将其发送到您在消息开头放置的号码.您的消息应该是这样的:

+15553331111/ hey, how is going?

如果你需要更详细的东西,Twilio有一些掩盖电话号码的教程. https://www.twilio.com/docs/tutorials

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...