def notifyUsers(bot, update, product_id): photo = db.getGalleryItem(product_id)['file_id'] # get all users id from db user_ids = users.getAllUsersId() # notify each user for user_id in user_ids: logging.info("Sending notification to {}".format(user_id)) # Avoid sending notification to the admin if is_admin(user_id): continue bot.send_message( chat_id=user_id, text="There is a new item in our Store:", ) bot.send_photo(chat_id=user_id, photo=photo) text = productDetails(product_id) bot.send_message( chat_id=user_id, text=text, parse_mode='Markdown', )
def productDetails(product_id): product = db.getGalleryItem(product_id) product.pop('file_id') product.pop('id') text = '' if product['title'] is not None: text += "*{}*\n".format(product['title']) product.pop('title') if product['price'] is not None: text += "Price: {}\n".format(product['price']) product.pop('price') text += '_' * 20 + "\n" for key, value in product.items(): text += "{}: {}\n".format(key.capitalize(), value) return text
def info(bot, update): query = update.callback_query data = query['data'] # Format: job_id item_id = data.split('_')[1] import db galleryItam = db.getGalleryItem(item_id) keyboardBtn = [] if galleryItam is None: logging.info('No item found in database with id={}'.format(item_id)) text = "No Item found (or wrong item id was sent)" else: photo_id = galleryItam['id'] text = """*{}* _By: {}_ *Size:* {} cm *Price:* {} IRR""".format( galleryItam['title'], galleryItam['by'], galleryItam['size'], galleryItam['price'], ) keyboardBtn.append([ (InlineKeyboardButton( text='Buy', callback_data="buy_{}".format(photo_id), )), ]) keyboard = InlineKeyboardMarkup(keyboardBtn) bot.edit_message_text(chat_id=query.message.chat_id, text=text, parse_mode='Markdown', message_id=query.message.message_id, reply_markup=keyboard)
def info(bot, update): query = update.callback_query data = query['data'] # Format: job_id item_id = data.split('_')[1] import db galleryItam = db.getGalleryItem(item_id) keyboardBtn = [] if galleryItam == None: logging.info('No item found in database with id={}'.format(item_id)) text = "No Item found (or wrong item id was sent)" else: photo_id = galleryItam['id'] title = galleryItam['title'] # The following information is for presentation. Later, all of them # must text = """*{}* _By: Mostafa Ahangarha_ *Size:* 100x70 cm *On:* Sep 15, 2017 *Price:* $ 30""".format(title) keyboardBtn.append([ (InlineKeyboardButton(text='Buy', callback_data="buy_{}".format(photo_id))) ]) keyboard = InlineKeyboardMarkup(keyboardBtn) bot.edit_message_text(chat_id=query.message.chat_id, text=text, parse_mode='Markdown', message_id=query.message.message_id, reply_markup=keyboard)
def uploadPhoto(bot, update): keyValues = {} # dictionary containing key-values to be saved in db # There are three versions of the uploaded photos. photo[0] is the lightest # and the photo [2] is the largest in terms of size. Since we don't need # large files we will only save the light version. Otherwise we need to # save all the files_ids in an array file_id = update.message.photo[0].file_id caption = update.message.caption # caption structure is as key1:val1\nkey2:val2\n... keyValues['file_id'] = file_id rows = caption.split('\n') for row in rows: key, value = row.split(':', 1) key = key.strip().lower() value = value.strip() keyValues[key] = value # TODO: Make sure the necessary data (title and price) are provided product_id = db.add_new_item(keyValues) if product_id is False: text = "Not successfull! :(" else: text = "Added successfully :)\n\nInto:\n" text += productDetails(product_id) bot.send_photo(chat_id=update.message.chat_id, photo=db.getGalleryItem(product_id)['file_id']) bot.send_message( chat_id=update.message.chat_id, text=text, parse_mode='Markdown', ) # Send a post to registered users and norify them about the new product notifyUsers(bot, update, product_id)