-
Notifications
You must be signed in to change notification settings - Fork 0
/
lunchbot.py
181 lines (146 loc) · 5.22 KB
/
lunchbot.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# LunchBot wants to know what's for lunch.
#
# Adapted with gratitude from
# https://www.fullstackpython.com/blog/build-first-slack-bot-python.html
#
# Author: April Shen
# Created on 2016-10-06
import os
import requests
import time
from datetime import date, datetime
from slackclient import SlackClient
from slack_app import get_attachments
# bot's ID as an environment variable
BOT_ID = os.environ.get("BOT_ID")
# how bot is addressed
AT_BOT = "<@" + BOT_ID + ">"
# default channel to post to (e.g. for polls)
CHANNEL = "#whitechapel"
# commands
MONDAY = 'monday'
TUESDAY = 'tuesday'
WEDNESDAY = 'wednesday'
THURSDAY = 'thursday'
FRIDAY = 'friday'
SATURDAY = 'saturday'
SUNDAY = 'sunday'
TODAY = 'today'
TOMORROW = 'tomorrow'
weekdays = [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY]
weekends = [SATURDAY, SUNDAY]
all_days = weekdays + weekends
relative = [TODAY, TOMORROW]
# column names
IMAGE_COL = 'Image'
TITLE_COL = 'Meal'
KEY_COL = 'Date'
# URL for data
DATA_URL = os.environ.get("SLACK_BOT_URL")
# instantiate Slack client
slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN'))
def next_day(day):
return (day + 1) % 7
def get_formatted_date(day):
# want nearest future date on the given day
desired_day = all_days.index(day)
current = date.today()
while current.weekday() != desired_day:
current = current.replace(day=current.day+1)
return current.strftime('%d %B %Y')
def get_actual_day(day):
# for day = TODAY or YESTERDAY
today = date.today().weekday()
return all_days[today if day == TODAY else next_day(today)]
def get_cat():
attach = []
r = requests.get('http://thecatapi.com/api/images/get')
if r.status_code == 200:
attach = [{
'image_url': r.url,
'text': '<' + r.url + '|src>'
}]
return attach
def post_poll():
text = "What did you think of today's lunch?\n:+1: Loved it!\n:-1: Please, never again...\n:unicorn: ¯\_(ツ)_/¯"
val = get_formatted_date(get_actual_day(TODAY))
attach = get_attachments(DATA_URL, TITLE_COL, IMAGE_COL, key_col=KEY_COL, key_val=val)
print 'done'
# Send poll
response = slack_client.api_call("chat.postMessage", channel=CHANNEL,
text=text, attachments=attach,
as_user=True)
poll_timestamp = response['ts']
def log_poll():
pass
# at time that today's poll is being posted
# save all reactions to yesterday's poll to a csv
def handle_command(command, channel):
"""
Receives commands directed at the bot and determines if they
are valid commands. If so, then acts on the commands. If not,
returns back what it needs for clarification.
"""
response = "Not sure what you mean. Try typing a day of the week!"
attach = None
# weekdays
for day in weekdays:
if day in command:
response = "Here's what's for lunch! Yum :yum:"
# convert day to date, because of how data formatted
date_string = get_formatted_date(day)
attach = get_attachments(DATA_URL, TITLE_COL, IMAGE_COL, key_col=KEY_COL, key_val=date_string)
# weekends
for day in weekends:
if day in command:
response = "There's no lunch on the weekends! Have a cat instead :cat:"
attach = get_cat()
# relative
for day in relative:
if day in command:
# compute which day, and handle command with that day
actual_day = get_actual_day(day)
handle_command(actual_day, channel)
return # so it doesn't post twice
print 'done'
# Send response
slack_client.api_call("chat.postMessage", channel=channel,
text=response, attachments=attach,
as_user=True)
def parse_slack_output(slack_rtm_output):
"""
The Slack Real Time Messaging API is an events firehose.
this parsing function returns None unless a message is
directed at the Bot, based on its ID.
"""
output_list = slack_rtm_output
if output_list and len(output_list) > 0:
for output in output_list:
if output and 'text' in output and AT_BOT in output['text']:
# return text after the @ mention, whitespace removed
return output['text'].split(AT_BOT)[1].strip().lower(), \
output['channel']
return None, None
if __name__ == "__main__":
READ_WEBSOCKET_DELAY = 1 # 1 second delay between reading from firehose
POLL_TIME = 14 # what hour to poll after
POLLED = False # have we polled yet today?
if slack_client.rtm_connect():
print("LunchBot connected and running!")
while True:
command, channel = parse_slack_output(slack_client.rtm_read())
if command and channel:
handle_command(command, channel)
# polling
now = datetime.now()
if not POLLED and (now.hour == POLL_TIME) and (now.weekday() < 5):
post_poll()
POLLED = True
if now.hour > POLL_TIME:
POLLED = False
time.sleep(READ_WEBSOCKET_DELAY)
else:
print("Connection failed. Invalid Slack token or bot ID?")