Search in sources :

Example 1 with TwilioRestException

use of com.twilio.sdk.TwilioRestException in project java-docs-samples by GoogleCloudPlatform.

the class SendSmsServlet method service.

@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    final String twilioAccountSid = System.getenv("TWILIO_ACCOUNT_SID");
    final String twilioAuthToken = System.getenv("TWILIO_AUTH_TOKEN");
    final String twilioNumber = System.getenv("TWILIO_NUMBER");
    final String toNumber = (String) req.getParameter("to");
    if (toNumber == null) {
        resp.getWriter().print("Please provide the number to message in the \"to\" query string parameter.");
        return;
    }
    TwilioRestClient client = new TwilioRestClient(twilioAccountSid, twilioAuthToken);
    Account account = client.getAccount();
    MessageFactory messageFactory = account.getMessageFactory();
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("To", toNumber));
    params.add(new BasicNameValuePair("From", twilioNumber));
    params.add(new BasicNameValuePair("Body", "Hello from Twilio!"));
    try {
        Message sms = messageFactory.create(params);
        resp.getWriter().print(sms.getBody());
    } catch (TwilioRestException e) {
        throw new ServletException("Twilio error", e);
    }
}
Also used : ServletException(javax.servlet.ServletException) Account(com.twilio.sdk.resource.instance.Account) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) MessageFactory(com.twilio.sdk.resource.factory.MessageFactory) Message(com.twilio.sdk.resource.instance.Message) TwilioRestException(com.twilio.sdk.TwilioRestException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) TwilioRestClient(com.twilio.sdk.TwilioRestClient)

Example 2 with TwilioRestException

use of com.twilio.sdk.TwilioRestException in project java-docs-samples by GoogleCloudPlatform.

the class SendSmsServlet method service.

@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    final String twilioAccountSid = System.getenv("TWILIO_ACCOUNT_SID");
    final String twilioAuthToken = System.getenv("TWILIO_AUTH_TOKEN");
    final String twilioNumber = System.getenv("TWILIO_NUMBER");
    final String toNumber = (String) req.getParameter("to");
    if (toNumber == null) {
        resp.getWriter().print("Please provide the number to message in the \"to\" query string parameter.");
        return;
    }
    TwilioRestClient client = new TwilioRestClient(twilioAccountSid, twilioAuthToken);
    Account account = client.getAccount();
    MessageFactory messageFactory = account.getMessageFactory();
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("To", toNumber));
    params.add(new BasicNameValuePair("From", twilioNumber));
    params.add(new BasicNameValuePair("Body", "Hello from Twilio!"));
    try {
        Message sms = messageFactory.create(params);
        resp.getWriter().print(sms.getBody());
    } catch (TwilioRestException e) {
        throw new ServletException("Twilio error", e);
    }
}
Also used : ServletException(javax.servlet.ServletException) Account(com.twilio.sdk.resource.instance.Account) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) MessageFactory(com.twilio.sdk.resource.factory.MessageFactory) Message(com.twilio.sdk.resource.instance.Message) TwilioRestException(com.twilio.sdk.TwilioRestException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) TwilioRestClient(com.twilio.sdk.TwilioRestClient)

Aggregations

TwilioRestClient (com.twilio.sdk.TwilioRestClient)2 TwilioRestException (com.twilio.sdk.TwilioRestException)2 MessageFactory (com.twilio.sdk.resource.factory.MessageFactory)2 Account (com.twilio.sdk.resource.instance.Account)2 Message (com.twilio.sdk.resource.instance.Message)2 ArrayList (java.util.ArrayList)2 ServletException (javax.servlet.ServletException)2 NameValuePair (org.apache.http.NameValuePair)2 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)2