serverfaces
serverfaces

Reputation: 1175

Twilio Welcome Greeting without Say

Is there a way to add a customized text when I call someone without having the "url" field specified?

public void call() {

        TwilioRestClient client = 
                 new TwilioRestClient("******", "******");
           Account myAccount = client.getAccount();

           CallFactory callFactory = myAccount.getCallFactory();
            Map<String, String> callParams = new HashMap<String, String>();
            callParams.put("To", "+1XXXXXXXXXXX");
            callParams.put("From", "+1MYTWILIONUMBER");
            callParams.put("Url", "https://foo.bar.com");

            try {
                callFactory.create(callParams);
            } catch (TwilioRestException e) {
                e.printStackTrace();
            }
    }

In the above code snippet, I am setting "Url" as one of the call parameters. If someone answers the call, then Twilio will invoke the "Url" endpoint to read a message or something.

Let's say the message is simple. Can I set the message as part of the call request itself to avoid a call from Twilio to my server side to get the message?

Something like:

 callParams.put("Message", "Hello, Foo.");

When someone answers the call, I would like the message to be used.

Upvotes: 0

Views: 191

Answers (2)

philnash
philnash

Reputation: 73029

Twilio developer evangelist here. (Hi Megan!)

I'm afraid there is no simple way to play a message in the manner you are asking. All Twilio calls are handled the same, by making a callback to the URL you provide to find out what to do next.

However, you don't need to provide anything complicated as that URL. It can be a simple static XML file with the <Say> element in hosted as a static file somewhere.

You could also use our labs feature, Twimlets which allows you to set up simple call flows, including a message using <Say>.

It's not as easy as just passing the message in the initial API call, but it allows you to easily extend your call to something more complicated without changing that initial code. I think that trade off is worth it in the long term.

Upvotes: 1

Megan Speir
Megan Speir

Reputation: 3811

I work with the Developer Community at Twilio. In order to do what you want you must return valid Twiml. So it could look something like this:

    // Create a TwiML response and add our friendly message.
    TwiMLResponse twiml = new TwiMLResponse();
    Say say = new Say("Hello Monkey");
    try {
        twiml.append(say);
    } catch (TwiMLException e) {
        e.printStackTrace();
    }

    response.setContentType("application/xml");
    response.getWriter().print(twiml.toXML());
}

Upvotes: 1

Related Questions