0

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 Answers1

1

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).
IObert
  • 2,118
  • 1
  • 10
  • 17
  • Not using phone numbers would be like replying to e-mails without using e-mail addresses, would it not? I'm looking for a link on how to parse a phone number from an incoming request. – Nicholas Saunders Jun 12 '23 at 15:08
  • No, because Twilio does a lot of footwork for you in the background. So you can rely on the cookie. Or only return TwiML that doesn't specify a number at all -- at it will still find its way back to the original sender. But it is possible to work with the number if that's what you want. – IObert Jun 12 '23 at 19:12
  • I'm just not understanding why sessions are even being used. Sessions of what? That, I suppose, is where I'm confused. A text message is sent and then replied to, and then maybe that reply is replied to. Yes, while Twilio employs REST, there are no "sessions" or "cookies" in text messages. – Nicholas Saunders Jun 13 '23 at 04:39
  • Like HTTP is, by nature, a stateless protocol, cookies can help the server to connect all individual request to one session. And similarly, Twilio, connects all messages within this 4-hour window, to one session. This is intended to make your life easier. In some way, you can see Twilio as a technology bridge which converts events from the SMS-protocol to the HTTP-protocol to make it easy for web devs. – IObert Jun 13 '23 at 06:47
  • okay, and not to drag this out, but within a four hour window: would multiple messages from multiple numbers then be grouped under a single session? Or, would each conversation thread be distinct? – Nicholas Saunders Jun 13 '23 at 15:37
  • Each thread is distinct :) – IObert Jun 14 '23 at 07:11