-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue_bot.py
60 lines (42 loc) · 1.72 KB
/
queue_bot.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
from slackclient import SlackClient
import os, time
from slackqueue import SlackQueue
_QUEUE = SlackQueue()
def run_bot_update_queue(sc, channel_name):
"""Check latest messages constantly and update queue."""
# Set the channel to send messages to, based on name passed
channel_info = sc.server.channels.find(channel_name)
channel_id = channel_info.id
# TODO: Add whitelist of users who are allowed to interact with queue bot
# to make sure staff are the only ones who can manage the queue.
# Read messages forever!
while True:
latest = sc.rtm_read()
# Are there any new messages, and are they not the stock bot connection
# message?
if (latest != [] and latest != [{'text': u'hello'}]):
latest = latest[0]
# Make sure we're in the desired channel and the latest update
# was actually a message, not a typing notification.
if (latest.get("channel") == channel_id and
latest.get("type") == "message"):
text = latest["text"]
print "Latest:", latest
_QUEUE.update(text)
if _QUEUE.needs_message:
sc.rtm_send_message(
channel_id,
_QUEUE.generate_display()
)
_QUEUE.needs_message = False
time.sleep(.5)
if __name__ == "__main__":
# Get API token and instantiate a client for all our Slack
# interactions.
slack_token = os.environ.get("BOT_API_TOKEN")
sc = SlackClient(slack_token)
# Try to connect to the real time messaging service
if sc.rtm_connect():
run_bot_update_queue(sc, "help")
else:
print "Connection Failed"