Example #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;
  }
  public static void main(String[] args) {
    /*get("/", (req, res) -> {
    String name = req.queryParams("name");
    return "Hello " + name;*/
    // get("/", (req, res) -> "Hello, World!");

    TwilioRestClient client =
        new TwilioRestClient(
            "AC337f1c5c266c5eeb86d8cfd6c8bf8eef",
            "fa33c9037bcc66b0099b53e2741de382"); // replace this
    Account mainAccount = client.getAccount();

    post(
        "/sms",
        (req, res) -> {
          String ngrokUrl = "http://dbde8329.ngrok.io"; // replace this
          String body = req.queryParams("Body");
          String to = req.queryParams("From");
          String from = "(610) 365-4879"; // replace this

          String uri = null;
          CallFactory callFactory = null;
          Map<String, String> callParams = new HashMap<>();

          TwiMLResponse twiml = new TwiMLResponse();
          String[] tokens = body.split(" ");
          String action = null;
          if (tokens.length >= 1) {
            action = tokens[0];
            switch (action.toLowerCase()) {
              case "play":
                uri = ngrokUrl + "/call?q=" + URLEncoder.encode(body.substring(5), "UTF-8");
                callFactory = mainAccount.getCallFactory();
                callParams = new HashMap<>();
                callParams.put("To", to);
                callParams.put("From", from);
                callParams.put("Url", uri);
                callParams.put("Method", "GET");
                callFactory.create(callParams);

                twiml.append(new Message("Your tune is on the way!"));
                res.type("text/xml");
                return twiml.toXML();
              case "translate":
                if (tokens.length < 1) {
                  // return error message
                  return twiml.toXML();
                }
                String text = body.substring((action + " ").length());
                System.out.println("translating " + text);

                LanguageTranslation service2 = new LanguageTranslation();
                service2.setUsernameAndPassword(
                    "17d780c4-46c1-4bd9-8bd9-059dc1f60913", "YdZgn3UctViZ");

                TranslationResult translationResult = service2.translate(text, "en", "es");
                String translation = translationResult.getTranslations().get(0).getTranslation();
                System.out.println(translation);
                twiml.append(new Message(translation));
                res.type("text/xml");
                return twiml.toXML();
              default:
                return twiml.toXML();
            }
          }
          return twiml.toXML();
        });

    get(
        "/call",
        (req, res) -> {
          TwiMLResponse twiml = new TwiMLResponse();

          String query = req.queryParams("q");
          String trackUrl = getTrackUrl(query);

          if (trackUrl != null) {
            twiml.append(new Play(trackUrl));
          } else {
            twiml.append(new Say("Sorry, song not found."));
          }

          res.type("text/xml");
          return twiml.toXML();
        });
  }