Send Slack Message
Show list of Slack users and quickly jump to DM thread
Some commands may require some configuration, which you might not want to add to your command source code. For example, you might need an API key.
Slapdash provides an interface for your command to collect configuration data, which is then sent alongside each command run.
The first time you run the command, it asks for the Slack Bot authentication token, then shows the list of users in Slack. If a user is selected, the Slack desktop is opened directly to the DM thread of the selected user.
The command asks to provide the Slack Bot Token.
This is an example of a Cloud Command, a command that runs on a server.
With a Cloud Command, you can share its URL with anyone, and each person will be able to use the command with their own authentication tokens.
See Cloud Commands for details on how to create and deploy cloud commands.
1
#!python
2
# -*- coding: utf-8 -*-
3
from flask import Flask, jsonify, request
4
from requests import get
5
6
MIN_TOKEN_LEN = 10
7
HELP_TEXT = """
8
To obtain the token,\n
9
- Create a Slack App at [Slack Apps page](https://api.slack.com/apps) (from scratch);
10
- Click **Bots**, then **Review Scopes to Add**;
11
- Add **users:read** Bot Token scope and then click **Install to Workspace** above;
12
- Copy bot token and paste in this field.
13
"""
14
15
app = Flask(__name__)
16
17
def get_name(member):
18
return member["profile"]["real_name"] or member["profile"]["display_name"]
19
20
@app.route("/", methods=["GET", "POST"])
21
def command():
22
# Read config field. If it's not yet entered by the user, show them the input
23
# form. Slapdash will save the entered value and not re-request again.
24
token = request.headers.get("slack-token")
25
if not token or len(token) < MIN_TOKEN_LEN:
26
return jsonify({
27
"config": {
28
"form": {
29
"fields": [
30
{
31
"type": "text",
32
"id": "slack-token",
33
"label": "Slack Bot Token",
34
"placeholder": "xoxb-***-***-***",
35
"helpText": HELP_TEXT,
36
"defaultValue": token,
37
"error": "Invalid token" if token and len(token) < MIN_TOKEN_LEN else None
38
}
39
]
40
}
41
}
42
})
43
44
# We have the token. Send Slack API request.
45
res = get(
46
url="https://slack.com/api/users.list?pretty=1",
47
headers={"Authorization": "Bearer " + token}
48
)
49
50
# Build CommandResponse from the Slack response.
51
return jsonify({
52
"view": {
53
"type": "list",
54
"options": [
55
{
56
"title": "@" + member["name"] +
57
(" — " + get_name(member) if get_name(member) else ""),
58
"icon": member["profile"]["image_48"],
59
"action": {
60
"type": "open-url",
61
"url": "slack://user?team=%s&id=%s" % (member["team_id"], member["id"])
62
}
63
}
64
for member in res.json()["members"]
65
if not member["is_bot"] and not member["deleted"]
66
]
67
}
68
})
69
70
@app.after_request
71
def add_header(response):
72
response.headers["Access-Control-Allow-Headers"] = "*" # for config headers
73
response.headers["Access-Control-Allow-Origin"] = "*"
74
return response
75
76
if __name__ == "__main__":
77
app.run(host="0.0.0.0", port=8080, debug=True)
Copied!
Last modified 5d ago
Copy link