Smithable websitedocsGitHubTry it
Menu

Slack and your app

A slash command in Slack opens a record in your app: /incident Payments are slow makes an incident on a status page, /lead Northwind, wants a quote a lead in a CRM. Slack events can reach the app the same way. Nothing about Slack is in the spec: the entry says how the call comes (a form), how Slack signs it (the time and the body, with its signing secret) and that Slack checks a new address first.

1. A slash command that opens a record

In smithable.yml, on the status starter:

incoming:
  chatCommand:
    create: Incident
    set: { title: "{text}", status: investigating, detectedAt: NOW() }
    body: form
    signed: { sha256: "v0:{timestamp}:{body}", prefix: "v0=", timestamp: X-Slack-Request-Timestamp }
    header: X-Slack-Signature
    challenge: challenge
    as: staff

From the command line: smithable op addIncoming chatCommand --create Incident --set "title={text}" status=investigating "detectedAt=NOW()" --form --signed-text "v0:{timestamp}:{body}" --signed-prefix v0= --timestamp X-Slack-Request-Timestamp --header X-Slack-Signature --challenge challenge --as staff.

2. On Slack's side

  1. At api.slack.com/apps, create an app (from scratch) in your workspace.
  2. Under Basic Information, copy the Signing Secret. In your app it is the setting INCOMING_CHATCOMMAND_SECRET (the entry's name in capitals), set in the app's environment when you host it yourself. On smithable.ai, publish the app, then paste it into the Calls in card on the project's page under Or use the service's own, and press Use this secret; the app restarts to use it (more).
  3. Under Slash Commands, create the command (/incident) with the Request URL https://<your app>/incoming/chatCommand.
  4. Install the app to the workspace. Type /incident Payments are slow in any channel: the incident appears in the app, titled "Payments are slow".

Slack shows the person what the address answers. The app answers with the record's id, which Slack shows as is; to tell the channel something in words, let the app post back when the record is made: an outgoing entry on Incident created to a target whose address is a Slack incoming webhook (mail and calls out).

3. Events instead of a command

Slack's Event Subscriptions (a message in a channel, a reaction, a new member) come as JSON, signed the same way, after the setup check. Make a second entry for them, without body: form, and let your own code decide what each event does:

incoming:
  chatEvents:
    custom: true                     # src/custom/incoming/chat-events.ts
    signed: { sha256: "v0:{timestamp}:{body}", prefix: "v0=", timestamp: X-Slack-Request-Timestamp }
    header: X-Slack-Signature
    challenge: challenge
    as: staff

The Request URL is https://<your app>/incoming/chatEvents; Slack checks it when you save it. Both entries share the app's signing secret: set INCOMING_CHATEVENTS_SECRET to the same value.

4. A test call, and what can go wrong

With the secret in $SECRET and the time in $TS:

BODY='text=Payments+are+slow&user_name=ada'
TS=$(date +%s)
SIG="v0=$(printf 'v0:%s:%s' "$TS" "$BODY" | openssl dgst -sha256 -hmac "$SECRET" | sed 's/^.* //')"
curl -s -X POST https://<your app>/incoming/chatCommand \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -H "X-Slack-Request-Timestamp: $TS" -H "X-Slack-Signature: $SIG" --data "$BODY"