-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlunch_bot.py
159 lines (112 loc) · 4.74 KB
/
lunch_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
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
import os
import time
import re
import random
from datetime import datetime
from slackclient import SlackClient
#import logging
#logging.getLogger('slackclient.client').addHandler(logging.NullHandler())
# Instantiate Slack Client
slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN'))
#print slack_client
# Lunch Bot User ID
lunch_bot_id = None
# Constants
RTM_READ_DELAY = 1 # 1 sec delay between reading from RTM
MENTION_REGEX = "^<@(|[WU].+?)>(.*)"
# Command Keywords
LUNCH_COMMAND = "lunch"
WHERE_COMMAND = "where"
PLACES_COMMAND = "places"
LIST_COMMAND = "list"
NEVER_COMMAND = "never"
AGAIN_COMMAND = "again"
# Dataset
RESTAURANTS = ["Au Bon Pain (Kern Blgd)", "Pho11", "Noodles & Co.", "Subway", "Starbucks", "Little Scheshuan", "Qdoba","Margarita\'s Pizza", "California Tortilla", "Doan\'s Bones", "John\'s Shanghai", "Five Guys", "Cafe 210", "Tadashi", "Federal Taphouse" , "Green Bowl", "Penn Pide", "Fiddlehead", "Burger King (HUB)", "Chik-fil-A (HUB)", "Sbarro (HUB)", "Panda Express (HUB)", "HUB Soups and Garden", "Blue Burrito (HUB)", "Grate Chee (HUB)", "McAlister\'s Deli", "Mixed Greens (HUB)","Hibachi-San (HUB)" ,"Sauly Boy\'s", "Jersey Mike\'s", "Kondu", "Panera Bread", "Champs", "Irving\'s", "Kaarma", "Cozy Thai", "Shaker\'s Grill Food Cart", "Yallah Taco", "India Pavilion", "Underground", "Canyon", "Taco Bell", "Chipotle...", "Waker Chicken", "Yummy Cafe", "McLanahans", "The Deli & Z Bar", "Primanti Bros.", "The Waffle Shop", "Big Bowl", "Penn Kebab", "Jimmy John\'s", "McDonals\'s", "Are U Hungry", "Beijing", "Mad Mex"]
AWFUL_PLACES = ["Qdoba", "Au Bon Pain (Westgate)", "Canyon", "Chipotle"]
# Generate random seed
random_num = random.seed(datetime.now())
###
# Aux Functions
###
def parse_bot_commands(slack_events):
for event in slack_events:
# Get the command
if event["type"] == "message" and not "subtype" in event:
matches = re.search(MENTION_REGEX, event["text"])
else:
matches = None
# If a command was given, extract it
if matches:
user_id = matches.group(1)
message = matches.group(2).strip()
else:
user_id = None
message = None
if user_id == lunch_bot_id:
return message, event["channel"]
else:
return None, None
def send_response_back(response, channel):
# Send response back to the channel
slack_client.api_call("chat.postMessage",
channel = channel,
text = response)
def send_restaurant_list(channel):
restaurants = ", ".join(RESTAURANTS)
response = "Here is a list of restaurants that I consider: \n" + restaurants
send_response_back(response, channel)
def send_lunch_suggestions(channel):
# Chosen restaurant
choice = random.choice(RESTAURANTS)
print("Index choice..." + str(choice))
#print("Restaurant chosen..." + str(restaurant))
# Suggest random restaurant
suggestion = "Today\'s suggestion is... " + choice + "."
if "Chipotle" in choice or "Qdoba" in choice:
new_choice = random.choice(RESTAURANTS)
if "Chipotle" in new_choice or "Qdoba" in new_choice:
new_choice = random.choice(RESTAURANTS)
suggestion = suggestion + " But a better choice might be " + new_choice + " instead..."
# Send suggestion back to the channel
send_response_back(suggestion, channel)
def send_never_places(channel):
places = ", ".join(AWFUL_PLACES)
response = "You might want to avoid these places... " + places + "."
send_response_back(response, channel)
def send_unknown_response(channel):
# Craft response
response = "I only know lunch places... but you can ask my developer to teach me new things."
# Send response
send_response_back(response, channel)
###
# Start...
###
if __name__ == "__main__":
# Connect to Slack
if slack_client.rtm_connect(with_team_state=False):
print("Lunch Bot connected and running!")
# Read the bot's user ID
lunch_bot_id = slack_client.api_call("auth.test")["user_id"]
# Listen for users requesting the bot's input
while True:
# Read command and channel of the message
user_input = slack_client.rtm_read()
# If the user made a request, respond
if user_input != []:
# Get command and channel
command, channel = parse_bot_commands(user_input)
# Process command and respond
if command:
command = command.lower()
if NEVER_COMMAND in command:
send_never_places(channel)
elif PLACES_COMMAND in command or LIST_COMMAND in command:
send_restaurant_list(channel)
elif LUNCH_COMMAND in command or WHERE_COMMAND in command or AGAIN_COMMAND in command:
send_lunch_suggestions(channel)
else:
send_unknown_response(channel)
time.sleep(RTM_READ_DELAY)
else:
print("Lunch Bot failed to connect...")