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.
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.
#!python# -*- coding: utf-8 -*-from flask import Flask, jsonify, requestfrom requests import getMIN_TOKEN_LEN =10HELP_TEXT ="""To obtain the token,\n- Create a Slack App at [Slack Apps page](https://api.slack.com/apps) (from scratch);- Click **Bots**, then **Review Scopes to Add**;- Add **users:read** Bot Token scope and then click **Install to Workspace** above;- Copy bot token and paste in this field."""app =Flask(__name__)defget_name(member):return member["profile"]["real_name"] or member["profile"]["display_name"]@app.route("/", methods=["GET", "POST"])defcommand():# Read config field. If it's not yet entered by the user, show them the input# form. Slapdash will save the entered value and not re-request again. token = request.headers.get("slack-token")ifnot token orlen(token)< MIN_TOKEN_LEN:returnjsonify({"config": {"form": {"fields": [ {"type": "text","id": "slack-token","label": "Slack Bot Token","placeholder": "xoxb-***-***-***","helpText": HELP_TEXT,"defaultValue": token,"error": "Invalid token"if token andlen(token) < MIN_TOKEN_LEN elseNone } ] } } })# We have the token. Send Slack API request. res =get( url="https://slack.com/api/users.list?pretty=1", headers={"Authorization": "Bearer "+ token} )# Build CommandResponse from the Slack response.returnjsonify({"view": {"type": "list","options": [ {"title": "@"+ member["name"] + (" — "+get_name(member) ifget_name(member) else""),"icon": member["profile"]["image_48"],"action": {"type": "open-url","url": "slack://user?team=%s&id=%s"% (member["team_id"], member["id"]) } }for member in res.json()["members"]ifnot member["is_bot"] andnot member["deleted"] ] } })@app.after_requestdefadd_header(response): response.headers["Access-Control-Allow-Headers"]="*"# for config headers response.headers["Access-Control-Allow-Origin"]="*"return responseif__name__=="__main__": app.run(host="0.0.0.0", port=8080, debug=True)