4

For some reason, I'm having a very tough time understanding how the Twilio model works; as a result, I'm trying to code a solution by guessing (something I hate doing.) I hope someone can help with the confusion.

I've already set up a forwarder so that when someone sends a text to my Twilio number, I receive it on my phone. The problem is, when I reply to that text, it goes to Twilio instead of back to the original sender.

I've tried passing my number as a 'from' string in the tag, but this gets rejected by Twilio as not being a valid Twilio number.

<?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>
Alex Baban
  • 11,312
  • 4
  • 30
  • 44
  • Why are you trying to use your number as from? Also when you reply to Twilio, Twilio knows from what number the message is (your number). I'm not sure but are you trying to mask your phone number, something like this use case: https://www.twilio.com/use-cases/masked-phone-numbers ? – Alex Baban Jun 19 '16 at 20:15
  • 1
    Because Twilio does not send the reply back to me, and that's what I'd like for it to do. In other words, if Joe texts my Twilio number, I will get his text on my cellphone - but if I reply to his text from my cellphone, the reply will go to (and stay at) Twilio, rather than being sent on to Joe. That last piece - how can I make that reply go back to Joe - is what I'm trying to figure out. – bluewater_sailor Jun 19 '16 at 21:53

1 Answers1

2

When a message is forwarded to you from Twilio you need to know the phone number which originated the message.

When you send a message you need to tell Twilio where to send your message.

So, with a convention like: the messages start with a phone number, then a /, then the actual message, you can use this code for the 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>"
    );

}

?>

As you can see, the code checks for your phone number. If the message that is received by Twilio is from your number then the code will send it to a number that you put at the start of your message. Your message should be something like this:

+15553331111/ Hey, how is going?

Twilio has some tutorials for masked phone numbers if you need something more elaborated. https://www.twilio.com/docs/tutorials

Alex Baban
  • 11,312
  • 4
  • 30
  • 44
  • Thanks, but this doesn't answer my question. Perhaps I haven't explained it well, so I'll try again: in a normal SMS conversation, If Joe texts me, his SMS is tagged as coming from his number - so if I respond to it, my reply will go back to him. But if Joe texts my Twilio number, the text I get is tagged as coming from **Twilio's** number - so if I reply to that text, my SMS does not get sent to Joe, but to my own number at Twilio. Having to manually create a new conversation by copying the number from the text for every person who texts me is not a reasonable solution. – bluewater_sailor Jun 19 '16 at 23:47
  • I understand, but if you don't tell Twilio where to send your message it will not know unless you have some convention that is also implemented in your code. Also when you receive messages, say Joe1 and Joe2 will send at the same time, to whom are you replying since you receive both messages from Twilio? Are you reserving the number just for Joe? Then you can hard-code his number and all messages will go to him when they are sent from your #. – Alex Baban Jun 20 '16 at 00:15
  • I guess the question comes down to "is there a way to implement a stateful mechanism in Twilio, similar to sessions and/or cookies in HTML". It's rather hard to believe that there would not be one - but it's also rather surprising that I can't find any examples of it in the tutorials. In any case, thanks for your help so far. Even if it's not the answer, it's at least helping me see the direction in which the problem lies. – bluewater_sailor Jun 20 '16 at 00:35