WhatsAPP - MS Teams integration via Twilio - twilio

I would like to integrate WhatsApp business to MS Teams. When I send a message via WhatsApp it is received in Twilio. I set webhook in Teams and in Twilio also, but Twilio can't forward the message to the Teams.
The Twilio give me a
11200 ERROR There was a failure attempting to retrieve the contents of this URL.
I checked the response of Teams and I found this in the body:
"Bad payload received by generic incoming webhook."
I tested the webhook via curl and I received the text in channel of Teams.

Twilio developer evangelist here.
From the MS Teams documentation (emphasis mine):
If Incoming Webhooks are enabled for a team in any channel, it exposes the HTTPS endpoint, which accepts correctly formatted JSON and inserts the messages into that channel.
Twilio webhooks are sent in the format application/x-www-form-urlencoded, so you will need something in the middle to reformat the Twilio webhook into a format that MS Teams can ingest.
From a quick search, it's a bit difficult to find a reference for what JSON that MS Teams actually expects. This page has some examples.
To do the reformatting, you could use a Twilio Function. Code like this might well work for a basic text message into Teams:
const got = require("got");
exports.handler = async function (context, event, callback) {
const teamsWebhookUrl = context.TEAMS_WEBHOOK_URL;
const teamsPayload = {
text: event.Body
};
try {
await got(teamsWebhookUrl, {
method: "POST",
body: JSON.stringify(teamsPayload),
headers: {
"Content-Type": "application/json"
}
);
const response = new Twilio.twiml.MessagingResponse();
callback(null, response);
} catch(error) {
callback(error);
}
}
This is untested, but the idea is that it builds a simple text message using the JSON from this curl example and send it to the Teams webhook URL using got. If there is a successful response from Teams then an empty response is sent back to the original Twilio webhook. If there is an error, then that error is logged in the Twilio debugger.
To use this code you will need to install got in the dependencies and add the Teams webhook URL in the environment variables.

Related

Output from Studio Flow via API not being sent to website callback

We are trying to implement a chatbot on our website.
My code successfully triggers the Flow. The Conversations log on Twilio shows that my code sent a message of "Hi" and the Flow triggered and sent the expected greeting.
The problem is that I'm not seeing anyplace where the Flow output is being sent to my website callback and so I'm not able to output the Flow messages to my website user.
When the Flow sends a message, where is the configuration that makes a callback to my website so I can output the message to the user?
onMessageAdded DOES get called on my website callback, but only for messages sent by the website code - not the Flow.
At this point I think the problem is a Twilio configuration for Conversations, Messages or the Flow, but it could be a configuration problem in my code.
Here is my rough initial code:
TwilioClient.Init(_twilioAccountSid, _twilioAuthToken);
//
// Create Conversation
var conversation = ConversationResource.Create(
friendlyName: "Test conversation",
messagingServiceSid: _twilioMessagingServiceSid,
attributes: null,
xTwilioWebhookEnabled: ConversationResource.WebhookEnabledTypeEnum.True
);
_log.Info("Conversation.Create: " + conversation.Sid);
//
// Attach Flow to Conversation
var webhook = WebhookResource.Create(
configurationMethod: WebhookResource.MethodEnum.Post,
configurationFlowSid: _twilioStudioFlowSid,
target: WebhookResource.TargetEnum.Studio,
configurationFilters: new List<string> {
"onMessageAdded",
"onMessageUpdated",
"onMessageRemoved",
"onConversationUpdated",
"onConversationRemoved",
"onParticipantAdded",
"onParticipantUpdated",
"onParticipantRemoved"
},
pathConversationSid: conversation.Sid
);
_log.Info("WebhookResource.Create: " + webhook.Sid);
//
// Create a Participant
var participant = ParticipantResource.Create(
identity: _identity,
pathConversationSid: conversation.Sid
);
_log.Info("Participant.Create: " + participant.Sid);
//
// Send Message
var message = MessageResource.Create(
author: _identity,
body: "Hi!",
xTwilioWebhookEnabled: MessageResource.WebhookEnabledTypeEnum.True,
pathConversationSid: conversation.Sid
);
_log.Info("Message.Create: " + message.Sid);
Is there a reason you decided to not use the Twilio Conversations SDK for JavaScript?
The architecture you are using may require this additional configuration.
Triggering Webhooks for REST API Events
Upon configuration, only actions from SDK-driven clients (like mobile phones or browsers) or SMS-based Participants will cause webhooks without further action on your part. This includes both Service-level webhooks and Conversation-Scoped Webhooks. This is a default behavior to help avoid infinite feedback loops.
Your Post-Event Webhook target, however, may be an important tool for archiving. In this case, you may also want to enable webhook "echoes" from actions you take on the REST API. To do so, you can add a header X-Twilio-Webhook-Enabled=true to any such request. Requests bearing this header will yield webhooks to the configured Post-Event webhook target.
Troubleshooting Webhook Delivery for Conversations or Chat
I don’t think there is a way to set this header when using Twilio Studio widgets.

Push Twilio alerts to Slack Channel directly

I am not sure that there is way but can I send Twilio alerts from Twilio Console to Slack channel directly without using Python or any web framework that will listen to Twilio requests and send the data to Slack channel?
Using Twilio console, I can trigger a webhook from there once there is a new error but the Slack requires a data in the API.
Thank you
Twilio developer evangelist here.
It looks as though Slack expects webhooks to be in JSON format in a particular format, in the simplest form an object with a text property.
Twilio webhooks are sent in application/x-www-form-urlencoded format with the parameters listed here.
In order to turn this into a webhook that Slack will understand you will need some code or service that will translate the form encoded request into a JSON request with the right fields.
It seems you are reticent to build and host something yourself. If the hosting is the problem, can I suggest you look into Twilio Functions to build this. Twilio Functions lets you host JavaScript functions that can respond to incoming HTTP requests.
An example of a Twilio Function that could translate these alert webhooks into a Slack webhook might look like this:
const got = require('got');
exports.handler = async function (context, event, callback) {
const slackUrl = context.SLACK_URL;
const { ErrorCode, Description, AccountSid } = event;
const message = `New error for Twilio Account ${AccountSid}.\n\n${ErrorCode}: ${Description}`
try {
await got(slackUrl, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ text: message })
})
callback(null, "<Response/>");
} catch(error) {
callback(error);
}
}
The above code is untested, but should give you a good a start. It uses got to make the HTTP request to the Slack webhook URL. In this case, the webhook URL is stored in an environment variable.

Can I generate a Twilio SMS when a new email is received (email -> SMS)?

I am trying to generate an SMS from Twilio when I receive a new email in my Gmail.
I need to find a way to forward the email to another email address and have that generate the call to Twilio to send the SMS.
Here is a thought!
You could setup an webhook that will post your email data using one of these services:
Services That Cost:
Either
https://automate.io/integration/gmail/webhooks
The Widget you would want to use.
OR
https://zapier.com/apps/gmail/integrations/webhook
Free Service:
This is a bit of a hack, but it is free as far as I can tell:
Forward all Emails To A Slack Channel: https://slack.com/help/articles/206819278-send-emails-to-slack
Create a slack app that listens for new messages, and when it recieves a message, send a post request to a Twilio Function... Instructions below.... Link to slack app building: https://api.slack.com/start/building
THEN: you would post the data to a twilio function that takes the data and sends and SMS with it. the basic started application to send a message in a twilio function looks like this:
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.MessagingResponse();
twiml.message("Hello World");
callback(null, twiml);
};
the event parameter will contain any data that you post to it. as this twilio doc shows:
Here is the twilio documentation for 'function' data posting
if your event looks like this: {"message-content": "Hey Jim I just wanted to send you this fantastic email over the holidays"}, all you would then need to do is as follows:
exports.handler = function(context, event, callback) {
context.getTwilioClient().messages.create({
to: '<ENTER YOUR PHONE NUMBER HERE>',
from: '<ENTER ONE OF YOUR TWILIO PHONE NUMBERS WITH SMS CAPABILITIES>',
body: event.message-content
}).then(msg => {
callback(null, msg.sid);
}).catch(err => callback(err));
};
If you have any questions or get stuck along the way, use this twilio doc to help you: https://www.twilio.com/docs/runtime/quickstart/programmable-sms-functions
Cheers!!

Twilio modify sms message before forwarding using TWIML

Trying to modify a SMS message (adding a name based on the from phone number) before forwarding the message to a phone using TWIML. The phone no list is small so I will use a switch statement I am guessing in a function. I am not sure how can I wire this together w/o my own server and just using Twilio hosted stuff (TWIML, function, ?)?
Twilio developer evangelist here.
You can absolutely modify the message before forwarding it on.
If you're looking to do so without using your own server, then Twilio Functions is your best bet. Twilio Functions gives you access to a Node.js environment in which you can write functions that respond to webhooks.
To forward a message to a number but add a name based on the incoming number, you can do something like this in a function:
contacts = {
"number": "name"
}
exports.handler = function(context, event, callback) {
const name = contacts[event.From];
if (typeof name !== 'undefined') {
const message = `${name} said: ${event.Body}`;
const response = new Twilio.twiml.MessagingResponse();
response.Message({ to: YOUR_NUMBER, from: YOUR_TWILIO_NUMBER }, message);
callback(null, response);
} else {
// handle not having a name in the contacts
}
}
Check out this quick start on using Twilio Functions for more detail.

Re-send a Twilio SMS Message

I store sent messages in a log table with their unique SIDs. With a periodic console task I iterate over the records having status = undelivered and request the status of it and if it's still undelivered, I'd like to re-send that very message. I don't want a new one as the message contains a verification code and we store only hash of it. Is it possible to re-send the old message having its SID?
Twilio Developer Evangelist here,
There is not a way to automatically resend an undelivered message using the API. You can work around this by grabbing the messages by SID and sending the undelivered ones again to the same number with the same body. When you use the REST API to get a message by SID, you have access to all of the data from that message, including the exact message body.
A quick example using the Twilio PHP Library would look like this:
<?php
$client = new Services_Twilio($AccountSid, $AuthToken);
$messageSIDs = array("Insert", "Array of", "Message SIDs", "here");
foreach ($messageSIDs as $sid) {
$msg = $client->account->messages->get($sid);
if ($msg->status == "undelivered") {
echo "Resending undelivered message for SID: $sid\n";
$sms = $client->account->messages->sendMessage(
// The number we are sending from.
$msg->from,
// The number we are sending to.
$msg->to,
// The sms body.
$msg->body
);
}
}
You can see all of the data that the REST API gives you about messages here

Resources