Skip to content

Instantly share code, notes, and snippets.

@manniru
Forked from kwhinnery/TwilioServlet.java
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save manniru/b39eb23eeeee85e917b3 to your computer and use it in GitHub Desktop.

Select an option

Save manniru/b39eb23eeeee85e917b3 to your computer and use it in GitHub Desktop.
package com.twilio;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import com.twilio.sdk.resource.instance.Account;
import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.factory.SmsFactory;
import com.twilio.sdk.resource.instance.Sms;
public class TwilioServlet extends HttpServlet {
/* Find your sid and token at twilio.com/user/account */
public static final String ACCOUNT_SID = "AC123";
public static final String AUTH_TOKEN = "456bef";
//Handle an incoming HTTP Request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
//Create a Twilio REST client
TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
Account account = client.getAccount();
//Use the API to send a text message
SmsFactory smsFactory = account.getSmsFactory();
Map<String, String> smsParams = new HashMap<String, String>();
smsParams.put("To", "+14105551234");
smsParams.put("From", "(410) 555-6789"); // Replace with a Twilio phone number in your account
smsParams.put("Body", "Where's Wallace?");
Sms sms = smsFactory.create(smsParams);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment