1

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.

Alex Baban
  • 11,312
  • 4
  • 30
  • 44

1 Answers1

1

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:

Alex Baban
  • 11,312
  • 4
  • 30
  • 44
  • Thanks for the answer Alex! We should add something like this to this repo: https://github.com/philnash/useful-twilio-functions/ – philnash Oct 03 '17 at 15:14
  • Thank you ALEX!!!!!!!! and Philnash for the inspiration made me look like a superstar at work!! Usecase: with so many 2 step verification codes in multiple accounts, setting a twilio # in the account phone number allows us to see all the 2 step verification sms codes rather than asking the owner of the Phone number. –  Oct 03 '17 at 19:23
  • I removed the reply and nothing happens when I trigger the message. Do you know what might cause this ? Normally an Amazon verification code comes in like this FROM: 262-966 625254 is your Amazon security code –  Oct 03 '17 at 21:33
  • I'm not sure, you might have to upgrade to a paid account. – Alex Baban Oct 03 '17 at 22:36
  • Turns out it was the short code, Twilio cannot receive text from short code. I ended up using voice response. your code helped a lot, and I was able to make it server less. https://stackoverflow.com/questions/46574133/send-twilio-speech-result-to-working-function –  Oct 06 '17 at 20:54