use of com.twilio.sdk.resource.factory.MessageFactory in project cryptonomica by Cryptonomica.
the class TwilioUtils method sendSms.
public static Message sendSms(final String phoneNumber, final String smsMessage) throws NumberParseException, IllegalArgumentException, TwilioRestException {
checkPhoneNumber(phoneNumber);
checkSmsMessage(smsMessage);
final String accountSid = ApiKeysUtils.getApiKey("twilioAccountSid");
final String authToken = ApiKeysUtils.getApiKey("twilioAuthToken");
final String twilioPhoneNumber = ApiKeysUtils.getApiKey("twilioPhoneNumber");
// Twilio.init(accountSid, authToken);
// Message message = Message.creator(
// new PhoneNumber(phoneNumber), // To number
// new PhoneNumber(twilioPhoneNumber), // From number
// smsMessage // SMS body
// ).create();
// LOG.warning(message.toString());
TwilioRestClient client = new TwilioRestClient(accountSid, authToken);
Account account = client.getAccount();
MessageFactory messageFactory = account.getMessageFactory();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("To", phoneNumber));
params.add(new BasicNameValuePair("From", twilioPhoneNumber));
params.add(new BasicNameValuePair("Body", smsMessage));
Message sms = messageFactory.create(params);
LOG.warning(sms.toString());
return sms;
}
use of com.twilio.sdk.resource.factory.MessageFactory 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);
}
}
use of com.twilio.sdk.resource.factory.MessageFactory 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);
}
}
Aggregations