def callback_remove_all_daily(call): user = User.query.filter_by(chat_id=call.from_user.id).first() all_reminders = Reminder.query.filter_by(user_id=user.id, is_phenomenon=False).all() for reminder in all_reminders: scheduler.remove_job( job_id=reminder.job_id, jobstore='default') # remove the time from schedule db.session.delete(reminder) # remove the time from db db.session.commit() try: bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=f"{hints['time daily'][user.language]}", reply_markup=gen_markup_hours( user_id=user.id, is_phenomenon=False, lang=user.language)) except: pass finally: bot.answer_callback_query( callback_query_id=call.id, show_alert=False, text=f"{hints['schedule delete'][user.language]}")
def callback_phenomenon(call): """ handle phenomenon db add phenomenon to db """ user = User.query.filter_by(chat_id=call.from_user.id).first() phenomenon_data = call.data[11:] phenomenon = Phenomenon.query.filter_by(phenomenon=phenomenon_data, user_id=user.id).first() try: db.session.delete(phenomenon) except UnmappedInstanceError as e: logger.error( f'The phenomenon has not been found. Creating new one\n{e}') new_phenomenon = Phenomenon(phenomenon=phenomenon_data, user_id=user.id) db.session.add(new_phenomenon) text = hints['phenomenon set'][user.language] else: text = hints['phenomenon delete'][user.language] db.session.commit() bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=hints['phenomena intro'][user.language], reply_markup=gen_markup_phenomena( user.id, user.language)) bot.answer_callback_query( callback_query_id=call.id, show_alert=False, text=f"{hints['phenomenon set del'][user.language]} " f"{phenomenon_button_names[phenomenon_data][user.language]} {text}")
def callback_all_phenomena(call): """ handle inline button 'all phenomena' add all phenomena to db """ user = User.query.filter_by(chat_id=call.from_user.id).first() all_ph_from_db = Phenomenon.query.filter_by(user_id=user.id, value=None).all() if len(all_ph_from_db) == 7: for ph in all_ph_from_db: db.session.delete(ph) text = hints['all untick'][user.language] else: for ph in phenomena_list: new_phenomenon = Phenomenon.query.filter_by( phenomenon=ph, user_id=user.id).first() if new_phenomenon is None: new_phenomenon = Phenomenon(user_id=user.id, phenomenon=ph) db.session.add(new_phenomenon) text = hints['all tick'][user.language] db.session.commit() bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=hints['phenomena intro'][user.language], reply_markup=gen_markup_phenomena( user.id, user.language)) bot.answer_callback_query(callback_query_id=call.id, show_alert=False, text=text)
def callback_inline_back_ph(call): """handle back to phenomenon button""" user = User.query.filter_by(chat_id=call.message.chat.id).first() bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=hints['phenomena intro'][user.language], reply_markup=gen_markup_phenomena( user.id, user.language))
def callback_inline_back(call): """handle back to hours button""" user = User.query.filter_by(chat_id=call.from_user.id).first() user_id = user.id bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=hints['time daily'][user.language], reply_markup=gen_markup_daily(user_id))
def callback_button_manually(call): """handle inline button 'manually'""" user = User.query.filter_by(chat_id=call.from_user.id).first() bot.edit_message_text( chat_id=call.message.chat.id, message_id=call.message.message_id, text=hints['phenomena manually intro'][user.language], reply_markup=gen_markup_phenomena_manually(user.id, user.language))
def callback_back_manually_phenomena(call): """ handle all manually phenomena db add all man phenomena to db """ user = User.query.filter_by(chat_id=call.from_user.id).first() bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=hints['phenomena intro'][user.language], reply_markup=gen_markup_phenomena( user.id, user.language))
def callback_phenomenon_time(call): """handle phenomenon inline keyboard time setting (hours)""" user = User.query.filter_by(chat_id=call.from_user.id).first() markup = gen_markup_hours(user_id=user.id, is_phenomenon=True, lang=user.language, callback='_ph') bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=hints['phenomena time'][user.language], reply_markup=markup)
def callback_worker(call): cid = call.message.chat.id if call.data in dicts.keys(): db.change_lang(cid, call.data) bot.edit_message_text(chat_id=cid, message_id=call.message.message_id, text=t('5', call.data)) if call.data in fiat.keys(): db.change_currency(cid, call.data) lang = db.get_lang(cid) bot.edit_message_text(chat_id=cid, message_id=call.message.message_id, text=t('11', lang))
def callback_inline_daily_min(call): """handle daily inline keyboard (minutes)""" user = User.query.filter_by(chat_id=call.from_user.id).first() hours = call.data[:2] markup = gen_markup_minutes(user_id=user.id, hours=hours, is_phenomenon=False, lang=user.language) bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=hints['time daily'][user.language], reply_markup=markup)
def poster(bot, chatId, text=None, buttons=None, ed=False, message_id=None, doc=None, img=None, inline=False, lenRow=None): if buttons: if ed and not img and not doc: bot.edit_message_text(chat_id=chatId, message_id=message_id, text=text, reply_markup=keyboarder( buttons, inline, lenRow)) else: if img: bot.send_photo(chat_id=chatId, photo=img, reply_markup=keyboarder(buttons, inline, lenRow)) if text: bot.send_message(chatId, text, reply_markup=keyboarder( buttons, inline, lenRow)) if doc: bot.send_document(chat_id=chatId, data=doc, reply_markup=keyboarder( buttons, inline, lenRow)) else: if ed and not img and not doc: bot.edit_message_text(chat_id=chatId, message_id=message_id, text=text) else: if img: bot.send_photo(chat_id=chatId, photo=img) if text: bot.send_message(chatId, text) if doc: bot.send_document(chat_id=chatId, data=doc)
def callback_inline_language(call): """Handle button 'language'""" user = User.query.filter_by(chat_id=call.message.chat.id).first() new_lang = call.data[:2] user.language = new_lang db.session.commit() try: bot.edit_message_text( chat_id=call.message.chat.id, message_id=call.message.message_id, text=hints['lang intro'][user.language], reply_markup=gen_markup_language(user_id=user.id)) except ApiException as e: # bad request logger.warning( f'Bad request. The output message has not been changed\n({e})') else: bot.send_message(chat_id=call.message.chat.id, text=f'{hints["lang chosen"][new_lang]}', reply_markup=call_settings_keyboard(lang=new_lang))
def buttons_callback(call): with Database() as db: cur_pos = db.get_cur_search_pos(call.message.chat.id) search_data = db.get_search_data(call.message.chat.id) new_pos = cur_pos grade = 0 if call.data == 'back': if cur_pos >= 1: new_pos -= 1 elif call.data == 'fwd': if cur_pos < len(search_data) - 1: new_pos += 1 elif call.data == 'like': grade = 1 elif call.data == 'dis': grade = -1 if new_pos != cur_pos: with Database() as db: db.change_cur_search_pos(call.message.chat.id, new_pos) keyboard = telebot.types.InlineKeyboardMarkup(4) key_back = telebot.types.InlineKeyboardButton('⬅', callback_data='back') key_fwd = telebot.types.InlineKeyboardButton('➡', callback_data='fwd') key_like = telebot.types.InlineKeyboardButton('👍', callback_data='like') key_dis = telebot.types.InlineKeyboardButton('👎', callback_data='dis') keyboard.add(key_back, key_fwd, key_like, key_dis) msg = 'Фильм: {}\nРейтинг: {}/10'.\ format(search_data[new_pos][0], search_data[new_pos][1]) bot.edit_message_text(msg, call.message.chat.id, call.message.message_id, reply_markup=keyboard) elif grade != 0: title, rating, dir_id = search_data[cur_pos] with Database() as db: db.set_grade(call.message.chat.id, title, dir_id, grade)
def callback_all_manual_phenomena(call): """handle all manually phenomena db add a manual phenomena to db""" user = User.query.filter_by(chat_id=call.from_user.id).first() manual_phenomena_from_db = Phenomenon.query.filter_by( user_id=user.id, is_manually=True).all() for ph in manual_phenomena_from_db: db.session.delete(ph) db.session.commit() try: bot.edit_message_text( chat_id=call.message.chat.id, message_id=call.message.message_id, text=hints['phenomena manually intro'][user.language], reply_markup=gen_markup_phenomena_manually(user.id, user.language)) except: pass finally: bot.answer_callback_query( callback_query_id=call.id, show_alert=False, text=f"{hints['remove manually'][user.language]}")
def callbacks(call): chat_id = call.message.chat.id message_id = call.message.message_id data = call.data.split() user = Users.query.filter(Users.chat_id == chat_id).first() if data[0] == 'sub_info': sub = Subscriptions.query.filter(Subscriptions.data == data[1]).first() text = f'{sub.title}\n\n{sub.description}\nАктивна {sub.work_time} дней\nСтоимость: {sub.price}' keyboard = Keyboards.buy_button(sub.data) bot.edit_message_text(chat_id=chat_id, message_id=message_id, text=text, reply_markup=keyboard) elif data[0] == 'my_subs_info': text = '' subs = get_user_subs(user_id=user.id) for row in subs: sub = Subscriptions.query.filter( Subscriptions.id == row[1]).first() sub_buy_time = str(row[2])[0:-7] sub_buy_time = datetime.strptime(sub_buy_time, '%Y-%m-%d %H:%M:%S') time_left = sub_buy_time.second + (sub.work_time * 24 * 60 * 60) time_left = time_left - datetime.now().second if time_left < 0: del_sub(user_id=user.id, sub_id=sub.id) left_str = 'Подписка истекла' else: time_left = time_left / 60 / 60 if time_left > 24: time_left = time_left / 24 left_str = f'Подписка заканчивается через {round(time_left)} дней' else: left_str = f'Подписка заканчивается через {round(time_left)} часов' text += f'===={sub.title}====\n\n{sub.description}\n{left_str}\n\n' if text == '': bot.edit_message_text(chat_id=chat_id, message_id=message_id, text='У вас нет ни одной подписки') else: bot.edit_message_text(chat_id=chat_id, message_id=message_id, text=text) elif data[0] == 'sub_buy': text = '' subs = get_user_subs(user_id=user.id) for row in subs: sub = Subscriptions.query.filter( Subscriptions.id == row[1]).first() if sub.data == data[1]: text = 'У вас уже приобретина данная подписка' if text == '': sub = Subscriptions.query.filter_by(data=data[1]).first() db.session.execute(user_subs.insert().values( user_id=user.id, sub_id=sub.id, buy_date=datetime.now())) db.session.commit() text = 'Поздравляю! Подписка приобретина' bot.edit_message_text(chat_id=chat_id, message_id=message_id, text=text) elif data[0] == 'info_about': bot.edit_message_text(chat_id=chat_id, message_id=message_id, text='Скоро будет готово')