How to receive and reply to SMS messages with Twilio?

170 views Asked by At

Example code:

import java.util.Arrays;
import java.util.List;
import com.twilio.Twilio;
import com.twilio.rest.notify.v1.service.Notification;

public class Example {
  // Find your Account Sid and Token at twilio.com/user/account
  public static final String ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  public static final String AUTH_TOKEN = "your_auth_token";

  public static final String SERVICE_SID = "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

  public static void main(String[] args) {
    // Initialize the client
    Twilio.init(ACCOUNT_SID, AUTH_TOKEN);

    List<String> toBindings = Arrays.asList(
    "{\"binding_type\":\"sms\",\"address\":\"+15555555555\"}",
    "{\"binding_type\":\"facebook-messenger\",\"address\":\"123456789123\"}");

    Notification notification = Notification
        .creator(SERVICE_SID)
        .setBody("Hello Bob")
        .setToBinding(toBindings)
        .create();

    System.out.println(notification.getSid());
  }
}

Which is all well and good. How are message replies received and tracked? Furthermore,how are those replies replied to?

Not looking to make a chat bot but just to send and receive messages in a somewhat threaded fashion. Looking to use TwiML and Java.


further reading shows:

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.twilio.twiml.messaging.Body;
import com.twilio.twiml.messaging.Message;
import com.twilio.twiml.MessagingResponse;
import com.twilio.twiml.TwiMLException;

public class TwilioServlet extends HttpServlet {
  public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    HttpSession session = request.getSession(true);
    Integer counter = (Integer) session.getAttribute("counter");
    if (counter == null) {
      counter = new Integer(0);
    }

    /* Increment the counter by one, and store the count in the session. */
    int count = counter.intValue();
    count++;
    session.setAttribute("counter", new Integer(count));

    // Create a dict of people we know.
    HashMap<String, String> callers = new HashMap<String, String>();
    callers.put("+14158675308", "Rey");
    callers.put("+12349013030", "Finn");
    callers.put("+12348134522", "Chewy");

    String fromNumber = request.getParameter("From");
    String toNumber = request.getParameter("To");
    String fromName = callers.get(fromNumber);
    if (fromName == null) {
      // Use the caller's name
      fromName = "Friend";
    }

    String message =
        fromName + " has messaged " + toNumber + " " + String.valueOf(count) + " times.";

    // Create a TwiML response and add our friendly message.
    Message sms = new Message.Builder().body(new Body(message)).build();
    MessagingResponse twimlResponse = new MessagingResponse.Builder().message(sms).build();

    response.setContentType("application/xml");

    try {
      response.getWriter().print(twimlResponse.toXml());
    } catch (TwiMLException e) {
      e.printStackTrace();
    }
  }
}

Which is all well and good...but why use cookies at all? Because each SMS comes from a specific number why not track messages by their phone number?

I must be asking the wrong question, or asking the question the wrong way, because the above code isn't using phone numbers to track conversations.

With e-mail, for example, conversations or threads aren't tracked or managed with cookies but by who sent the message. The header. With SMS the equivalent would be the phone number.

Looking for a high level explanation for why cookies are being used and why phone numbers aren't being used.

1

There are 1 answers

6
IObert On

I'm not sure if I understand this question. This sample code seems to show two different things:

  1. How to use cookies to keep track of an individual conversation without handling phone numbers. It will use different cookies for different conversations within a certain time window. See:

This means you can use server-side sessions to keep track of application state between requests. How cool is that? Twilio will expire the cookies for that conversation after four hours of inactivity, as if the user "closed the browser."

Source

  1. How to parse a phone number from an incoming request to do whatever you want programmatically (in this case, reading names from a HashMap).