-
Notifications
You must be signed in to change notification settings - Fork 2
/
wolt_notifier.py
184 lines (137 loc) · 5.57 KB
/
wolt_notifier.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
import json
import os
import requests
import threading
from flask import Flask
from flask import request
from slack import WebClient
CHECK_INTERVAL = 5.0
SLACK_TOKEN = os.getenv('SLACK_TOKEN')
if SLACK_TOKEN is None:
print('SLACK_TOKEN is not defined in environment variables')
exit(1)
SLACK_CLIENT = WebClient(SLACK_TOKEN)
URL_SEARCH = 'https://restaurant-api.wolt.com/v1/search?sort=releveancy&q=%s'
URL_REST_INFO = 'https://restaurant-api.wolt.com/v3/venues/slug/%s'
SCHEDULED_CHECKS = {}
app = Flask(__name__)
def send_message(user_id, text):
SLACK_CLIENT.chat_postMessage(
channel=user_id,
text=text,
response_type="in_channel"
)
def check():
if SCHEDULED_CHECKS:
print(f'Processing {str(len(SCHEDULED_CHECKS))} jobs...', flush=True)
users_to_delete = []
for user_id in SCHEDULED_CHECKS:
try:
response = requests.get(URL_REST_INFO % SCHEDULED_CHECKS[user_id])
response.raise_for_status()
result = response.json()['results'][0]
# Prefer Hebrew name
try:
rest_name = list(filter(lambda x: x["lang"] == "he", result["name"]))[
0]["value"]
except:
rest_name = list(filter(lambda x: x["lang"] == "en", result["name"]))[
0]["value"]
is_online = result['online']
if is_online:
order_url = result['public_url']
send_message(
user_id,
f'Yay! :sunglasses: *{rest_name}* is available for orders <{order_url}|here>.'
)
users_to_delete.append(user_id)
except Exception as e:
print(f'Unable to process job (User ID: {user_id}, Slug: {SCHEDULED_CHECKS[user_id]}: {str(e)}', flush=True)
send_message(
user_id,
'Woops, something went wrong on our side! :scream_cat: Please reschedule your notification.'
)
users_to_delete.append(user_id)
for user_id in users_to_delete:
del SCHEDULED_CHECKS[user_id]
print(f'Done ({str(len(users_to_delete))})', flush=True)
t = threading.Timer(CHECK_INTERVAL, check)
t.daemon = True
t.start()
def find_restaurant(query):
response = requests.get(URL_SEARCH % query)
response.raise_for_status()
results = response.json()['results']
ret = []
for result in results[:10]:
# Prefer Hebrew name
try:
rest_name = list(filter(lambda x: x["lang"] == "he", result["value"]["name"]))[
0]["value"]
except:
rest_name = list(filter(lambda x: x["lang"] == "en", result["value"]["name"]))[
0]["value"]
slug = result['value']['slug']
ret.append((rest_name, slug))
return ret
@app.route('/', methods=['POST'])
def regular_callback():
user_id = request.form['user_id']
command = request.form['command']
text = request.form['text']
channel_id = request.form['channel_id']
if command == '/wolt':
if text == 'cancel':
if user_id in SCHEDULED_CHECKS:
del SCHEDULED_CHECKS[user_id]
return 'I removed your scheduled notification! :smile: Type `/wolt [restaurant_name]` to start again!'
if user_id in SCHEDULED_CHECKS:
return 'It seems there is already a scheduled notification set for you :cry:. Type `/wolt cancel` to cancel it!'
try:
results = find_restaurant(text)
except:
return "Oops, something went wrong! :cry: Please try again."
if not results:
return "I didn't find any results matching this restaurant name :cry:. Try to be more specific!"
attachments = [
{
"fallback": "If you could read this message, you'd be choosing something fun to do right now.",
"color": "#3AA3E3",
"attachment_type": "default",
"callback_id": "game_selection",
"actions": [
{
"name": "rest_list",
"text": "Pick a restaurant...",
"type": "select",
"options": [
]
}
]
}
]
for result in results:
attachments[0]['actions'][0]['options'].append(
{
"text": result[0],
"value": f'{result[0]};{result[1]}'
},
)
return {
'text': "Choose a restaurant from the list and I will notify you when it's available for orders!",
'attachments': attachments
}
@app.route("/interactive_callback", methods=["POST"])
def interactive_callback():
payload = json.loads(request.form['payload'])
user_id = payload['user']['id']
user_name = payload['user']['name']
pair = payload['actions'][0]['selected_options'][0]['value'].split(';')
rest_name = pair[0]
slug = pair[1]
SCHEDULED_CHECKS[user_id] = slug
print(f"Scheduled notification for user '{user_name} for restaurant {rest_name} ({slug})", flush=True)
return f'Awesome! I will notify you as soon as {rest_name} is available for orders! :smile:'
if __name__ == '__main__':
check()
app.run(debug=True, host='0.0.0.0', port=80)