Skip to content

Running a minimal Slack App on Cloudflare Workers to take advantage of R2, D1, KV, and more useful utilities.

Notifications You must be signed in to change notification settings

I-Dont-Remember/cloudflare-workers-slack-app

Repository files navigation

Slack App on Cloudflare Workers

Corresponding guide: Building a Slack App On Cloudflare Workers


Building out a minimal Slack App on Cloudflare Workers to take advantage of the many utilities that are available (and continue to be released) on their platform.

Uses the Sagi Slack client for Cloudflare Workers (GitHub) since the recommended Slack Bolt and @slack/web-api packages are Node based and have issues running in the Cloudflare environment.

Benefits of the Slack Platform

The newer Slack Platform comes with a few obvious handy features:

  • Built-in functions for interacting with Slack APIs
  • "Run on Slack" lets you avoid dealing with hosting your own infra
  • NoSQL datastores, so you don't have to host your own persistence

Benefits of Slack Apps on Cloudflare Workers

Despite not being natively supported by Slack's SDKs, running your app on Cloudflare Workers unlocks a number of commonly needed tools that may make it worth it:

On the surface there's a lot going for the Cloudflare ecosystem - I will add the ⚠️ caveat though: I have not used them in Production, so cannot speak to availability, uptime, etc. that are important considerations when choosing platforms.

Run locally

make run

Simple Test event

make send_test_event

Response should look something like (404 since it's from us, not Slack):

./send-test-event.sh
HTTP/1.1 404 Not Found
date: Mon, 27 Feb 2023 03:05:00 GMT
content-type: application/json; charset=UTF-8
vary: Accept-Encoding
server: cloudflare
Connection: keep-alive
Keep-Alive: timeout=5
Transfer-Encoding: chunked

{"ok":false,"error":"invalid request signature"}

Deploy changes to worker

make publish

Tools

Wrangle

Cloudflare recommends you install the Wrangler CLI locally to your project, rather than globally. It's annoying to run npm run wrangle -- < args I actually wanted>, so I added a simple script ./wrangle that acts as a proxy to the locally installed version of wrangler.

# Example
./wrangler secret list

Localhost Tunnels

When developing locally, Slack isn't able to "see" your localhost server to send events to. To get around this, we can use a tool that will forward a public address (like https://abc234.ngrok.io) to your localhost:< your app port>. You have a couple common options to choose from:

  • ngrok - simple to use, but endpoint changes every time and you have to update in the Slack App Console unless you have the paid version.
  • Cloudflared - newer kid on the block, I haven't used it much yet.

FAQ

  • Getting Authentication Error [1000] when using Wrangler?

    • Kind of a PITA, but was able to fix by running wrangler login again. Did this a few times, worked every time, though it doesn't seem like the right approach.
    • More info
  • Why can't I just use the @slack/web-api package?

    • That package depends on Axios for sending requests, which seems like a pain to get working on Cloudflare](https://community.cloudflare.com/t/can-i-use-axios-in-a-worker/168139/2). If you try it, you'll end up with:
      [WARN]  web-api:WebClient:0 http request failed adapter is not a function
      TypeError: adapter is not a function
          at dispatchRequest (index.js:4214:14)
          at Axios.request (index.js:4488:19)
          at Axios.httpMethod [as post] (index.js:4514:23)
          at Function.wrap [as post] (index.js:3133:19)
      
  • Seeing in logs: Your worker called response.clone(), but did not read the body of both clones....

    • This seems to be happening because of how The workers-slack package implements request verification. If it bothers you, you'll either need to submit an Issue and hope for a more efficient function (here or at the package), or write your own Slack verification functionality.
  • < your question here :D >