示例#1
0
  /**
   * This method takes in a String and converts it into a TwiMl Response which can be sent to the
   * caller
   *
   * @param fizzBuzz {@link String} that needs to sent as Response
   * @return {@link TwiMLResponse} converted to XML-string
   * @throws TwiMLException
   */
  public static String getTwiMlResponseFromString(String str) throws TwiMLException {
    TwiMLResponse twiml = new TwiMLResponse();
    try {
      Say say = new Say(str);
      say.setVoice("man");
      twiml.append(say);

    } catch (TwiMLException e) {
      Logger.error("Exception Occured while generating TwmiMl from string :" + str + " : " + e);
      throw new TwiMLException(
          "Exception Occured while generating TwmiMl from string :" + str + " : " + e);
    }
    return twiml.toXML();
  }
示例#2
0
  public static void main(String[] args) {

    ArrayList<People> peoples = new ArrayList<People>();

    TwilioRestClient client =
        new TwilioRestClient(
            "AC24dcb8ff451fefca20dcf24db7169cc6",
            "1232ab47c4eb8674dffe239456c4c582"); // replace this
    Account mainAccount = client.getAccount();

    staticFileLocation("/public"); // Static files

    get(
        "/name",
        (req, res) -> {
          String name = req.queryParams("foo");
          return "Hello, " + name;
        });

    get(
        "/form",
        (req, res) -> {
          String delay = req.queryParams("delay");
          int delayInt = Integer.parseInt(delay);

          String phoneNum = req.queryParams("phoneNum");
          String questions = req.queryParams("questions");

          peoples.add(new People(phoneNum, delayInt, questions));

          return "Message Sent :)";
        });

    post(
        "/sms",
        (req, res) -> {
          TwiMLResponse twiml = new TwiMLResponse();

          twiml.append(new Message("Notes Stopped"));

          return twiml.toXML();
        });
  }
示例#3
0
  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();
        });
  }