-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
115 lines (92 loc) · 3.59 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
To learn available listener arguments, visit https://slack.dev/bolt-python/api-docs/slack_bolt/kwargs_injection/args.html
https://api.slack.com/events/message
"""
import configparser
import logging
import os
import sys
import time
import urllib.request
from threading import Thread
from typing import Callable
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
from bot import bot_meta, client, handler, store
from util import message, user
logging.basicConfig(level=logging.INFO)
config = configparser.ConfigParser()
config.read(sys.argv[1])
BOT_TOKEN = config["DEFAULT"]["SLACK_BOT_TOKEN"]
SIGNING_SECRET = config["DEFAULT"]["SLACK_SIGNING_SECRET"]
# Initializes your app with your bot token and socket mode handler
app = App(
token=BOT_TOKEN,
signing_secret=SIGNING_SECRET
)
store = store.Store(sys.argv[1].strip('dispense.conf'))
bot = bot_meta.BotMeta(store)
client = client.Client(app)
handler = handler.Handler(app, bot, client)
@app.middleware
def log_request(logger: logging.Logger, body: dict, next: Callable):
logger.debug(f'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ @app.middleware: {body}')
return next()
@app.event({'type': 'app_mention'})
def event_app_mention(body, say, logger):
logger.debug("BEGIN body #############################")
logger.debug(body)
logger.debug("END body ###############################")
dispense_msg = message.create_msg(body, client)
handler.event_app_mention(dispense_msg, say, logger)
@app.event({'type': 'file_shared'})
def handle_file_shared_events(body, say, logger):
logger.debug("BEGIN body #############################")
logger.debug(body)
logger.debug("END body ###############################")
dispense_msg = message.create_msg(body, client)
store.publish_message(dispense_msg)
handler.handle_file_shared_events(dispense_msg, say, logger)
@app.event({'type': 'message', 'subtype': 'message_deleted'})
def handle_message_deleted(body, say, logger):
# TODO Future iteration
return
@app.event({'type': 'message', 'subtype': 'message_changed'})
def handle_message_changed(body, say, logger):
# TODO Future iteration
return
@app.event({'type': 'message', 'subtype': None})
def handle_message_events(body, say, logger):
logger.debug("BEGIN body #############################")
logger.debug(body)
logger.debug("END body ###############################")
dispense_msg = message.create_msg(body, client)
store.publish_message(dispense_msg)
def consumer():
while True:
try:
offset = store.fetch_offset()
dispense_messages = store.fetch_messages(offset, bot.team_id)
for dispense_msg in dispense_messages:
dispense_msg = adapt_message(dispense_msg)
if dispense_msg.file_id is not None:
if handler.share_file(dispense_msg) == True:
store.acked_message(dispense_msg)
elif dispense_msg.is_thread:
# TODO Future iteration
store.acked_message(dispense_msg)
else:
if handler.share_message(dispense_msg) == True:
store.acked_message(dispense_msg)
except Exception as e:
print(e)
time.sleep(1)
def adapt_message(dispense_msg):
channel_id = store.fetch_channel_id(dispense_msg.channel_name)
dispense_msg.channel_id = channel_id
return dispense_msg
if __name__ == "__main__":
t = Thread(target=consumer)
t.daemon = True
t.start()
SocketModeHandler(app, config["DEFAULT"]["SLACK_APP_TOKEN"]).start()