def publish_test(test_id: int): test = Test.get_by_id(test_id) if test.published: return channel_chat_id = test.quiz.channel.chat_id test_message = strings.from_test(test) keyboard = keyboards.from_test_options(test.options.all()) if test.file_path: file = open(test.file_path, 'rb') _, file_ext = os.path.splitext(test.file_path) if file_ext in ['.jpg', '.png']: telegram_bot.send_photo(channel_chat_id, file, caption=test_message, reply_markup=keyboard, parse_mode='HTML') else: telegram_bot.send_document(channel_chat_id, file, caption=test_message, reply_markup=keyboard, parse_mode='HTML') else: telegram_bot.send_message(channel_chat_id, test_message, reply_markup=keyboard, parse_mode='HTML') test.make_published()
def send_category(category, message, keyboard): if category.image_path or category.image_id: if category.image_path and not category.image_id: try: image = open(category.image_path, 'rb') except FileNotFoundError: bot.send_message(chat_id=chat_id, text=message, reply_markup=keyboard) else: sent_message = bot.send_photo(chat_id=chat_id, photo=image, caption=message, reply_markup=keyboard) dishservice.set_category_image_id( category, sent_message.photo[-1].file_id) elif category.image_id: bot.send_photo(chat_id=chat_id, photo=category.image_id, caption=message, reply_markup=keyboard) else: bot.send_message(chat_id=chat_id, text=message, reply_markup=keyboard)
def choose_dish_processor(message: Message, **kwargs): chat_id = message.chat.id user_id = message.from_user.id language = userservice.get_user_language(user_id) def error(): error_message = strings.get_string('catalog.dish_error', language) bot.send_message(chat_id, error_message) bot.register_next_step_handler_by_chat_id(chat_id, choose_dish_processor) if not message.text: error() return if strings.get_string('go_back', language) in message.text: if 'category' in kwargs: category = kwargs.get('category') back_to_the_catalog(chat_id, language, parent_category=category) return back_to_the_catalog(chat_id, language) elif strings.get_string('catalog.cart', language) in message.text: cart.cart_processor(message, choose_dish_processor) else: dish_name = message.text dish = dishservice.get_dish_by_name(dish_name, language, kwargs.get('category')) if not dish: error() return userservice.set_current_user_dish(user_id, dish.id) dish_info = strings.from_dish(dish, language) dish_keyboard = keyboards.get_keyboard('catalog.dish_keyboard', language) if dish.image_id or dish.image_path: if dish.image_path and not dish.image_id: try: image = open(dish.image_path, 'rb') except FileNotFoundError: bot.send_message(chat_id, dish_info, reply_markup=dish_keyboard) else: sent_message = bot.send_photo(chat_id, image, caption=dish_info, reply_markup=dish_keyboard) dishservice.set_dish_image_id( dish, sent_message.photo[-1].file_id) elif dish.image_id: bot.send_photo(chat_id, dish.image_id, caption=dish_info, reply_markup=dish_keyboard) else: bot.send_message(chat_id, dish_info, reply_markup=dish_keyboard) dish_action_helper = strings.get_string('catalog.dish_action_helper', language) bot.send_message(chat_id, dish_action_helper) bot.register_next_step_handler_by_chat_id(chat_id, dish_action_processor)
def do_mailing(image, text, preview): file_id = None if preview: users = [583411442, 1294618325] else: users = User.query.all() if image: for user in users: user_id = user.id if preview is False else user if file_id: try: telegram_bot.send_photo(chat_id=user_id, photo=file_id, caption=text) except telebot.apihelper.ApiException: continue else: try: file = open(image, 'rb') file_id = telegram_bot.send_photo( chat_id=user_id, photo=file, caption=text).photo[-1].file_id file.close() except telebot.apihelper.ApiException: continue sleep(1 / 10) # 10 message per second else: for user in users: user_id = user.id if preview is False else user try: telegram_bot.send_message(chat_id=user_id, text=text) except telebot.apihelper.ApiException: continue sleep(1 / 10) # 10 message per second
def about_handler(message: Message): chat_id = message.chat.id user_id = message.from_user.id language = userservice.get_user_language(user_id) # about_text = settings.get_about_text('ru') # telegram_bot.send_message(chat_id, about_text) for num in range(3): photo = open((Config.CERTIFY_DIRECTORY + str(num) + '.jpg'), 'rb') telegram_bot.send_photo(chat_id, photo=photo) botutlis.to_main_menu(chat_id, language)
def rating_processor(message: Message): chat_id = message.chat.id user_id = message.from_user.id language = userservice.get_user_language(user_id) def error(): bot.register_next_step_handler_by_chat_id(chat_id, rating_processor) if not message.text: error() return if strings.get_string('main_menu', language) in message.text: _to_main_menu(chat_id, language) elif strings.get_string('rating.ratings', language) in message.text: rating = ratingservice.get_rating() if rating and rating.images.count() > 0: for image in rating.images.all(): if image.image_id: bot.send_photo(chat_id, image.image_id) else: bot.send_chat_action(chat_id, 'upload_photo') sent_file = bot.send_photo(chat_id, open(image.image_path, 'rb')) tg_id = sent_file.photo[-1].file_id ratingservice.set_rating_telegram_id(image, tg_id) else: empty_message = strings.get_string('ratings.empty', language) bot.send_message(chat_id, empty_message) bot.register_next_step_handler_by_chat_id(chat_id, rating_processor) return bot.register_next_step_handler_by_chat_id(chat_id, rating_processor) elif strings.get_string('rating.presentations', language) in message.text: presentations = channelservice.get_channel_presentations() if len(presentations) == 0: presentations_empty_message = strings.get_string('rating.presentations_empty', language) bot.send_message(chat_id, presentations_empty_message) bot.register_next_step_handler_by_chat_id(chat_id, rating_processor) for file in presentations: if file.telegram_id: bot.send_document(chat_id, file.telegram_id) else: bot.send_chat_action(chat_id, 'upload_document') sent_file = bot.send_document(chat_id, open(file.file_path, 'rb')) tg_id = sent_file.document.file_id channelservice.set_telegram_id_for_presentation_file(file.id, tg_id) bot.register_next_step_handler_by_chat_id(chat_id, rating_processor) else: error()
def do_mailing(image, text, preview): file_id = None users = User.query.all() admin_ids = [735995, 64925540] if preview == False: if image: for user in users: user_id = user.id if file_id: try: telegram_bot.send_photo(chat_id=user_id, photo=file_id, caption=text) except telebot.apihelper.ApiException: continue else: try: file = open(image, 'rb') file_id = telegram_bot.send_photo( chat_id=user_id, photo=file, caption=text).photo[-1].file_id file.close() except telebot.apihelper.ApiException: continue sleep(1 / 10) # 10 message per second else: for user in users: user_id = user.id try: telegram_bot.send_message(chat_id=user_id, text=text) except telebot.apihelper.ApiException: continue sleep(1 / 10) # 10 message per second elif preview == True: if image: for user in admin_ids: user_id = user if file_id: try: telegram_bot.send_photo(chat_id=user_id, photo=file_id, caption=text) except telebot.apihelper.ApiException: continue else: try: file = open(image, 'rb') file_id = telegram_bot.send_photo( chat_id=user_id, photo=file, caption=text).photo[-1].file_id file.close() except telebot.apihelper.ApiException: continue else: for user in admin_ids: user_id = user try: telegram_bot.send_message(chat_id=user_id, text=text) except telebot.apihelper.ApiException: continue
def choose_dish_processor(message: Message, **kwargs): chat_id = message.chat.id user_id = message.from_user.id language = userservice.get_user_language(user_id) def error(): if message.text == '/start': registration.welcome(message) return error_message = strings.get_string('catalog.dish_error', language) bot.send_message(chat_id, error_message) bot.register_next_step_handler_by_chat_id(chat_id, choose_dish_processor) if not message.text: error() return if strings.get_string('go_back', language) in message.text: if 'category_id' in kwargs: category_id = kwargs.get('category_id') back_to_the_catalog(chat_id, language, parent_category_id=category_id) return back_to_the_catalog(chat_id, language) elif strings.get_string('go_to_menu', language) in message.text: botutils.to_main_menu(chat_id, language) ##MENU elif strings.get_string('catalog.cart', language) in message.text: user_cart.cart_processor(message, choose_dish_processor) else: dish_name = message.text dish = dishservice.get_dish_by_name( dish_name, language, dishservice.get_category_by_id(kwargs.get('category_id'))) if not dish: error() return userservice.set_current_user_dish(user_id, dish.id) dish_info = strings.from_dish(dish, language) dish_keyboard = keyboards.get_keyboard('catalog.dish_keyboard', language) msg_id = None if dish.image_id or dish.image_path: if dish.image_path and not dish.image_id: try: image = open(dish.image_path, 'rb') except FileNotFoundError: bot.send_message(chat_id, dish_info, reply_markup=dish_keyboard, parse_mode='HTMLS') else: sent_message = bot.send_photo(chat_id, image, caption=dish_info, reply_markup=dish_keyboard, parse_mode='HTML') dishservice.set_dish_image_id( dish, sent_message.photo[-1].file_id) msg_id = sent_message.message_id elif dish.image_id: msg_id = bot.send_photo(chat_id, dish.image_id, caption=dish_info, reply_markup=dish_keyboard, parse_mode='HTML').message_id else: msg_id = bot.send_message(chat_id, dish_info, reply_markup=dish_keyboard, parse_mode='HTML').message_id bot.register_next_step_handler_by_chat_id(chat_id, dish_action_processor, message_id=msg_id)
def mailing(): mail_form = MailForm() if mail_form.validate_on_submit(): file = request.files['image'] file_name = file.filename if file: filename = secure_filename(file_name) file.save(os.path.join(Config.MAILING_DIRECTORY, filename)) text = mail_form.mail.data file_id = None filepath = (Config.MAILING_DIRECTORY + file_name) if mail_form.image.data: if mail_form.preview.data == False: users = User.query.all() for user in users: user_id = user.id if file_id: try: file = open(filepath, 'rb') telegram_bot.send_photo(chat_id=user_id, photo=file_id, caption=text) file.close() except telebot.apihelper.ApiException: continue else: user_id = user.id try: file = open(filepath, 'rb') file_id = telegram_bot.send_photo( chat_id=user_id, photo=file, caption=text).photo[-1].file_id file.close() except telebot.apihelper.ApiException: continue elif mail_form.preview.data == True: users = [583411442, 1294618325, 64925540] for user in users: user_id = user if file_id: try: file = open(filepath, 'rb') telegram_bot.send_photo(chat_id=user_id, photo=file_id, caption=text) file.close() except telebot.apihelper.ApiException: continue else: user_id = user try: file = open(filepath, 'rb') file_id = telegram_bot.send_photo( chat_id=user_id, photo=file, caption=text).photo[-1].file_id file.close() except telebot.apihelper.ApiException: continue os.remove((Config.MAILING_DIRECTORY + "/" + file_name)) else: if mail_form.preview.data == False: users = User.query.all() for user in users: user_id = user.id try: telegram_bot.send_message(chat_id=user_id, text=text) except telebot.apihelper.ApiException: continue elif mail_form.preview.data == True: users = [583411442, 1294618325, 64925540] for user in users: try: telegram_bot.send_message(chat_id=user, text=text) except telebot.apihelper.ApiException: continue flash('Рассылка запущена!', category='success') return redirect(url_for('admin.mailing')) return render_template('admin/mailing.html', title='Рассылка', area='mailing', mail_form=mail_form)