LewlSauce
LewlSauce

Reputation: 5872

Twilio awaits response, I don't want server to respond

I am using a Slack webhook to process incoming SMS messages from Twilio. However, the way I have it set up, It seems that Twilio is expecting the web server (slack) to respond to it. This causes errors to be generated in Twilio, and I obviously don't want errors because I'll be getting emails.

I am using the twilio-ruby gem in Ruby to send out the SMS messages, and using the slack-ruby-client to monitor incoming messages from Slack.

How do I stop Twilio from trying to expect a response from the web server when it POSTS to the Slack webhook? Is that even possible or do I have this all configured incorrectly?

EDIT

Here's the function that I have which sends the forwarded SMS to Slack:

const https = require("https");

// Make sure to declare SLACK_WEBHOOK_PATH in your Environment
// variables at
// https://www.twilio.com/console/runtime/functions/configure

exports.handler = (context, event, callback) => {
  // Extract the bits of the message we want
  const { To, From, Body } = event;

  // Construct a payload for slack's incoming webhooks
  const slackBody = JSON.stringify({
        text: `!asi SMS\nFrom: ${From}\nMessage: ${Body}`
  });

  // Form our request specification
  const options = {
    host: "hooks.slack.com",
    port: 443,
    path: context.SLACK_WEBHOOK_PATH,
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "Content-Length": slackBody.length
    }
  };

  // send the request
  const post = https.request(options, res => {
    // only respond once we're done, or Twilio's functions
    // may kill our execution before we finish.
    res.on("end", () => {
      // respond with an empty message
      callback(null, new Twilio.twiml.MessagingResponse());
    });
  });
  post.write(slackBody);
  post.end();
};

Upvotes: 0

Views: 130

Answers (1)

philnash
philnash

Reputation: 73057

Twilio developer evangelist here.

Twilio is always going to expect at least a 200 response or will timeout at 15 seconds for incoming message webhooks.

You could avoid the error messages by using something in between Twilio and Slack, like Zapier (example in this blog post) or using a Twilio Function (as described here) or with Twilio Studio (from the documentation here).

Hope one of those ideas helps!

Update

Further to my earlier answer, and given the code you used to make the call, I have an update.

When making a request using Node's built in https module you will not get the end event until you have read the data. This is what is causing the timeout between Twilio and the Twilio Function, you are never responding to it because you don't consume the data from the request.

In a quick test I found that just listening for the data event meant that the end event did fire. So update your function to:

  const post = https.request(options, res => {
    // only respond once we're done, or Twilio's functions
    // may kill our execution before we finish.
    res.on("data", () => {});
    res.on("end", () => {
      // respond with an empty message
      callback(null, new Twilio.twiml.MessagingResponse());
    });
  });

And it should work.

Upvotes: 2

Related Questions