public
Last active

  • Download Gist
ChatServlet.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
package com.twilio;
 
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
 
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.google.appengine.api.xmpp.JID;
import com.google.appengine.api.xmpp.Message;
import com.google.appengine.api.xmpp.MessageBuilder;
import com.google.appengine.api.xmpp.SendResponse;
import com.google.appengine.api.xmpp.XMPPService;
import com.google.appengine.api.xmpp.XMPPServiceFactory;
import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.factory.SmsFactory;
import com.twilio.sdk.resource.instance.Sms;
import com.twilio.sdk.verbs.TwiMLResponse;
 
@SuppressWarnings("serial")
public class ChatServlet extends HttpServlet {
HashSet<String> subs = new HashSet<String>();
 
// Send out a given message to all subscribers
private void sendMessage(String body) {
Iterator<String> it = subs.iterator();
while (it.hasNext()) {
String sub = it.next();
String messageBody = "Group Chat: " + body;
 
// We assume an at symbol is a chat client
if (sub.indexOf("@") >= 0) {
JID jid = new JID(sub);
Message msg = new MessageBuilder().withRecipientJids(jid)
.withBody(messageBody).build();
 
boolean messageSent = false;
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
SendResponse status = xmpp.sendMessage(msg);
messageSent = (status.getStatusMap().get(jid) == SendResponse.Status.SUCCESS);
 
if (!messageSent) {
// TODO: Handle this case.
}
}
 
// Otherwise, it's an SMS number
else {
TwilioRestClient client = new TwilioRestClient(
"AC308ac9a994b1bd8b73ca0166442236c7",
"1cb7a094ce91af64cc6bcc12a449fb1c");
 
Map<String, String> params = new HashMap<String, String>();
params.put("Body", messageBody);
params.put("To", sub);
params.put("From", "+16122948105");
 
SmsFactory messageFactory = client.getAccount().getSmsFactory();
 
try {
Sms message = messageFactory.create(params);
System.out.println(message.getSid());
} catch (TwilioRestException e) {
e.printStackTrace();
}
}
}
}
 
// Handle Incoming SMS
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
try {
TwiMLResponse twiml = new TwiMLResponse();
 
// parse the body, looking for a command
String smsBody = request.getParameter("Body");
String smsFrom = request.getParameter("From");
 
// Unsubscribe, if requested
if (smsBody.startsWith("STOP")) {
subs.remove(smsFrom);
com.twilio.sdk.verbs.Sms bye = new com.twilio.sdk.verbs.Sms(
"You have been unsubscribed. Thank You!");
twiml.append(bye);
} else {
// If they aren't subscribed, subscribe them
if (!subs.contains(smsFrom)) {
subs.add(smsFrom);
}
sendMessage(smsBody);
}
 
response.setContentType("application/xml");
response.getWriter().print(twiml.toXML());
 
} catch (Exception e) {
e.printStackTrace();
System.out.println(e);
}
}
 
// Handle Incoming XMPP Chat messages
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException {
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
Message msg = xmpp.parseMessage(req);
 
JID fromJid = msg.getFromJid();
String body = msg.getBody();
 
// Unsubscribe, if requested
if (body.startsWith("STOP")) {
subs.remove(fromJid.getId());
} else {
// If they aren't subscribed, subscribe them
if (!subs.contains(fromJid.getId())) {
subs.add(fromJid.getId());
}
sendMessage(body);
}
}
}
appengine-web.xml
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>twiliosandbox</application>
<version>1</version>
 
<!-- Allows App Engine to send multiple requests to one instance in parallel: -->
<threadsafe>true</threadsafe>
 
<!-- Configure java.util.logging -->
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties" />
</system-properties>
 
<inbound-services>
<service>xmpp_message</service>
<service>xmpp_presence</service>
<service>xmpp_subscribe</service>
<service>xmpp_error</service>
</inbound-services>
 
</appengine-web-app>
web.xml
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
<servlet>
<servlet-name>chat</servlet-name>
<servlet-class>com.twilio.ChatServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>chat</servlet-name>
<url-pattern>/_ah/xmpp/message/chat/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>chat</servlet-name>
<url-pattern>/chat</url-pattern>
</servlet-mapping>
 
<servlet>
<servlet-name>SystemServiceServlet</servlet-name>
<servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
<init-param>
<param-name>services</param-name>
<param-value />
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SystemServiceServlet</servlet-name>
<url-pattern>/_ah/spi/*</url-pattern>
</servlet-mapping>
 
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.