This repository has been archived by the owner on Jan 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
73 lines (60 loc) · 2.12 KB
/
app.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
68
69
70
71
72
73
# -*- coding: utf-8 -*-
"""
In this file, we'll create a routing layer to handle incoming and outgoing
requests between our bot and Slack.
"""
import os
import jinja2
import json
import time
from concurrent.futures import ThreadPoolExecutor
from flask import make_response, render_template
from slackclient import SlackClient
from slackeventsapi import SlackEventAdapter
from handlers import process_message
from plugins import Reddit
from log import setup_custom_logger
logger = setup_custom_logger('root')
# Load client_id/client_secret
client_id = os.environ.get("CLIENT_ID")
client_secret = os.environ.get("CLIENT_SECRET")
verification = os.environ.get("VERIFICATION_TOKEN")
token=""
client = None #SlackClient("")
events_adapter = SlackEventAdapter(verification, "/events")
template_loader = jinja2.ChoiceLoader([
events_adapter.server.jinja_loader,
jinja2.FileSystemLoader(['templates']),
])
events_adapter.server.jinja_loader = template_loader
executor = ThreadPoolExecutor(max_workers=4)
@events_adapter.on("message")
def handle_message(event_data):
event = event_data['event']
if 'bot_id' in event or 'text' not in event:
return
executor.submit(process_message, event, client)
@events_adapter.server.before_first_request
def before_first_request():
if not client_id:
print("Can't find Client ID, did you set this env variable?")
if not client_secret:
print("Can't find Client Secret, did you set this env variable?")
if not verification:
print("Can't find Verification Token, did you set this env variable?")
if __name__ == '__main__':
try:
with open("auth.json", "r") as f:
auth = json.loads(f.read())
client = SlackClient(auth["bot"]["bot_access_token"])
except Exception as e:
print(e)
print("No token registered, please run oauth.py")
debug = False
if 'DEBUG' in os.environ:
debug = True
executor.submit(events_adapter.start, debug=debug, port=9040)
reddit = Reddit(client)
executor.submit(reddit.loop)
while True:
time.sleep(60)