Skip to content

Slack

Overview

Some concepts that must known required for developers

[1] Understanding the Events Loop of Slack:

The event loop

Many apps built using the Events API will follow the same abstract event-driven sequence:

1. A user creates a circumstance that triggers an event subscription to your application

2. Your server receives a payload of JSON describing that event

3. Your server acknowledges receipt of the event

4. Your business logic decides what to do about that event

5. Your server carries out that decision

If your app is a bot listening to messages with specific trigger phrases, that event loop may play out something like:

1. Members send messages in a channel the bot belongs to. The messages are about lots of things, but some of them contain today's secret word.
Your server receives a message.channels event, as per its bot subscription and membership in #random.

2. Your server responds with a swift and confident HTTP 200 OK.

3. Your bot is trained to listen for today's secret word, and having found it, decides to send a message to the channel, encouraging everyone to keep that word secret.

4. Your server uses chat.postMessage from the Web API to post that message to #random.

5. Using the Web API with the Events API empowers your app or bot to do much more than just listen and reply to messages.

Ref: The Event Loop

[2] Why Slack application need to required URL endpoint?

Ref: Request URL Configuration & Verification

[3] Listening for events

Ref: https://github.com/slackapi/bolt-python

Apps typically react to a collection of incoming events, which can correspond to Events API events, actions, shortcuts, slash commands or options requests. For each type of request, there's a method to build a listener function.

# Listen for an event from the Events API
app.event(event_type)(fn)

# Convenience method to listen to only `message` events using a string or re.Pattern
app.message([pattern ,])(fn)

# Listen for an action from a Block Kit element (buttons, select menus, date pickers, etc)
app.action(action_id)(fn)

# Listen for dialog submissions
app.action({"callback_id": callbackId})(fn)

# Listen for a global or message shortcuts
app.shortcut(callback_id)(fn)

# Listen for slash commands
app.command(command_name)(fn)

# Listen for view_submission modal events
app.view(callback_id)(fn)

# Listen for options requests (from select menus with an external data source)
app.options(action_id)(fn)

The recommended way to use these methods are decorators:

@app.event(event_type)
def handle_event(event):
    pass

[4] 3 types of Token in Slack

There are three main token types available to a Slack app: user (xoxp), bot (xoxb), and app-level (xapp) tokens.

User tokens allow you to call API methods on behalf of users after they install or authenticate the app. There may be several user tokens for a single workspace.

Bot tokens are associated with bot users, and are only granted once in a workspace where someone installs the app. The bot token your app uses will be the same no matter which user performed the installation. Bot tokens are the token type that most apps use.

App-level tokens represent your app across organizations, including installations by all individual users on all workspaces in a given organization and are commonly used for creating WebSocket connections to your app.

Ref: Getting Started

[5] Event API documentation:

URL official of Event Types

[6] Progress:

  • Create Slack Application with detail secrets and methods

  • Create Adapter to handler the requests, which not using the SocketHandler

  • Create component handlers functions that responds to the requests

  • Implement API elements into application.

[7] Example on Slack and Dialogs

Example for Diagram

Ref: https://api.slack.com/best-practices/blueprints/slash-command-and-dialogs

[8] Problem: slash command dispatch failed

Solution:

While the documentation tells you:

"use the Request URL is your base server link + "/slashcommand" after it"

This is incorrect. The request URL should be: "/slack/events"

Of course the command needs to match whats in the 'edit command' window and in the method '.command' in your app.js:

app.command('/flash-card', async ({ ack, body, client })

Ref:

[9] Connect database MySQL base on the repo Streaming-Magnus

Interacty

say() sends a message to the channel where the event was triggered

[10] next_() is an alias for next() in Python

Ref: https://slack.dev/bolt-python/api-docs/slack_bolt/kwargs_injection/args.html#slack_bolt.kwargs_injection.args.Args.next