-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsme_handlers.py
422 lines (406 loc) · 13.6 KB
/
sme_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
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
412
413
414
415
416
417
418
419
420
421
422
from faunadb.errors import FaunaError
from faunadb import query as q
from helpers import generate_link, parse_product_info
from extensions import client
from consts import (
SME_DETAILS, SME_CAT, SME_CATALOGUE,
ADD_PRODUCTS, POST_VIEW_CATALOGUE
)
from telegram import (
Update,
InlineKeyboardButton, InlineKeyboardMarkup
)
from telegram.ext import (
CallbackContext,
ConversationHandler
)
from cloudinary.uploader import upload
from helpers import update_sme_latest
def business_details(update, context):
bot = context.bot
chat_id = update.message.chat.id
data = update.message.text.split(',')
if len(data) < 4 or len(data) > 4:
bot.send_message(
chat_id=chat_id,
text="Invalid entry, please make sure to input the details "
"as requested in the instructions"
)
return SME_DETAILS
context.user_data["sme_dets"] = data
# categories = [
# ['Clothing/Fashion', 'Hardware Accessories'],
# ['Food/Kitchen Ware', 'ArtnDesign'],
# ['Other']
# ]
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"
)
]
]
markup = InlineKeyboardMarkup(categories, one_time_keyboard=True)
bot.send_message(
chat_id=chat_id,
text="Pick a category for your business from the options",
reply_markup=markup
)
return SME_CAT
def business_details_update(update, context):
bot = context.bot
chat_id = update.callback_query.message.chat.id
choice = update.callback_query.data
biz_link = generate_link(context.user_data["sme_dets"][0])
# create business
new_sme = client.query(
q.create(
q.collection("Business"),
{"data": {
"name": context.user_data["sme_dets"][0].lower(),
"email": context.user_data["sme_dets"][1],
"address": context.user_data["sme_dets"][2],
"telephone": context.user_data["sme_dets"][3],
"category": choice.lower(),
"business_link": biz_link,
"chat_id": chat_id
}}
)
)
context.user_data["sme_name"] = context.user_data["sme_dets"][0]
context.user_data["sme_id"] = new_sme["ref"].id()
context.user_data["sme_cat"] = choice
context.user_data["sme_link"] = biz_link
button = [[
InlineKeyboardButton(
text="Add a product",
callback_data=choice.lower()
)
]]
bot.send_message(
chat_id=chat_id,
text="Business account created successfully, "
"let's add some products shall we!.",
reply_markup=InlineKeyboardMarkup(button)
)
return ADD_PRODUCTS
def add_product(update, context):
bot = context.bot
chat_id = update.callback_query.message.chat.id
if "link" in update.callback_query.data:
bot.send_message(
chat_id=chat_id,
text=context.user_data['sme_link']
)
bot.send_message(
chat_id=chat_id,
text="Here you go!, share the link with people so they can see your store."
)
return ConversationHandler.END
elif "catalogue" in update.callback_query.data:
context.user_data['sme_chat_id'] = chat_id
return SME_CATALOGUE
bot.send_message(
chat_id=chat_id,
text="Add the Name, Description, and Price of product, "
"separated by commas(,) as caption to the product's image"
)
return ADD_PRODUCTS
def product_info(update: Update, context: CallbackContext):
data = update.message
bot = context.bot
photo = bot.getFile(update.message.photo[-1].file_id)
file_ = open('product_image.png', 'wb')
photo.download(out=file_)
data = update.message.caption.split(',')
# upload image to cloudinary
with open('product_image.png', 'rb') as file_:
send_photo = upload(
file_,
width=200, height=150,
crop='thumb'
)
# create new product
newprod = client.query(
q.create(
q.collection("Product"),
{
"data":
{
"name": data[0],
"description": data[1],
"price": float(data[2]),
"image": send_photo["secure_url"],
"sme": context.user_data["sme_name"],
"sme_chat_id": update.message.chat.id,
"category": context.user_data["sme_cat"]
}
}
)
)
# update latest products stack
client.query(
q.let(
{
"biz_stack": q.if_(
q.is_empty(
q.match(
q.index("business-stack_by_name"),
context.user_data["sme_name"]
)
),
q.create(
q.collection('Business_Stack'),
{
'data': {
'name': context.user_data['sme_name'],
'stack': []
}
}
),
q.get(
q.match(
q.index("business-stack_by_name"),
context.user_data["sme_name"]
)
)
)
},
q.update(
q.select(['ref'], q.var('biz_stack')),
{
"data": {
'stack': q.append(
newprod['ref'].id(),
q.select(
['data', 'stack'],
q.var('biz_stack')
)
)
}
}
)
)
)
# update latest on business using stack
update_sme_latest(context.user_data['sme_name'])
# create response
button = [[InlineKeyboardButton(
text='Add another product',
callback_data=context.user_data["sme_name"]
)]]
update.message.reply_text(
"Added product successfully",
reply_markup=InlineKeyboardMarkup(button)
)
return ADD_PRODUCTS
def post_show_catalogue(update, context):
bot = context.bot
chat_id = update.callback_query.message.chat.id
# check for selected option
data = update.callback_query.data
if "Edit" in data:
bot.send_message(
chat_id=chat_id,
text="Kindly add details for the update using the following format, "
"{product_attribute: value} for example {price: 50} or perhaps, "
"{price: 50, description: new description}"
)
context.user_data['product_id'] = data.split(';')[1]
return POST_VIEW_CATALOGUE
# else if user chooses to remove product
# faunadb transaction to delete product and
# update biz latest product
client.query(
q.do(
# delete product
q.delete(
q.ref(
q.collection('Product'),
data.split(';')[1]
)
),
# if product deleted is the last latest
q.let(
{
'biz_stack': q.if_(
q.is_empty(
q.match(
q.index("business-stack_by_name"),
context.user_data['sme_name']
)
),
q.create(
q.collection('Business_Stack'),
{
'data': {
'name': context.user_data['sme_name'],
'stack': []
}
}
),
q.get(
q.match(
q.index("business-stack_by_name"),
context.user_data['sme_name']
)
)
)
},
q.if_(
q.equals(data.split(';')[1], context.user_data['sme_latest']),
q.update(
q.select(
['ref'],
q.var('biz_stack')
),
{
"data": {
"stack": q.reverse(
q.drop(
1,
q.reverse(
q.select(
['data', 'stack'],
q.var('biz_stack')
)
)
)
)
}
}
),
q.var('biz_stack')
)
)
)
)
# update the sme's latest product
update_sme_latest(context.user_data['sme_name'])
button = [
[
InlineKeyboardButton(
text="Go back to catalogue",
callback_data=context.user_data['sme_name']
)
]
]
bot.send_message(
chat_id=chat_id,
text="Removed product from catalogue successfully! 🗑️",
reply_markup=InlineKeyboardMarkup(button)
)
return SME_CATALOGUE
def update_product_info(update, context):
bot = context.bot
chat_id = update.message.chat.id
data = parse_product_info(update.message.text)
button = [
[
InlineKeyboardButton(
text="Go back to catalogue",
callback_data=context.user_data['sme_name']
)
]
]
if data is False:
bot.send_message(
chat_id=chat_id,
text="Invalid Entry, please try again",
reply_markup=InlineKeyboardMarkup(button)
)
return SME_CATALOGUE
# if it parsed the data correctly then we can update the product info
try:
client.query(
q.update(
q.ref(
q.collection('Product'),
context.user_data['product_id']
),
{'data': data}
)
)
bot.send_message(
chat_id=chat_id,
text="Updated product details succesfully!",
reply_markup=InlineKeyboardMarkup(button)
)
return SME_CATALOGUE
except FaunaError as e:
bot.send_message(
chat_id=chat_id,
text="An error occurred while trying to update the product, "
"please try again!.",
reply_markup=InlineKeyboardMarkup(button)
)
print(e)
return SME_CATALOGUE
def show_catalogue(update, context):
bot = context.bot
chat_id = context.user_data['sme_chat_id']
# fetch products owned by business
products = client.query(
q.map_(
lambda x: q.get(x),
q.paginate(
q.match(
q.index("product_by_business"),
context.user_data['sme_name']
)
)
)
)
if len(products['data']) == 0:
button = [
[
InlineKeyboardButton(
text="Add products to your catalogue",
callback_data=chat_id
)
]
]
bot.send_message(
chat_id=chat_id,
text="Hi! 😃 you don't seem to have added any products yet!.",
reply_markup=InlineKeyboardMarkup(button)
)
return ADD_PRODUCTS
for product in products["data"]:
context.user_data["sme_name"] = product['data']['sme']
button = [
[
InlineKeyboardButton(
text="Edit Info",
callback_data="Edit;" + product["ref"].id()
)
],
[
InlineKeyboardButton(
text="Delete product from catalogue",
callback_data="Delete;" + product["ref"].id()
)
]
]
bot.send_photo(
chat_id=chat_id,
photo=product["data"]["image"],
caption=f"{product['data']['name']} \nDescription: {product['data']['description']}\nPrice:{product['data']['price']}",
reply_markup=InlineKeyboardMarkup(button)
)
return POST_VIEW_CATALOGUE