-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgeneric_handlers.py
382 lines (362 loc) · 11.9 KB
/
generic_handlers.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
from telegram import (
ReplyKeyboardRemove, Update,
InlineKeyboardButton, InlineKeyboardMarkup
)
from telegram.ext import (
CallbackContext,
ConversationHandler
)
# from cloudinary.uploader import text, upload
# from sendgrid import SendGridAPIClient
# from sendgrid.helpers.mail import Mail
from faunadb import query as q
from faunadb.errors import NotFound
from helpers import (
emailcheck, dispatch_mail
)
from extensions import client
from consts import (
CHOOSING, CLASS_STATE, BIZ_SEARCH, CHOOSE_PREF,
SHOW_STOCKS, ADD_PRODUCTS, SME_DETAILS
)
# inital options
reply_keyboard = [
[
InlineKeyboardButton(
text="SME",
callback_data="SME"
),
InlineKeyboardButton(
text="Customer",
callback_data="Customer"
)
]
]
markup = InlineKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
def start(update, context: CallbackContext) -> int:
print("You called")
bot = context.bot
chat_id = update.message.chat.id
# Check if user already exists before creating new user
try:
_user = client.query(
q.get(
q.match(
q.index("user_by_chat_id"),
chat_id
)
)
)
if _user is not None:
bot.send_message(
chat_id=chat_id,
text="Hi it seems you already have an account with us!"
)
# figure out what kind of user they are
if _user['data']['is_smeowner']:
# find business with user's chat_id
sme = client.query(
q.get(
q.match(
q.index("business_by_chat_id"),
chat_id
)
)
)
if sme:
context.user_data["sme_name"] = sme['data']['name']
context.user_data['sme_cat'] = sme['data']['category']
context.user_data['sme_id'] = sme['ref'].id()
context.user_data['sme_latest'] = sme['data']['latest']
context.user_data['sme_link'] = sme['data']['business_link']
button = [
[
InlineKeyboardButton(
text="Add A New Product",
callback_data=chat_id
)
],
[
InlineKeyboardButton(
text="Get a business link",
callback_data="link"
)
],
[
InlineKeyboardButton(
text="View your catalogue",
callback_data="catalogue"
)
]
]
_markup = InlineKeyboardMarkup(
button,
one_time_keyboard=True
)
bot.send_message(
chat_id=chat_id,
text=f"Welcome back {_user['data']['name']}!",
reply_markup=_markup
)
return ADD_PRODUCTS
else:
context.user_data["user-id"] = _user["ref"].id()
context.user_data["user-name"] = _user['data']['name']
context.user_data['user-data'] = _user['data']
button = [
[
InlineKeyboardButton(
text="View vendors to buy from",
callback_data="customer"
)
],
[
InlineKeyboardButton(
text="Search for a vendor by name",
callback_data="customer;search"
)
]
]
bot.send_message(
chat_id=chat_id,
text=f"Welcome back {_user['data']['name']}",
reply_markup=InlineKeyboardMarkup(button)
)
return CLASS_STATE
except NotFound:
bot.send_message(
chat_id=chat_id,
text="Hi fellow, Welcome to SMEbot ,"
"Please tell me about yourself, "
"provide your full name, email, and phone number, "
"separated by comma each e.g: "
"John Doe, [email protected], +234567897809"
)
return CHOOSING
# get data generic user data from user and store
def choose(update, context):
bot = context.bot
chat_id = update.message.chat.id
# create new data entry
data = update.message.text.split(',')
if len(data) < 3 or len(data) > 3:
bot.send_message(
chat_id=chat_id,
text="Invalid entry, please make sure to input the details "
"as requested in the instructions"
)
bot.send_message(
chat_id=chat_id,
text="Type /start, to restart bot"
)
return ConversationHandler.END
new_user = client.query(
q.do(
# create cart
q.create(
q.collection("Cart"),
{
"data":
{
"items": [],
"chat_id": chat_id
}
}
),
q.create(q.collection('User'), {
"data": {
"name": data[0],
"email": data[1],
"telephone": data[2],
"is_smeowner": False,
"preference": "",
"chat_id": chat_id
}
})
)
)
context.user_data["user-id"] = new_user["ref"].id()
context.user_data["user-name"] = data[0]
context.user_data['user-data'] = new_user['data']
bot.send_message(
chat_id=chat_id,
text="Collected information succesfully!..🎉🎉 \n"
"Which of the following do you identify as ?",
reply_markup=markup
)
if emailcheck(data[1].replace(" ", "")):
dispatch_mail(data[1].replace(" ", ""))
return CLASS_STATE
def classer(update, context):
bot = context.bot
chat_id = update.callback_query.message.chat.id
name = context.user_data["user-name"]
if update.callback_query.data.lower() == "sme":
# update user as smeowner
client.query(
q.update(
q.ref(q.collection("User"), context.user_data["user-id"]),
{"data": {"is_smeowner": True}}
)
)
bot.send_message(
chat_id=chat_id,
text=f"Great! {name}, please tell me about your business, "
"provide your BrandName, Brand email, Address, and phone number"
"in that order, each separated by comma(,) each e.g: "
"JDWears, [email protected], 101-Mike Avenue-Ikeja, +234567897809",
reply_markup=ReplyKeyboardRemove()
)
return SME_DETAILS
categories = [
[
InlineKeyboardButton(
text="Clothing/Fashion",
callback_data="Clothing/Fashion"
),
InlineKeyboardButton(
text="Hardware Accessories",
callback_data="Hardware Accessories"
)
],
[
InlineKeyboardButton(
text="Food/Kitchen Ware",
callback_data="Food/Kitchen Ware"
),
InlineKeyboardButton(
text="ArtnDesign",
callback_data="ArtnDesign"
)
]
]
if 'search' in update.callback_query.data.lower():
bot.send_message(
chat_id=chat_id,
text="Please enter the name of the business you're looking for"
)
return BIZ_SEARCH
bot.send_message(
chat_id=chat_id,
text="Here's a list of categories available"
"Choose one that matches your interest",
reply_markup=InlineKeyboardMarkup(categories)
)
return CHOOSE_PREF
def searcher(update, context):
bot = context.bot
chat_id = update.message.chat.id
data = update.message.text.lower()
# search for business using index
try:
biz = client.query(
q.get(
q.match(
q.index("business_by_name"),
data
)
)
)
button = [
[
InlineKeyboardButton(
text="View Products",
callback_data=biz["data"]["name"]
)
],
[
InlineKeyboardButton(
text="Select for updates",
callback_data="pref" + ',' + biz["data"]["name"]
)
]
]
if "latest" in biz['data'].keys():
thumbnail = client.query(q.get(q.ref(q.collection("Product"), biz["data"]["latest"])))
print(thumbnail)
bot.send_photo(
chat_id=chat_id,
photo=thumbnail["data"]["image"],
caption=f"{biz['data']['name']}",
reply_markup=InlineKeyboardMarkup(button)
)
else:
bot.send_message(
chat_id=chat_id,
text=f"{biz['data']['name']}",
reply_markup=InlineKeyboardMarkup(button)
)
return SHOW_STOCKS
except NotFound:
button = [
[
InlineKeyboardButton(
text="View vendors to buy from",
callback_data="customer"
)
],
[
InlineKeyboardButton(
text="Search for a vendor by name",
callback_data="customer;search"
)
]
]
bot.send_message(
chat_id=chat_id,
text="Oops didn't find any vendor with that name"
"check with your spelling to be sure its correct.",
reply_markup=InlineKeyboardMarkup(button)
)
return CLASS_STATE
# def update_preference(update, context):
# bot = context.bot
# chat_id = update.callback_query.message.chat.id
# data = update.callback_query.data
# if "pref" in data:
# data = data.split(',')[0].replace(' ', '')
# print(data)
# user = client.query(
# q.update(
# q.ref(
# q.match(q.index('user_by_name'), context.user_data['user-data']['name']),
# )
# )
# )
# # update preference
# client.query(
# q.update(
# q.ref(
# q.collection('User'), user['ref'].id(),
# ),
# {'data': {'preference': user['data']['preference']+data+','}}
# )
# )
# button = [
# [
# InlineKeyboardButton(
# text="Vieew more businesses category",
# callback_data='customer'
# )
# ]
# ]
# bot.send_message(
# chat_id=chat_id,
# text="Updated preference successfully!!"
# )
# return CLASS_STATE
# Control
def cancel(update: Update, context: CallbackContext) -> int:
update.message.reply_text(
'Bye! I hope we can talk again some day.',
reply_markup=ReplyKeyboardRemove()
)
return ConversationHandler.END
def search_(update, context):
bot = context.bot
chat_id = update.message.chat.id
bot.send_message(
chat_id=chat_id,
text="Please enter the name of the business you're looking for"
)
return BIZ_SEARCH