I am looking for a way to say 'Thank you' and also make an API call at the end of function execution in Twilio.
Something like this:
responseObject = {
"actions": [
{
"say": "Thank you!"
},
{
"redirect": {
"uri": "API_LINK",
"method": "POST"
}
}
]
}
Sadly, twilio ignores every other message if you have a redirect. I tried to solve this by redirecting to TwiML first:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>Thank you!</Say>
<Redirect method="POST">API_LINK</Redirect>
</Response>
But the above also does not work - It says application error after 'Thank you!' at the end. Also, I am passing query parameters along with API call: https://example.link?a=value&b=value. Not sure, how to pass that with TwiML.
This API request is supposed to send message to microsoft teams channel.
Twilio developer evangelist here.
To start with, it seems like you are talking about the end of an Autopilot dialogue, not the end of function execution. If that is the case, you are getting errors because you are mixing Autopilot Actions and TwiML. When you are interacting with an Autopilot assistant you should only respond to it with Actions JSON, not with TwiML.
Also, Autopilot will expect all responses to requests that it makes, including via redirect, to respond with Actions JSON. So it's not recommended to make API requests using a redirect action.
Instead, I would recommend that you use a Twilio Function (or your own back-end) to make your API request from JavaScript and respond with the "say" action.
A Twilio Function might look something like this:
exports.handler = function (context, event, callback) {
// make request to Teams API
// I'm not sure the API method you are using, but use an http client like got, superagent or node-fetch, or a dedicated API client if there is one available
// create your actions
const actions = {
actions: [
{
"say": "Thank you!"
}
]
};
// return the actions JSON
callback(null, actions);
});
Alternatively, you can set up to receive an Autopilot webhook when a dialogue ends. This way you can respond to Autopilot with just the "say" action which will cause the end of the dialogue and trigger the webhook. Then in your webhook handler you can make the API request to Teams.
Let me know if that helps at all.