예제 #1
0
  /**
   * Example of making an outgoing call.
   *
   * @param client authenticated twilio client object
   * @param from the caller id of the phone call
   * @param to the phone number to be called
   * @param url the URL to execute when the called party answers
   */
  private static void makeCallExample(TwilioRestClient client, String from, String to, String url) {

    // build map of post parameters
    Map<String, String> params = new HashMap<String, String>();
    params.put("From", from);
    params.put("To", to);
    params.put("Url", url);
    TwilioRestResponse response;
    try {
      response =
          client.request(
              "/" + APIVERSION + "/Accounts/" + client.getAccountSid() + "/Calls",
              HttpMethod.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();
    }
  }
예제 #2
0
  /**
   * Example of deleting a recording from your twilio account
   *
   * @param client authenticated twilio client object
   * @param RecordingSid the twilio Recording Id String you wish to delete
   */
  private static void deleteRecordingsExample(TwilioRestClient client, String RecordingSid) {
    TwilioRestResponse response;
    try {
      response =
          client.request(
              "/"
                  + APIVERSION
                  + "/Accounts/"
                  + client.getAccountSid()
                  + "/Recordings/"
                  + RecordingSid,
              HttpMethod.GET,
              null);

      if (response.isError())
        System.out.println(
            "Error deleting recording: "
                + response.getHttpStatus()
                + "\n"
                + response.getResponseText());
      else {
        System.out.println(response.getResponseText());
      }
    } catch (TwilioRestException e) {
      e.printStackTrace();
    }
  }
예제 #3
0
  /**
   * Example of retrieving the Recordings for an account, filtered by call id
   *
   * @param client authenticated twilio client object
   * @param CallSid the twilio call ID string
   */
  private static void getRecordingsExample(TwilioRestClient client, String CallSid) {

    // build map of parameters
    Map<String, String> params = new HashMap<String, String>();
    params.put("CallSid", CallSid);

    TwilioRestResponse response;
    try {
      response =
          client.request(
              "/" + APIVERSION + "/Accounts/" + client.getAccountSid() + "/Recordings",
              HttpMethod.GET,
              params);

      if (response.isError())
        System.out.println(
            "Error fetching recordings: "
                + response.getHttpStatus()
                + "\n"
                + response.getResponseText());
      else {
        System.out.println(response.getResponseText());
      }
    } catch (TwilioRestException e) {
      e.printStackTrace();
    }
  }
예제 #4
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();
   }
 }
예제 #5
0
  /**
   * Perform a GET request against the given fully qualified uri. This is a shortcut to {@link
   * #request(String, String, Map)} with method "GET" and no parameters
   *
   * @param fullUri The full uri, including protocol://hostname/path
   * @return {@link TwilioRestResponse} the response from the query
   * @throws TwilioRestException the twilio rest exception
   */
  public TwilioRestResponse get(final String fullUri) throws TwilioRestException {
    TwilioRestResponse response = null;

    for (int retry = 0; retry < numRetries; retry++) {
      response = request(fullUri, "GET", (Map) null);
      if (response.isClientError()) {
        throw TwilioRestException.parseResponse(response);
      } else if (response.isServerError()) {
        try {
          Thread.sleep(100 * retry); // Backoff on our sleep
        } catch (final InterruptedException e) {
        }
        continue;
      }

      return response;
    }
    int errorCode = response == null ? -1 : response.getHttpStatus();
    throw new TwilioRestException("Cannot fetch: " + fullUri + " ", errorCode);
  }
예제 #6
0
  /**
   * Make a request, handles retries + back-off for server/network errors
   *
   * @param path the URL (absolute w.r.t. the endpoint URL - i.e. /2010-04-01/Accounts)
   * @param method the HTTP method to use, defaults to GET
   * @param paramList for POST or PUT, a list of data to send, for GET will be appended to the URL
   *     as querystring params
   * @return The response
   * @throws TwilioRestException if there's an client exception returned by the TwilioApi
   */
  public TwilioRestResponse safeRequest(
      final String path, final String method, final List<NameValuePair> paramList)
      throws TwilioRestException {

    TwilioRestResponse response = null;
    for (int retry = 0; retry < numRetries; retry++) {
      response = request(path, method, paramList);
      if (response.isClientError()) {
        throw TwilioRestException.parseResponse(response);
      } else if (response.isServerError()) {
        try {
          Thread.sleep(100 * retry); // Backoff on our sleep
        } catch (final InterruptedException e) {
        }
        continue;
      }

      return response;
    }
    int errorCode = response == null ? -1 : response.getHttpStatus();
    throw new TwilioRestException("Cannot fetch: " + method + " " + path, errorCode);
  }
예제 #7
0
  /**
   * Example of retrieving recent error and warning notifications from your account
   *
   * @param client authenticated twilio client object
   */
  private static void getNotificationsExample(TwilioRestClient client) {
    TwilioRestResponse response;
    try {
      response =
          client.request(
              "/" + APIVERSION + "/Accounts/" + client.getAccountSid() + "/Notifications",
              HttpMethod.GET,
              null);

      if (response.isError())
        System.out.println(
            "Error fetching recent notifications: "
                + response.getHttpStatus()
                + "\n"
                + response.getResponseText());
      else {
        System.out.println(response.getResponseText());
      }
    } catch (TwilioRestException e) {
      e.printStackTrace();
    }
  }
예제 #8
0
  public TwilioRestResponse request(
      final String path, final String method, final List<NameValuePair> paramList)
      throws TwilioRestException {

    HttpUriRequest request = setupRequest(path, method, paramList);

    HttpResponse response;
    try {
      response = httpclient.execute(request);
      HttpEntity entity = response.getEntity();

      Header[] contentTypeHeaders = response.getHeaders("Content-Type");
      String responseBody = "";

      if (entity != null) {
        responseBody = EntityUtils.toString(entity);
      }

      StatusLine status = response.getStatusLine();
      int statusCode = status.getStatusCode();

      TwilioRestResponse restResponse =
          new TwilioRestResponse(request.getURI().toString(), responseBody, statusCode);

      // For now we only set the first content type seen
      for (final Header h : contentTypeHeaders) {
        restResponse.setContentType(h.getValue());
        break;
      }

      return restResponse;

    } catch (final ClientProtocolException e1) {
      throw new RuntimeException(e1);
    } catch (final IOException e1) {
      throw new RuntimeException(e1);
    }
  }