示例#1
0
  private static boolean callTo(DirectCaller caller) {
    String callFrom = caller.callFrom;
    String callTo = caller.callTo;

    try {

      // Get the account and call factory class
      Account acct = client.getAccount();
      CallFactory callFactory = acct.getCallFactory();

      // build map of post parameters
      Map<String, String> params = new HashMap<String, String>();
      String url = "http://e4fa2e.ngrok.com/v1/twilio/directCall/" + caller.fizzBuzzInput;
      Logger.info("Calling API " + url);
      params.put("From", callFrom);
      params.put("To", callTo);
      params.put("Url", url);
      params.put("Method", "GET");

      // Make a phone call ( This makes a POST request to the Calls
      // resource)
      callFactory.create(params);
    } catch (TwilioRestException e) {
      Logger.error(e.getErrorMessage());
      return false;
    }
    Logger.info("Calling Directly..");
    return true;
  }
示例#2
0
 public static void main(String[] args) {
   /* Twilio AccountSid and AuthToken */
   String AccountSid = "AC31f00612c5c6f553a3f4a089e5012531";
   String AuthToken = "dc40be9bcfdbc7677e5fff3ddbaf68ca";
   /* Outgoing Caller ID previously validated with Twilio */
   String CallerID = "+919173365243";
   String ToCall = "+919471281973";
   String Url =
       "http://twimlets.com/message?Message%5B0%5D=Hello%20from%20my%20java%20application.&Message%5B1%5D=http%3A%2F%2Fcom.twilio.music.electronica.s3.amazonaws.com%2Fteru_-_110_Downtempo_Electronic_4.mp3";
   /* Instantiate a new Twilio Rest Client */
   TwilioRestClient client = new TwilioRestClient(AccountSid, AuthToken, null);
   // build map of post parameters
   Map params = new HashMap();
   params.put("From", CallerID);
   params.put("To", ToCall);
   params.put("Url", Url);
   TwilioRestResponse response;
   try {
     response =
         client.request(
             "/" + APIVERSION + "/Accounts/" + client.getAccountSid() + "/Calls", "POST", params);
     if (response.isError())
       System.out.println(
           "Error making outgoing call: "
               + response.getHttpStatus()
               + "n"
               + response.getResponseText());
     else {
       System.out.println(response.getResponseText());
     }
   } catch (TwilioRestException e) {
     e.printStackTrace();
   }
 }
示例#3
0
  /**
   * Notify the user via SMS.
   *
   * @param user User who has to be notified
   * @param message The message to send to the user
   */
  public static final void notifyViaSms(User user, String message) {
    // 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", "+1" + user.getPhone());
    smsParams.put("From", TWILIO_NUMBER);
    smsParams.put("Body", message);
    try {
      smsFactory.create(smsParams);
    } catch (TwilioRestException e) {
      e.printStackTrace();
    }
  }
示例#4
0
  public static void send(String phone, String text) {

    TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

    // Build a filter for the MessageList
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("Body", text));
    boolean to = params.add(new BasicNameValuePair("To", "+" + phone));
    params.add(new BasicNameValuePair("From", "+17075496112"));

    MessageFactory messageFactory = client.getAccount().getMessageFactory();
    Message message = null;
    try {
      message = messageFactory.create(params);
    } catch (TwilioRestException e) {
      e.printStackTrace();
    }
    System.out.println(message.getSid());
  }
  @PUT
  @Path("{title}/{body}/{isDone}")
  @Produces(value = "text/plain")
  @Consumes(MediaType.TEXT_PLAIN)
  public Response toggleStatusListItem(
      @PathParam("title") String title,
      @PathParam("body") String body,
      @PathParam("isDone") boolean isDone) {
    // Find your Account Sid and Token at twilio.com/user/account
    ListItem item = new ListItem(title, body, isDone);
    boolean done = isDone ? false : true;
    int index = list.indexOf(item);
    item.setDone(done);
    list.remove(index);
    list.add(index, item);

    if (isDone == Boolean.FALSE) {

      String ACCOUNT_SID = "AC96d0a9024203de2a4177624c789fad17";
      String AUTH_TOKEN = "919a89967cc484a35a1a0cfd0555dd57";

      TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
      try {
        // Build a filter for the MessageList
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("Body", "Task : " + body + "is complete."));
        params.add(new BasicNameValuePair("To", "+14086270378"));
        params.add(new BasicNameValuePair("From", "+18316847487"));

        MessageFactory messageFactory = client.getAccount().getMessageFactory();
        Message message = messageFactory.create(params);
        System.out.println(message.getSid());
        return Response.status(200).entity(body.toString()).build();
      } catch (TwilioRestException e) {
        System.out.println(e.getErrorMessage());
      }
    }
    return Response.status(500).entity(body.toString()).build();
  }
示例#6
0
  @Override
  public void sendSms(SmsDto smsDto) {

    try {

      TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

      Account account = client.getAccount();

      MessageFactory messageFactory = account.getMessageFactory();
      List<NameValuePair> params = new ArrayList<NameValuePair>();
      params.add(new BasicNameValuePair("To", smsDto.getTo()));
      params.add(new BasicNameValuePair("From", FROM_PHONE));
      params.add(new BasicNameValuePair("Body", SMS_BODY));
      Message sms = messageFactory.create(params);

      log.debug("Sent SMS: " + sms.getBody() + "to: " + sms.getTo());

    } catch (TwilioRestException ex) {
      log.debug(ex.toString());
    }
  }