Skip to content

Instantly share code, notes, and snippets.

@guffyWave
Forked from kwhinnery/ChatServlet.java
Created January 27, 2016 11:22
Show Gist options
  • Save guffyWave/d232c42d14ae57ef9592 to your computer and use it in GitHub Desktop.
Save guffyWave/d232c42d14ae57ef9592 to your computer and use it in GitHub Desktop.
<?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>
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);
}
}
}
<?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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment