Create a slcak bot and send a message from slack to the server. Sending a message from the server to slack is-> "Send a message from python to Slack"
Prepare a server to access from slack. It is necessary to meet the conditions by referring to url_verification event.
import flask
from flask import request, Response
import os
import json
app = flask.Flask(__name__)
@app.route('/', methods=["POST"])
def index():
data = request.data.decode('utf-8')
data = json.loads(data)
# for challenge of slack api
if 'challenge' in data:
token = str(data['challenge'])
return Response(token, mimetype='text/plane')
# for events which you added
if 'event' in data:
print("get event")
event = data['event']
if 'user' in event:
print("user = ", event["user"])
if "text" in event:
print("text = ", event["text"])
return Response("nothing", mimetype='text/plane')
port = os.getenv('VCAP_APP_PORT', '8000')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(port), debug=True)
Click "Create New App". Create an app by selecting the app name and workspace.
On the Event Subscriptions page, set Enable Events to "ON"
Specify the url of the server prepared in [1](# 1-Preparing the server).
Under Subscribe to workspace events, select workspace event from "Add Workspace Event". This time, select message.channels to get the message of the channel. Other events can be found at API Event Types.
I was able to receive messages from the slack channel on the server! I think I can do anything. Once you pass the challenge, you can stop the server afterwards and start it again.
Recommended Posts