forked from vishwa35/slackbot-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
56 lines (44 loc) · 1.57 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import json
import logging
from flask import Flask, request, make_response, Response
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
from slack_sdk.signature import SignatureVerifier
from slashCommand import Slash
logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__)
@app.route("/slack/test", methods=["POST"])
def command():
if not verifier.is_valid_request(request.get_data(), request.headers):
return make_response("invalid request", 403)
info = request.form
# # send user a response via DM
# im_id = slack_client.im_open(user=info["user_id"])["channel"]["id"]
# ownerMsg = slack_client.chat_postMessage(
# channel=im_id,
# text=commander.getMessage()
# )
# # send channel a response
# response = slack_client.chat_postMessage(
# channel='#{}'.format(info["channel_name"]),
# text=commander.getMessage()
# )
try:
response = slack_client.chat_postMessage(
channel='#{}'.format(info["channel_name"]),
text=commander.getMessage()
)#.get()
except SlackApiError as e:
logging.error('Request to Slack API Failed: {}.'.format(e.response.status_code))
logging.error(e.response)
return make_response("", e.response.status_code)
return make_response("", response.status_code)
# Start the Flask server
if __name__ == "__main__":
SLACK_BOT_TOKEN = os.environ['SLACK_BOT_TOKEN']
SLACK_SIGNATURE = os.environ['SLACK_SIGNATURE']
slack_client = WebClient(SLACK_BOT_TOKEN)
verifier = SignatureVerifier(SLACK_SIGNATURE)
commander = Slash("Hey there! It works.")
app.run()