-
Notifications
You must be signed in to change notification settings - Fork 0
/
talktome.py
67 lines (54 loc) · 2.06 KB
/
talktome.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
57
58
59
60
61
62
63
64
65
66
67
#/usr/bin/env python
import glob
import json
import logging
import os
import yaml
from apscheduler.schedulers.background import BackgroundScheduler
from flask import Flask, request, make_response
from slackeventsapi import SlackEventAdapter
from lib import core, zendesk, messaging
app = Flask(__name__)
logging.basicConfig(level=logging.DEBUG)
SLACK_SIGNING_SECRET = os.environ["SLACK_SIGNING_SECRET"]
slack_events_adapter = SlackEventAdapter(SLACK_SIGNING_SECRET, "/slack/events", app)
SLACK_VERIFICATION_TOKEN = os.environ["SLACK_VERIFICATION_TOKEN"]
# scheduler
scheduler = BackgroundScheduler()
# find all workflows and build a TalkCore for each of them.
talk_core = {}
for config in glob.glob("configs/*.yaml"):
workflow = yaml.safe_load(file(config))
talk_core[workflow['label']] = core.TalkCore(
os.environ["SLACK_BOT_TOKEN"],
"dropbox.com",
os.environ["ZENDESK_URL"],
os.environ["ZENDESK_EMAIL"],
os.environ["ZENDESK_API"],
workflow,
)
scheduler.add_job(talk_core[workflow['label']].run, 'interval', minutes=1)
scheduler.start()
# Helper for verifying that requests came from Slack
def verify_slack_token(request_token):
if SLACK_VERIFICATION_TOKEN != request_token:
logging.error("Error: invalid verification token!")
logging.error("Received %s but was expecting %s", request_token, SLACK_VERIFICATION_TOKEN)
return make_response("Request contains invalid Slack verification token", 403)
@app.route("/slack/message_actions", methods=["POST"])
def message_actions():
# Parse the request payload
json_data = json.loads(request.form["payload"])
# Verify that the request came from Slack
verify_slack_token(json_data["token"])
# Route to correct TalkCore
for block in json_data['message']['blocks']:
if block['block_id'] in talk_core:
talk_core[block['block_id']].message_actions(json_data)
return make_response("", 200)
@app.route("/")
def hello():
return "Go away!"
# Start the server on port 3000
if __name__ == "__main__":
app.run(port=3000)