7

Im creating an web app that uses twilio and I seem to have a problem.

The system consist of 3 actors:

  • A physical customer
  • A physical server
  • Our web application

Whenever a client creates a new "job" via a form, all of the job details will be sent in a text to many different servers. When any one of the servers reply to that text message, we need to be able to associate his reply with the particular "job" he replied to. The problem is I can't figure out how to do this without buying a number for each job and just associating each number with a job. If possible i would only like to use one number, since buying thousands of number is completely wasteful and impossible.

user2158382
  • 4,430
  • 12
  • 55
  • 97

3 Answers3

8

Cookies are the best way to handle a conversation, but because you cannot tell which text message the server replies to, it can be tricky. If you send the server 2 jobs, and they choose to reply to the first job, you have no way of knowing this.

I think you have two sensible ways of doing this:

  • Give each job a reference code (2 or 3 digits to make it easy) that the server can reply with. This is the most reliable way to do it.
  • Use multiple - but not many - phone numbers. So that during a period of one day, or a few hours, you only send a server one job per number. Then you can recycle the pool of numbers and reuse them after the job has 'timed out'. This rather depends on what your expect volumes are.
Megan Speir
  • 3,745
  • 1
  • 15
  • 25
xmjw
  • 3,154
  • 21
  • 29
5

Well according to Twilio, the answer is to use cookies:

http://www.twilio.com/docs/quickstart/php/sms/tracking-conversations

Just like in web applications, a cookie is a small file that your application can store on Twilio's servers to keep track of information, such as a username or account. For Twilio SMS, cookies are scoped to the "conversation" between two parties -- you can have a unique cookie for each To/From phone number pair. For example, you can store a unique cookie for any messages sent between 415-555-2222 and 415-555-1111, which will be different than the cookie used between 415-555-3333 and 415-555-1111.

E.J. Brennan
  • 45,870
  • 7
  • 88
  • 116
  • 1
    This will work if you don't need to track multiple conversations between 2 numbers. Say, if 415-555-2222 and 415-555-1111 are exchanging messages about 2 different subjects. – Paulo Abreu Nov 10 '14 at 19:11
  • I'm confused about where the cookie is actually set. If you search the page for `cookie`, the term doesn't appear in the code anywhere. What am I missing? – Jason Swett Aug 05 '15 at 15:59
1

I ran into this recently, and found an article that explains pretty well using the pool idea that @xmjw mentioned

How users can send text messages to each other over Twilio

Although the application is different, the concept here is sort of the same. For anyone else that might stumble upon this question and needing other solutions.

What I ended up doing in my solution was generating unique response codes. Then on the reply, I could query for the the response code and the from number to relate back to the original text. In my case though, it was a human on both sides. For one side being automated like a server, you could have the text be parsed for what the code is, then returned with that code. Then you would just SELECT * FROM messages WHERE code = ?.

dikirill
  • 1,873
  • 1
  • 19
  • 21
jeremywoertink
  • 2,281
  • 1
  • 23
  • 29