-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
141 lines (104 loc) · 3.99 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import json
import logging
from typing import Dict
from slack_bolt import App
from slack_bolt.adapter.aws_lambda.chalice_handler import ChaliceSlackRequestHandler
from chalice import Chalice, Response, Cron
from chalicelib.bot_service import BotService
from chalicelib.slack_message import SlackMessage
from chalicelib.slack_reaction import SlackReaction
from chalicelib.global_constants import *
from chalicelib.response_generator import *
bolt_app = App(
token=os.environ.get("SLACK_BOT_TOKEN"),
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
process_before_response=True,
)
log_levels: Dict[str, int] = {
'DEBUG': logging.DEBUG,
'INFO': logging.INFO,
'WARN': logging.WARN,
'ERROR': logging.ERROR
}
@bolt_app.middleware
def log_request(logger, body, next):
app.log.debug(json.dumps(body, indent=2))
return next()
@bolt_app.event("reaction_added")
def handle_emoji_reaction_added(client, event, logger):
app.log.debug("*** EMOJI ADDED ***")
app.log.debug(json.dumps(event, indent=2))
if event['reaction'] != EMOJI:
return
msg = SlackReaction.parse(event)
bot_service = BotService( client, DYNAMO_TABLE_NAME, **{'endpoint_url': DYNAMO_ENDPOINT_URL})
bot_service.award_emoji_points_from_msg(msg)
# @app.event("reaction_removed")
# def handle_emoji_reaction_removed(ack, respond, message, event, client, command, say, logger):
# logger.debug(json.dumps(event, indent=2))
# # TODO figure out how/if to remove points upon removing a reaction
# return
@bolt_app.message(f":{EMOJI}:")
def handle_emoji_message(client, message, logger):
app.log.debug("*** MESSAGE ADDED ***")
app.log.debug(json.dumps(message, indent=2))
if 'bot_id' in message:
app.log.warning("Ignore bot event")
return
msg = None
try:
msg = SlackMessage.parse(message)
except Exception as e:
app.log.error(e)
return
bot_service = BotService( client, DYNAMO_TABLE_NAME, **{'endpoint_url': DYNAMO_ENDPOINT_URL})
bot_service.award_emoji_points_from_msg(msg)
@bolt_app.command(f"/{SLASH_COMMAND.lower()}")
def handle_slash_command(client, ack, respond, command):
# Acknowledge command request
ack()
app.log.debug(json.dumps(command, indent=2))
subcommand = command['text']
if subcommand == 'help':
# TODO
respond("Help text will go here!")
return
bot_service = BotService( client, DYNAMO_TABLE_NAME, **{'endpoint_url': DYNAMO_ENDPOINT_URL})
if subcommand == EMOJI_PLURAL:
points_sent = bot_service.get_user_points_sent_today(command['user_id'])
respond(f"You've given {count_with_emoji_text(points_sent)} today.")
elif 'weekly' in subcommand:
respond(bot_service.weekly_leaderboard())
elif 'monthly' in subcommand:
respond(bot_service.monthly_leaderboard())
else:
respond(bot_service.all_time_leaderboard())
ChaliceSlackRequestHandler.clear_all_log_handlers()
app = Chalice(app_name=BOT_NAME.lower())
slack_handler = ChaliceSlackRequestHandler(app=bolt_app, chalice=app)
app.log.setLevel(log_levels[LOG_LEVEL.upper()])
@app.route(
"/slack/events",
methods=["POST"],
content_types=["application/x-www-form-urlencoded", "application/json"],
)
def events() -> Response:
return slack_handler.handle(app.current_request)
@app.route("/slack/install", methods=["GET"])
def install() -> Response:
return slack_handler.handle(app.current_request)
@app.route("/slack/oauth_redirect", methods=["GET"])
def oauth_redirect() -> Response:
return slack_handler.handle(app.current_request)
# Send weekly leaderboard every Monday morning
@app.schedule(Cron(0, 14, '*', '?', '1', '*'))
def handle_weekly_leaderboard_message(event):
app.log.warning("*** WEEKLY LEADERBOARD TRIGGERED ***")
pass
# def handler(event, context):
# slack_handler = SlackRequestHandler(app=app)
# return slack_handler.handle(event, context)
#
#
# if __name__ == "__main__":
# app.start(port=int(os.environ.get("PORT", 3000)))