TwilioRestClient client = new TwilioRestClient("ACCOUNT_SID", "AUTH_TOKEN"); Map params = new HashMap(); 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(); }
public class TwilioSmsServlet extends HttpServlet { // Handle Incoming SMS public void doPost(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")) { MultichannelChatManager.removeSub(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 (!MultichannelChatManager.isSubscribed(smsFrom)) { MultichannelChatManager.addSub(smsFrom); } MultichannelChatManager.sendMessage(smsBody, "sms"); } response.setContentType("application/xml"); response.getWriter().print(twiml.toXML()); } catch (Exception e) { e.printStackTrace(); System.out.println(e); } } }
public class XMPPReceiverServlet extends HttpServlet { // Handle Incoming XMPP Chat messages public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException { XMPPService xmpp = XMPPServiceFactory.getXMPPService(); Message msg = xmpp.parseMessage(req); // The "JID" is the unique ID of this chat client, which we use to subscribe JID fromJid = msg.getFromJid(); String body = msg.getBody(); // Unsubscribe, if requested if (body.startsWith("STOP")) { MultichannelChatManager.removeSub(fromJid.getId()); } else { // If they aren't subscribed, subscribe them if (!MultichannelChatManager.isSubscribed(fromJid.getId())) { MultichannelChatManager.addSub(fromJid.getId()); } MultichannelChatManager.sendMessage(body, "xmpp"); } } }
JID jid = new JID(sub); Message msg = new MessageBuilder().withRecipientJids(jid).withBody(messageBody).build(); XMPPService xmpp = XMPPServiceFactory.getXMPPService(); xmpp.sendMessage(msg);
//Generate a client token for the GAE Channel API public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { ChannelService channelService = ChannelServiceFactory.getChannelService(); //The token must be based on some unique identifier for the client - I am using the current time //for demo purposes... String clientId = String.valueOf(System.currentTimeMillis()); String token = channelService.createChannel(clientId); //Subscribe this client MultichannelChatManager.addSub(clientId); //Reply with the token res.setContentType("text/plain"); res.getWriter().print(token); }
//Get a client token to use with the channel API $.ajax('/chat',{ method:'GET', dataType:'text', success: function(token) { console.log(token); var channel = new goog.appengine.Channel(token); var socket = channel.open(); //Assign our handler function to the open socket socket.onmessage = onMessage; } });
ChannelService channelService = ChannelServiceFactory.getChannelService(); channelService.sendMessage(new ChannelMessage(sub,messageBody));
Demonstrate your proficiency to design, build and manage solutions on Google Cloud Platform.