4

Goal

A Slack Event-Subscription event is hitting my Firebase Endpoint, and then, a Firebase Cloud Function is using the dialog_open endpoint to open a dialog in Slack with some values from Firebase.

Issue

When the Firebase Cloud function hits Slack's dialog_open endpoint, I get errors in the console log.

I am getting body: { ok: false, error: 'trigger_expired' }

However, the logs in Firebase show the round-trip is less than 500 milliseconds. But, I don't see the trigger ID which will log to the trigger_id from the first request (see code below).

4:57:12.443 PM | info | helloWorld | body: { ok: false, error: 'trigger_expired' } 
4:57:04.163 PM | outlined_flag | helloWorld | Function execution took 254 ms, finished with status code: 200
4:57:03.910 PM | outlined_flag | helloWorld | Function execution started

When I trigger the Slack event a second, third, or fourth time after a fresh deploy, I get body: { ok: false, error: 'invalid_trigger' }

5:31:29.757 PM helloWorld body: { ok: false, error: 'invalid_trigger' }
5:31:28.744 PM helloWorld Function execution took 9 ms, finished with status code: 200
5:31:28.740 PM helloWorld json.trigger_id: "405615464868.7467239747.e706f2732257c541c445ad3938a29fd3"
5:31:28.735 PM helloWorld Function execution started

This also happens fast enough (9ms), but a different trigger error, AND this time I do see the json.trigger_id from the Slack Event-Subscription event.

Last thing, I tried JSON.stringify:

trigger_id: JSON.stringify(json.trigger_id),

Now the logs and error are different:

5:33:24.512 PM | info | helloWorld | body: { ok: false, error: 'internal_error' }
5:33:23.565 PM | outlined_flag | helloWorld | Function execution took 13 ms, finished with status code: 200
5:33:23.559 PM | info | helloWorld | json.trigger_id: "406895248231.7467239747.7490e460213b3d65a44eef9f2e30c168"
5:33:23.553 PM | outlined_flag | helloWorld | Function execution started

Question

I must be doing something dumb. Any guesses what is wrong with my trigger_id?

Code

Here is the Firebase Cloud Function:

import * as rp from "request-promise";
import * as functions from "firebase-functions";

export const helloWorld = functions.https.onRequest((request, response) => {
  return new Promise((_resolve, _reject) => {
    let json = JSON.parse(request.body.payload);
    console.log("json.trigger_id:", json.trigger_id);

    const options = {
      method: "POST",
      uri: "https://slack.com/api/dialog.open",
      body: {
        trigger_id: json.trigger_id,
        dialog: {
          callback_id: json.callback_id,
          title: "Request a Ride",
          submit_label: "Request",
          elements: [
            { type: "text", label: "Pickup Location", name: "loc_origin" },
            { type: "text", label: "Dropoff Location", name: "loc_destination" }
          ]
        }
      },
      json: true,
      headers: {
        "Content-type": "application/json; charset=utf-8",
        Authorization:
          "Bearer xoxp-secret"
      }
    };

    rp(options)
      .then(function(body) {
        console.log("body:", body);
      })
      .catch(function(err) {
        console.log("err:", err);
      });
    return response.status(200).json({ message: "Hello from Firebase" });
  }).catch(err => response.status(500).send(err));
});
Chadd
  • 636
  • 7
  • 30
  • 1
    For anyone else coming across this, [here is the link to the relevant Slack docs](https://api.slack.com/dialogs). I can't spot anything you're doing wrong.... but I'll mention that if you're looking at the function logs via the Firebase console, sometimes not all log messages show up on the parent-level log display. A more reliable place to find log messages is in the GCP logs directly: Visit https://console.cloud.google.com and select Cloud Functions on the left sidebar menu, then click on the 3 dots and choose "View logs". Might help you debug. – JeremyW Jul 27 '18 at 20:11
  • I think I have made some silly mistakes. See the excellent response to this question on the slackapi GitHub page. https://github.com/slackapi/node-slack-sdk/issues/597#issuecomment-411256005 Once fixed, I will post the solution here. – Chadd Aug 08 '18 at 18:19

1 Answers1

0

Answer

Broken Promises: In my example, the promises are just wrong.

Message vs Dialog: Dialogs were not needed or appropriate for this goal.

Code Smell: The web.chat.postMessage isn't needed to send back a message. A message can be sent back to Slack using the Firebase Cloud Function's res.

Here is an improved example.

...
exports.eventsSlackBot = functions.https.onRequest((req, res) => {
  return new Promise((resolve, reject) => {
      // asyc things happening
    })
    .then(() => {
      resolve.status(200).send({
        "text": "I am a test message http://slack.com",
        "attachments": [{
          "text": "And here’s an attachment!"
        }]
      });
    })
    .catch(console.error);
});
...
Chadd
  • 636
  • 7
  • 30