title: Using Slack's API dialog_open from Firebase Cloud Functions
body: <h2>Goal</h2>
  
  <p>A Slack Event-Subscription event is hitting my Firebase Endpoint, and then, a Firebase Cloud Function is using the <code>dialog_open</code> endpoint to open a dialog in Slack with some values from Firebase.</p>
  
  <h2>Issue</h2>
  
  <p>When the Firebase Cloud function hits Slack's  <code>dialog_open</code> endpoint, I get errors in the console log.</p>
  
  <p>I am getting <code>body: { ok: false, error: 'trigger_expired' }</code></p>
  
  <p>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).</p>
  
  <pre><code>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
  </code></pre>
  
  <p>When I trigger the Slack event a second, third, or fourth time after a fresh deploy, I get <code>body: { ok: false, error: 'invalid_trigger' }</code></p>
  
  <pre><code>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
  </code></pre>
  
  <p>This also happens fast enough (9ms), but a different trigger error, AND this time I <strong>do</strong> see the <code>json.trigger_id</code> from the Slack Event-Subscription event.</p>
  
  <p>Last thing, I tried JSON.stringify:</p>
  
  <p><code>trigger_id: JSON.stringify(json.trigger_id),</code></p>
  
  <p>Now the logs and error are different:</p>
  
  <pre><code>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
  </code></pre>
  
  <h2>Question</h2>
  
  <p>I must be doing something dumb. Any guesses what is wrong with my <code>trigger_id</code>?</p>
  
  <h2>Code</h2>
  
  <p>Here is the Firebase Cloud Function:</p>
  
  <pre><code>import * as rp from "request-promise";
  import * as functions from "firebase-functions";
  
  export const helloWorld = functions.https.onRequest((request, response) =&gt; {
    return new Promise((_resolve, _reject) =&gt; {
      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 =&gt; response.status(500).send(err));
  });
  </code></pre>