-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
411 lines (362 loc) · 15 KB
/
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# -*- coding: utf-8 -*-
import os
from slackclient import SlackClient
from flask import jsonify
import json
from app_factory import mongo
from pprint import pprint
import frichti_api
import nestor_api
import popchef_api
import pickles_api
import urllib
import utils
import copy
import logging
from random import shuffle
# Instantiate logger instance
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
PROVIDER_CHOICES = [{
'tag': 'frichti',
'name': 'Frichti',
'color': '#f7e77a',
'has_api': True,
'message': 'Voir le menu !',
'thumb_url': frichti_api.FRICHTI_LOGO
},
# {
# 'tag': 'popchef',
# 'name': 'Popchef',
# 'color': '#1ec28e',
# 'has_api': True,
# 'message': 'Voir le menu !',
# 'thumb_url': popchef_api.POPCHEF_LOGO
# },
# {
# 'tag': 'ubereats',
# 'name': 'UberEats',
# 'color': '#1c95a5',
# 'has_api': False,
# 'website_url': "https://www.ubereats.com",
# 'message': 'Allez sur le site !',
# 'thumb_url': "http://cdn2.ubergizmo.com/wp-content/uploads/2016/01/ubereats-logo-640x640.jpg"
# },
# {
# 'tag': 'foodora',
# 'name': 'Foodora',
# 'color': '#d51965',
# 'has_api': False,
# 'website_url': "https://www.foodora.fr",
# 'message': 'Allez sur le site !',
# 'thumb_url': "http://www.chez-dang.com/wp-content/uploads/2017/02/foodora_web.png"
# },
# {
# 'tag': 'deliveroo',
# 'name': 'Deliveroo',
# 'color': '#21ccbe',
# 'has_api': False,
# 'website_url': "https://deliveroo.fr/fr/",
# 'message': 'Allez sur le site !',
# 'thumb_url': "http://www.underconsideration.com/brandnew/archives/deliveroo_logo.png"
# },
{
'tag': 'pickles',
'name': 'Pickles',
'color': '#6da440',
'has_api': True,
'website_url': "https://www.pickles.fr/",
'message': 'Montre moi le menu !',
'thumb_url': pickles_api.PICKLES_LOGO
},
{
'tag': 'nestor',
'name': 'Nestor',
'color': '#ef6537',
'has_api': True,
'website_url': "https://www.nestorparis.com/",
'message': 'Montre moi le menu !',
'thumb_url': nestor_api.NESTOR_LOGO
}]
# TODO : Ajouter un boutton pour revenir en arriere a chaque etape
# TODO : Understand kitchen id in frichti api
class FoodSlackingBot(object):
""" Instanciates a Bot object to handle Slack interactions."""
def __init__(self):
super(FoodSlackingBot, self).__init__()
self.name = "food_slacking_bot"
self.emoji = ":robot_face:"
self.id = ''
# When we instantiate a new bot object, we can access the app
# credentials we set earlier in our local development environment.
self.oauth = {"client_id": os.environ.get("CLIENT_ID"),
"client_secret": os.environ.get("CLIENT_SECRET"),
# Scopes provide and limit permissions to what our app
# can access. It's important to use the most restricted
# scope that your app will need.
"scope": "bot"}
self.verification = os.environ.get("VERIFICATION_TOKEN")
self.client = SlackClient("")
def auth(self, code):
"""
Authenticate with OAuth and assign correct scopes.
Save a dictionary of authed team information in memory on the bot
object.
Parameters
----------
code : str
temporary authorization code sent by Slack to be exchanged for an
OAuth token
"""
# After the user has authorized this app for use in their Slack team,
# Slack returns a temporary authorization code that we'll exchange for
# an OAuth token using the oauth.access endpoint
auth_response = self.client.api_call(
"oauth.access",
client_id=self.oauth["client_id"],
client_secret=self.oauth["client_secret"],
code=code
)
# To keep track of authorized teams and their associated OAuth tokens,
# we will save the team ID and bot tokens to the global
# authed_teams object
pprint(auth_response)
logging.info("Saving new credentials to database :\n - team_id: " +
auth_response["team_id"] + "\n - team_name: " + auth_response["team_name"])
mongo.db.credentials.update({
'team_id': auth_response["team_id"]
}, {
'$set': {'team_id': auth_response["team_id"],
'team_name': auth_response["team_name"],
'access_token': auth_response["access_token"],
'bot_id': auth_response["bot"]["bot_user_id"],
'bot_token': auth_response["bot"]["bot_access_token"]
}
}, True)
# Reconnect the Slack Client with the correct bot token
self.client = SlackClient(auth_response["bot"]["bot_access_token"])
def authToCorrectTeam(self, team_id):
team_credentials = mongo.db.credentials.find_one({'team_id': team_id})
logger.info("Connecting slack client to :\n - team_name: " + team_credentials['team_name'] + "\n - team_id: " +
team_id)
self.client = SlackClient(team_credentials['bot_token'])
self.id = team_credentials['bot_id']
def getAtBot(self):
return '<@' + self.id + '>'
def display_providers(self, team, channel):
logger.info("postMessage : display provider selection buttons")
text = "Chez qui voulez-vous commander aujourd'hui ?"
# Build attachments
attachments = []
shuffle(PROVIDER_CHOICES) # Randomize order
for provider in PROVIDER_CHOICES:
if provider['has_api']:
new_attachment = {
"title": provider['name'],
"callback_id": "food_provider_selection",
"color": provider['color'],
"thumb_url": provider['thumb_url'],
"attachment_type": "default",
"actions": [{
"name": "choice",
"text": provider['message'],
"type": "button",
"value": provider['tag']
}]
}
else:
# If no API, just put a link to the website !
new_attachment = {
"title": provider['name'],
"text": provider['website_url'],
"callback_id": "food_provider_selection",
"color": provider['color'],
"thumb_url": provider['thumb_url'],
"attachment_type": "default",
}
attachments.append(new_attachment)
attachments = json.dumps(attachments)
self.client.api_call("chat.postMessage", token=team, channel=channel,
text=text,
attachments=attachments,
as_user=True)
def post_message(self, team, channel, message):
logger.info("postMessage : " + message)
self.client.api_call("chat.postMessage", token=team,
channel=channel, text=message, as_user=True)
def format_menu_categories(self, provider, categories):
# Returns a replacement response to replace the active Slack message,
# and 'other_responses' which will appear as new messages
# TODO : Define a model structure for categories param
provider_URLS = self.get_provider_URLS(provider)
if not categories:
# If not categories available for this day (holidays for example)
response = {
'attachments': [{
'text': "Aucune catégorie disponible aujourd'hui ! Retentez votre chance demain :)",
"author_name": provider.title(),
"author_link": provider_URLS['BASE_URL'],
"author_icon": provider_URLS['LOGO_URL']
}]
}
return response
response = {
'text': 'Choisissez une catégorie pour connaitre ses choix :',
'attachments': [{
"author_name": provider.title(),
"author_link": provider_URLS['BASE_URL'],
"author_icon": provider_URLS['LOGO_URL']
}]
}
attachment_template = {
'color': '36a64f',
'pretext': "",
'callback_id': 'menu_category_selection',
'actions': []
}
new_attachment = copy.deepcopy(attachment_template)
for idx, category in enumerate(categories):
new_action = {
'name': 'choice',
'text': category['label'],
'type': 'button',
'value': provider + '/' + category['tag']
}
new_attachment['actions'].append(new_action)
if (idx + 1) % 5 == 0:
response['attachments'].append(new_attachment)
new_attachment = copy.deepcopy(attachment_template)
response['attachments'].append(new_attachment)
return response
def format_propositions(self, provider, propositions):
# Returns a replacement response to replace the active Slack message,
# and 'other_responses' which will appear as new messages
# TODO : Define a model structure for propositions param
# response = {
# "text": "Liste des boissons Frichti : :",
# "attachments": [
# {
# "title": "Evian",
# "title_link": "http://df",
# "fields": [
# {
# "value": "Eau minérale naturelle finement pétillante.",
# "short": true
# }
# ],
# "thumb_url": "https://cdn.shopify.com/s/files/1/0832/9391/products/evian.jpg?v=1487582835",
# "color": "#7CD197"
# }
# ]
# }
provider_URLS = self.get_provider_URLS(provider)
# If not proposition in the selected category
if not propositions:
response = {
'attachments': [{
'text': "Rien à manger ici aujourd'hui ! Retentez votre chance demain :)",
"author_name": provider.title(),
"author_link": provider_URLS['BASE_URL'],
"author_icon": provider_URLS['LOGO_URL']
}]
}
return response
if propositions[0]['category_label'].strip()[-1] in ['s', '!', ')']:
pluralized_category = propositions[0]['category_label']
else:
pluralized_category = propositions[0]['category_label'] + 's'
response = {
"attachments": [{
"author_name": provider.title() + " : " + pluralized_category,
"author_link": provider_URLS['BASE_URL'],
"author_icon": provider_URLS['LOGO_URL']
}]
}
attachment_template = {
"title": "",
"title_link": "",
"fields": [
{
# Short description
"value": "",
"short": False
}, {
# Price
"title": "",
"short": False
}
],
"thumb_url": "",
"color": None
}
for proposition in propositions:
new_attachment = copy.deepcopy(attachment_template)
new_attachment['title'] = proposition['title']
new_attachment[
'title_link'] = provider_URLS['PRODUCT_BASE_URL'] + proposition['productId']
new_attachment['fields'][0][
'value'] = proposition['shortDescription']
if proposition['price']:
new_attachment['fields'][1][
'title'] = ("Prix : " + str(proposition['price']) + " €")
if provider == 'nestor':
new_attachment['fields'][1]['title'] += " (Menu complet)"
new_attachment['thumb_url'] = proposition['image']['url']
new_attachment['color'] = utils.random_color()
response['attachments'].append(new_attachment)
return response
def get_provider_URLS(self, provider):
# Used to get useful base urls for each provider
if provider == 'frichti':
return {
"BASE_URL": frichti_api.FRICHTI_BASE_URL,
"LOGO_URL": frichti_api.FRICHTI_LOGO,
"PRODUCT_BASE_URL": frichti_api.FRICHTI_BASE_URL + '/p/'
}
elif provider == 'popchef':
return {
"BASE_URL": popchef_api.POPCHEF_BASE_URL,
"LOGO_URL": popchef_api.POPCHEF_LOGO,
"PRODUCT_BASE_URL": popchef_api.POPCHEF_BASE_URL + '/p/'
}
elif provider == 'pickles':
return {
"BASE_URL": pickles_api.PICKLES_BASE_URL,
"LOGO_URL": pickles_api.PICKLES_LOGO,
"PRODUCT_BASE_URL": pickles_api.PICKLES_BASE_URL
}
elif provider == 'nestor':
return {
"BASE_URL": nestor_api.NESTOR_BASE_URL,
"LOGO_URL": nestor_api.NESTOR_LOGO,
"PRODUCT_BASE_URL": nestor_api.NESTOR_BASE_URL
}
def ask(self, provider, query, param1=None):
response = "Should never happen !"
if provider == 'frichti':
response = frichti_api.ask_frichti(query, param1)
if query == 'menu_categories':
logging.info("postMessage: menu_categories for " + provider)
response = self.format_menu_categories(provider, response)
elif query == 'propositions':
logging.info("postMessage: propositions for " + provider)
response = self.format_propositions(provider, response)
elif provider == 'popchef':
response = popchef_api.ask_popchef(query, param1)
if query == 'menu_categories':
logging.info("postMessage: menu_categories for " + provider)
response = self.format_menu_categories(provider, response)
elif query == 'propositions':
logging.info("postMessage: propositions for " + provider)
response = self.format_propositions(provider, response)
elif provider == 'pickles':
response = pickles_api.ask_pickles(query, 'only_category')
if query == 'menu_categories':
logging.info("postMessage: propositions for " + provider)
response = self.format_propositions(provider, response)
elif provider == 'nestor':
response = nestor_api.ask_nestor(query, 'only_category')
if query == 'menu_categories':
logging.info("postMessage: propositions for " + provider)
response = self.format_propositions(provider, response)
return response