-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlistener.py
executable file
·59 lines (44 loc) · 2.04 KB
/
listener.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
#!/usr/bin/env python3
"""Tool for listening to testimonial-related activity in KA's slack channels.
Listener connects to slack's real-time messaging API, keeps an eye on any
activity related to testimonials, and triggers appropriate responses.
For example, listener will notice when a new testimonial notification has been
posted and automatically add an upvote button for others to click by responding
w/ its own emoji reaction.
For another example, listener will notice when an employee has upvoted a
testimonial and send that upvote to KA's API for tracking.
See https://api.slack.com/rtm for more info about slack's real-time API.
"""
import logging
import time
import slack
import secrets
import testimonials
def handle_messages(messages):
"""Handle a list of slack msgs, responding to testimonial-related ones."""
for message in messages:
# If we see a new testimonial announcement, automatically add some
# emoji reaction buttons.
if testimonials.is_new_testimonial_announcement(message):
testimonials.add_emoji_reaction_buttons(message)
# If we see a reaction to a testimonial announcement, respond to it.
reacted_to_message = (
testimonials.maybe_get_reacted_to_testimonial_message(message))
if reacted_to_message:
testimonials.send_updated_reaction_totals(message,
reacted_to_message)
def listen():
"""Start listening to slack channels the Testimonials Turtle bot is in."""
# STOPSHIP: better error handling so an exception doesn't crash the module
client = slack.WebClient(secrets.slack_testimonials_turtle_api_token)
if not client.rtm_connect():
logging.critical("Failed to connect to Slack RTM API, bailing.")
return
# Once connected, just continually pull messages and handle those that are
# testimonials-related.
while True:
messages = client.rtm_read()
handle_messages(messages)
time.sleep(1)
if __name__ == '__main__':
listen()