I have enabled incoming web hooks on Slack API incoming web-hooks. Webhook URLs for Your Workspace:
INSTRUCTIONS:
To dispatch messages with your webhook URL, send your message in JSON as the body of an application/json POST request.
Add this webhook to your workspace below to activate this curl example.
Sample curl request to post to a channel:
curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}' https://hooks.slack.com/services/T0AFATG95/B73FDS5R6V/hFApP
(token is incomplete).
When i send the POST request it works!
APP 2:18 PM Hello, World!
Now I want to have Twillio send this POST request to slack using a web-hook or TwiML. ??? This is basically a relay to the slack URL. how do I make a POST request to that URL, so the message sent to the twillio # is sent in the body?
Under twillio phone settings I select what happens when:
A message comes in Webhook [WEB HOOK GOES HERE] HTTP POST
OR I CAN USE:
TwiML Bin instead and assign it in the twilio phone settings.
If I cannot achieve this I will use a lambda function to do it, I just though there might be a way to achieve this since i do not manipulate anything about the message.
You can do this with a Twilio Function
, here is the code for the function:
const got = require('got');
exports.handler = function(context, event, callback) {
const requestBody = {
text: event.Body
};
got.post('https://hooks.slack.com/services/T0AFATG95/B73FDS5R6V/hFApP', {
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
})
.then(response => {
let twiml = new Twilio.twiml.MessagingResponse();
twiml.message("Your message has been forwarded to Slack.");
callback(null, twiml);
})
.catch(err => {
callback(err);
});
};
Notes:
event.Body
holds the SMS message sent to your Twilio numberif you don't want to send any response to the sender comment out or delete this line: twiml.message("Your message has been forwarded to Slack.");
also read this Building apps with Twilio Functions
https://support.twilio.com/hc/en-us/articles/115007737928-Building-apps-with-Twilio-Functions
a blog post by @philnash is the inspiration for this answer https://www.twilio.com/blog/2017/07/forward-incoming-sms-messages-to-email-with-node-js-sendgrid-and-twilio-functions.html