[英]How can I use Twilio for “pass-through” SMS exchanges?
出於某種原因,我很難理解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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.