Search in sources :

Example 1 with Message

use of com.twilio.sdk.resource.instance.Message in project cryptonomica by Cryptonomica.

the class OnlineVerificationAPI method sendTestSms.

// end: getDocumentsUploadKey
/* --- Test SMS service: */
@ApiMethod(name = "sendTestSms", path = "sendTestSms", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public StringWrapperObject sendTestSms(// final HttpServletRequest httpServletRequest,
final User googleUser, @Named("phoneNumber") final String phoneNumber, @Named("smsMessage") final String smsMessage) throws // see: https://cloud.google.com/appengine/docs/java/endpoints/exceptions
UnauthorizedException, BadRequestException, NotFoundException, NumberParseException, IllegalArgumentException, TwilioRestException {
    /* --- Check authorization: */
    CryptonomicaUser cryptonomicaUser = UserTools.ensureCryptonomicaOfficer(googleUser);
    /* --- Send SMS */
    Message message = TwilioUtils.sendSms(phoneNumber, smsMessage);
    return new StringWrapperObject(message.toJSON());
}
Also used : Message(com.twilio.sdk.resource.instance.Message) StringWrapperObject(net.cryptonomica.returns.StringWrapperObject) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 2 with Message

use of com.twilio.sdk.resource.instance.Message in project cryptonomica by Cryptonomica.

the class OnlineVerificationAPI method sendSms.

// end of sendTestSms();
/* --- Send SMS : */
@ApiMethod(name = "sendSms", path = "sendSms", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public StringWrapperObject sendSms(// final HttpServletRequest httpServletRequest,
final User googleUser, @Named("phoneNumber") final String phoneNumber, // in international format, f.e. +972523333333
@Named("fingerprint") final String fingerprint) throws // see: https://cloud.google.com/appengine/docs/java/endpoints/exceptions
UnauthorizedException, BadRequestException, NotFoundException, NumberParseException, IllegalArgumentException, TwilioRestException {
    /* --- Check authorization: */
    CryptonomicaUser cryptonomicaUser = UserTools.ensureCryptonomicaRegisteredUser(googleUser);
    // --- create SMS:
    String smsMessage = RandomStringUtils.randomNumeric(7);
    LOG.warning("smsMessage: " + smsMessage);
    // --- store SMS:
    PhoneVerification phoneVerification = null;
    phoneVerification = ofy().load().key(Key.create(PhoneVerification.class, fingerprint)).now();
    if (phoneVerification == null) {
        phoneVerification = new PhoneVerification(fingerprint);
    }
    if (phoneVerification.getVerified()) {
        throw new BadRequestException("Phone already verified for this OpenPGP public key " + fingerprint);
    }
    phoneVerification.setPhoneNumber(phoneNumber);
    phoneVerification.setUserEmail(cryptonomicaUser.getEmail());
    phoneVerification.setSmsMessage(smsMessage);
    phoneVerification.setFailedVerificationAttemps(0);
    phoneVerification.setSmsMessageSend(new Date());
    LOG.warning(GSON.toJson(phoneVerification));
    /* --- Send SMS */
    Message message = TwilioUtils.sendSms(phoneNumber, smsMessage);
    LOG.warning(message.toJSON());
    /* --- Save phoneVerification */
    ofy().save().entity(phoneVerification).now();
    return new StringWrapperObject("SMS message send successfully");
}
Also used : Message(com.twilio.sdk.resource.instance.Message) StringWrapperObject(net.cryptonomica.returns.StringWrapperObject) BadRequestException(com.google.api.server.spi.response.BadRequestException) Date(java.util.Date) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 3 with Message

use of com.twilio.sdk.resource.instance.Message 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;
}
Also used : 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) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) TwilioRestClient(com.twilio.sdk.TwilioRestClient)

Example 4 with Message

use of com.twilio.sdk.resource.instance.Message 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 5 with Message

use of com.twilio.sdk.resource.instance.Message 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

Message (com.twilio.sdk.resource.instance.Message)5 TwilioRestClient (com.twilio.sdk.TwilioRestClient)3 MessageFactory (com.twilio.sdk.resource.factory.MessageFactory)3 Account (com.twilio.sdk.resource.instance.Account)3 ArrayList (java.util.ArrayList)3 NameValuePair (org.apache.http.NameValuePair)3 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)3 ApiMethod (com.google.api.server.spi.config.ApiMethod)2 TwilioRestException (com.twilio.sdk.TwilioRestException)2 ServletException (javax.servlet.ServletException)2 StringWrapperObject (net.cryptonomica.returns.StringWrapperObject)2 BadRequestException (com.google.api.server.spi.response.BadRequestException)1 Date (java.util.Date)1