def callback_handler(call, user): pet = Pet.objects(pet_id=re.search('[0-9]+', call.data).group()).first() if pet: if 'apply' in call.data: pet.view = True pet.save() user = User.objects(user_id=pet.user_id).first() if user: bot.send_message(user.user_id, DICTIONARY[user.language]['applied_msg']) else: user = User.objects(user_id=pet.user_id).first() pet.delete() if user: bot.send_message(user.user_id, DICTIONARY[user.language]['denied_msg']) bot.edit_message_reply_markup(chat_id=call.message.chat.id, message_id=call.message.message_id) else: bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text='X') bot.edit_message_reply_markup(chat_id=call.message.chat.id, message_id=call.message.message_id, reply_markup=types.ReplyKeyboardRemove()) bot.answer_callback_query(call.id)
def parents_without_children_state(message, user, is_entry=False): if is_entry: bot.send_message( message.chat.id, DICTIONARY['ua']['parents_without_children_msg'], reply_markup=get_parents_without_children_keyboard('ua')) else: if message.text == DICTIONARY['ua']['all_about_nush_btn']: return True, 'all_about_nush_state' elif message.text == DICTIONARY['ua']['excursion_button']: bot.send_photo(message.chat.id, photo=open('img/excursion.png', 'rb'), caption=DICTIONARY['ua']['excursion_msg'], reply_markup=get_excursion_button_keyboard('ua')) elif message.text == DICTIONARY['ua']['ask_mon_question_btn']: return True, 'ask_mon_question_state' elif message.text == DICTIONARY['ua']['rating_mon_question_btn']: return True, 'rating_mon_question_state' elif message.text == DICTIONARY['ua']['back_button']: return True, 'parents_state' else: bot.send_message( message.chat.id, DICTIONARY['ua']['no_button'], reply_markup=get_parents_without_children_keyboard('ua')) return False, ''
def set_main_api_key_state(message, user, is_entry=False): if is_entry: bot.send_message(message.chat.id, DICTIONARY['ru']['set_api_key_msg']) else: bitmex_acc = BitmexAccount(api_key=message.text, is_main=True) bitmex_acc.save() return True, 'set_main_api_secret_state' return False, ''
def login_state(message, user, is_entry=False): if is_entry: bot.send_message(message.chat.id, DICTIONARY['ru']['welcome_msg'].format( user.first_name), reply_markup=categories_inline_keyboard(user=user)) else: return True, 'main_menu_state' return False, ''
def set_main_api_secret_state(message, user, is_entry=False): if is_entry: bot.send_message(message.chat.id, DICTIONARY['ru']['set_api_secret_msg']) else: bitmex_acc = BitmexAccount.objects(is_main=True).first() bitmex_acc.api_secret = message.text bitmex_acc.save() bot.send_message(message.from_user.id, DICTIONARY['add_account_success_btn']) return True, 'main_menu_state' return False, ''
def help_state(message, user, is_entry=False): if is_entry: bot.send_message(message.chat.id, DICTIONARY[user.language]['help_money_msg'], reply_markup=get_back_menu_keyboard(user.language)) else: if message.text == DICTIONARY[user.language]['back_menu_btn']: return True, 'main_menu_state' else: return True, 'main_menu_state' return False, ''
def ask_mon_question_state(message, user, is_entry=False): if is_entry: bot.send_message(message.chat.id, DICTIONARY['ua']['ask_mon_question_msg'], parse_mode="HTML", reply_markup=get_ask_mon_keyboard('ua')) else: if message.text == DICTIONARY['ua']['back_button']: return return_to_your_state(user) else: user.current_question = message.text user.save() return True, 'question_confirmation_state' return False, ''
def upgrade_qualification_state(message, user, is_entry=False): if is_entry: bot.send_message(message.chat.id, DICTIONARY['ua']['upgrade_qualification_msg'], reply_markup=get_upgrade_qualification_keyboard('ua')) else: if message.text == DICTIONARY['ua']['lectures_button']: bot.send_message( message.chat.id, DICTIONARY['ua']['lectures_msg'], parse_mode="HTML", reply_markup=get_upgrade_qualification_keyboard('ua')) elif message.text == DICTIONARY['ua']['online_courses_button']: bot.send_message( message.chat.id, DICTIONARY['ua']['online_courses_msg'], parse_mode="HTML", reply_markup=get_upgrade_qualification_keyboard('ua')) elif message.text == DICTIONARY['ua']['back_button']: return True, 'teachers_state' else: bot.send_message( message.chat.id, DICTIONARY['ua']['no_button'], reply_markup=get_upgrade_qualification_keyboard('ua')) return False, ''
def set_city_state(message, user, is_entry=False): if is_entry: bot.send_message( message.chat.id, DICTIONARY[user.language]['set_city_msg'], reply_markup=get_skip_keyboard(language=user.language)) else: if message.text == DICTIONARY[user.language]['skip_btn']: bot.send_message(message.chat.id, DICTIONARY[user.language]['signed_up_msg']) return True, 'main_menu_state' else: # check if given city exists in database current_cities = session.query(City.name).all() current_cities = [i for obj in current_cities for i in obj] if message.text in current_cities: user.city = message.text session.commit() bot.send_message(message.chat.id, DICTIONARY[user.language]['signed_up_msg']) return True, 'main_menu_state' else: bot.send_message(message.chat.id, DICTIONARY[user.language]['no_city_msg']) return True, 'main_menu_state' return False, ''
def main_menu_state(message, user, is_entry=False): if is_entry: bot.send_message(message.chat.id, DICTIONARY[user.language]['mainmenu_msg'], reply_markup=get_main_menu_keyboard( role=user.role, language=user.language)) else: if message.text == DICTIONARY[user.language]['my_events_btn']: if user_events := get_events_for_user(user): for event in user_events: events_msg = format_events_as_message(event, user.language) bot.send_message(message.chat.id, text=events_msg, parse_mode='Markdown', reply_markup=get_unsubscribe_keyboard( obj_id=event.id, language=user.language)) else: events_msg = DICTIONARY[user.language]['no_events_msg'] bot.send_message(message.chat.id, text=events_msg) return False, 'main_menu_state' elif message.text == DICTIONARY[user.language]['get_events_btn']: # send message with list of events if events := get_events_from_db_for_user(user): events_ids = [obj.id for obj in events] events_msg = format_events_as_message(events, user.language) bot.send_message(message.chat.id, text=events_msg, parse_mode='Markdown', reply_markup=events_inline_keyboard( events_ids, user, language=user.language))
def universal_callback_query_handler(reply, user): if reply.data == DICTIONARY['ua']['excursion_1_button']: bot.send_photo(reply.from_user.id, photo=open('img/lego.jpg', 'rb'), caption=DICTIONARY['ua']['excursion_1_msg'], reply_markup=get_inline_back_keyboard('ua')) elif reply.data == DICTIONARY['ua']['excursion_2_button']: bot.send_photo(reply.from_user.id, photo=open('img/party.jpg', 'rb'), caption=DICTIONARY['ua']['excursion_2_msg'], reply_markup=get_inline_back_keyboard('ua')) elif reply.data == DICTIONARY['ua']['excursion_3_button']: bot.send_photo(reply.from_user.id, photo=open('img/vidpochynok.jpg', 'rb'), caption=DICTIONARY['ua']['excursion_3_msg'], reply_markup=get_inline_back_keyboard('ua')) elif reply.data == DICTIONARY['ua']['excursion_4_button']: bot.send_photo(reply.from_user.id, photo=open('img/stendy.jpeg', 'rb'), caption=DICTIONARY['ua']['excursion_4_msg'], reply_markup=get_inline_back_keyboard('ua')) elif reply.data == DICTIONARY['ua']['excursion_5_button']: bot.send_photo(reply.from_user.id, photo=open('img/drawing.jpg', 'rb'), caption=DICTIONARY['ua']['excursion_5_msg'], reply_markup=get_inline_back_keyboard('ua')) elif reply.data == DICTIONARY['ua']['back_to_class_button']: bot.send_photo(reply.from_user.id, photo=open('img/excursion.png', 'rb'), caption=DICTIONARY['ua']['excursion_msg'], reply_markup=get_excursion_button_keyboard('ua')) # button for questions' rating elif reply.data == DICTIONARY['ua']['back_button']: bot.send_photo(reply.from_user.id, photo=open('img/excursion.png', 'rb'), caption=DICTIONARY['ua']['excursion_msg'], reply_markup=get_excursion_button_keyboard('ua')) else: question = Question.objects(question_id=reply.data).first() if question is None: pass else: if user.user_id not in question.subscribed_users: question.subscribed_users.append(user.user_id) question.rating += 1 question.save() else: bot.send_message(reply.from_user.id, "Ви вже підтримали це питання.") bot.answer_callback_query(reply.id)
def add_pet_sex_state(message, user, is_entry=False): if is_entry: bot.send_message(message.chat.id, DICTIONARY[user.language]['add_pet_sex_msg'], reply_markup=get_add_pet_sex_keyboard(user.language)) else: if message.text == DICTIONARY[user.language]['male_pet_btn']: pet = Pet.objects(pet_id=user.current_pet).first() pet.sex = False pet.save() bot.send_message(message.chat.id, DICTIONARY[user.language]['ok_msg']) return True, 'add_pet_age_state' elif message.text == DICTIONARY[user.language]['female_pet_btn']: pet = Pet.objects(pet_id=user.current_pet).first() pet.sex = True pet.save() bot.send_message(message.chat.id, DICTIONARY[user.language]['ok_msg']) return True, 'add_pet_age_state' elif message.text == DICTIONARY[user.language]['back_menu_btn']: pet = Pet.objects(pet_id=user.current_pet).first() if pet: pet.delete() return True, 'main_menu_state' else: bot.send_message(message.chat.id, DICTIONARY[user.language]['use_keyboard_msg'], reply_markup=get_add_pet_sex_keyboard( user.language)) return False, ''
def choose_language_state(message, user, is_entry=False): if is_entry: bot.send_message(message.chat.id, DICTIONARY['ww']['choose_language_message'], reply_markup=get_languages_keyboard('ww')) else: if message.text == DICTIONARY['ww']['ru_button']: user.language = 'ru' user.save() bot.send_message( message.chat.id, DICTIONARY[user.language]['saved_language_message']) return True, 'main_menu_state' elif message.text == DICTIONARY['ww']['ua_button']: user.language = 'ua' user.save() bot.send_message( message.chat.id, DICTIONARY[user.language]['saved_language_message']) return True, 'main_menu_state' else: bot.send_message(message.chat.id, DICTIONARY['ww']['use_buttons_warning_message'], reply_markup=get_languages_keyboard('ww')) return False, ''
def send_support_respond_state(message, user, is_entry=False): if is_entry: bot.send_message( message.chat.id, DICTIONARY[user.language]['admin_respond_msg'], reply_markup=get_back_keyboard(language=user.language)) else: if message.text == DICTIONARY[user.language]['back_btn']: return True, 'admin_menu_state' else: bot.send_message( message.chat.id, DICTIONARY[user.language]['send_support_respond_msg']) return True, 'admin_menu_state' return False, ''
def sender_state(message, user, is_entry=False): if not user.is_admin: return True, 'main_menu_state' if is_entry: bot.send_message( message.chat.id, DICTIONARY[user.language]['enter_message_to_send_msg'], reply_markup=get_back_menu_keyboard(user.language)) else: if message.text == DICTIONARY[user.language]['back_menu_btn']: return True, 'admin_state' else: Thread(target=send_message_to_users, args=[bot, User.objects, message.text]).start() return True, 'admin_state' return False, ''
def change_city_state(message, user, is_entry=False): if is_entry: bot.send_message(message.chat.id, "Введи название города:", reply_markup=get_back_keyboard(language='ru')) else: if message.text == DICTIONARY['ru']['back_btn']: return True, 'settings_menu_state' else: # TODO: check if given city exists in Ukraine user.city = message.text session.commit() bot.send_message(message.chat.id, "Город успешно обновлен") return True, 'main_menu_state' return False, ''
def thematic_sections_state(message, user, is_entry=False): if is_entry: bot.send_message(message.chat.id, DICTIONARY['ua']['thematic_sections_msg'], reply_markup=get_thematic_sections_keyboard('ua')) else: if message.text == DICTIONARY['ua']['old_system_diff_btn']: return True, 'difference_with_old_system_state' if message.text == DICTIONARY['ua']['shortly_about_nush_btn']: return True, 'shortly_about_nush_state' elif message.text == DICTIONARY['ua']['back_button']: return True, 'all_about_nush_state' else: bot.send_message(message.chat.id, DICTIONARY['ua']['no_button'], reply_markup=get_thematic_sections_keyboard('ua')) return False, ''
def question_confirmation_state(message, user, is_entry=False): if is_entry: bot.send_message(message.chat.id, DICTIONARY['ua']['question_confirmation_msg'] % user.current_question, parse_mode="Markdown", reply_markup=get_question_confirmation_keyboard('ua')) else: if message.text == DICTIONARY['ua']['question_confirmation_btn']: question = Question(text=user.current_question, subscribed_users=[]) question.save() bot.send_message(message.chat.id, "Дякуємо Вам за питання!") return return_to_your_state(user) elif message.text == DICTIONARY['ua']['question_decline_btn']: return True, 'ask_mon_question_state' return False, ''
def handle_location(message): try: schools = get_closest_school(message.location.latitude, message.location.longitude) if not schools: bot.send_message(message.chat.id, DICTIONARY['ua']['no_schools_nearby_msg']) else: for school in schools: bot.send_location(message.chat.id, school['position']['lat'], school['position']['lon']) bot.send_message( message.chat.id, DICTIONARY['ua']['school_nearby_msg'].format( school['poi']['name'], school['address']['freeformAddress'])) except Exception as e: print(e)
def set_language_state(message, user, is_entry=False): if is_entry: bot.send_message(message.chat.id, DICTIONARY[user.language]['set_lang_msg'].format( user.first_name), reply_markup=get_languages_keyboard()) else: if message.text == DICTIONARY['ua_lang_btn']: user.language = 'ua' session.commit() return True, 'login_state' elif message.text == DICTIONARY['ru_lang_btn']: user.language = 'ru' session.commit() return True, 'login_state' else: return True, 'set_language_state' return False, ''
def parents_state(message, user, is_entry=False): if is_entry: bot.send_message(message.chat.id, DICTIONARY['ua']['is_children_msg'], reply_markup=get_parents_choose_keyboard('ua')) else: if message.text == DICTIONARY['ua']['is_children_button']: user.with_child_in_school = True user.save() return True, 'parents_with_children_state' elif message.text == DICTIONARY['ua']['no_children_button']: user.with_child_in_school = False user.save() return True, 'parents_without_children_state' else: bot.send_message(message.chat.id, DICTIONARY['ua']['no_button'], reply_markup=get_parents_choose_keyboard('ua')) return False, ''
def admin_state(message, user, is_entry=False): if not user.is_admin: return True, 'main_menu_state' if is_entry: bot.send_message(message.chat.id, DICTIONARY[user.language]['admin_msg'], reply_markup=get_admin_keyboard(user.language)) else: if message.text == DICTIONARY[user.language]['sender_btn']: return True, 'sender_state' elif message.text == DICTIONARY[user.language]['adm_info_btn']: pass elif message.text == DICTIONARY[user.language]['back_menu_btn']: return True, 'main_menu_state' else: bot.send_message(message.chat.id, DICTIONARY[user.language]['use_keyboard_msg'], reply_markup=get_admin_keyboard(user.language)) return False, ''
def admin_settings_state(message, user, is_entry=False): if is_entry: bot.send_message( message.chat.id, 'Ви можете додати нові міста та змінити категорії', reply_markup=get_admin_settings_keyboard(language=user.language)) else: if message.text == DICTIONARY[user.language]['back_btn']: return True, 'admin_menu_state' elif message.text == DICTIONARY[user.language]['settings_city_btn']: return True, 'admin_edit_cities_state' # elif message.text == DICTIONARY[user.language]['settings_categories_btn']: # return True, 'admin_edit_cities_state' else: bot.send_message( message.chat.id, DICTIONARY[user.language]['send_support_respond_msg']) return True, 'admin_menu_state' return False, ''
def support_menu_state(message, user, is_entry=False): if is_entry: bot.send_message( message.chat.id, DICTIONARY[user.language]['support_menu_msg'], reply_markup=get_back_keyboard(language=user.language)) else: if message.text == DICTIONARY[user.language]['back_btn']: return True, 'main_menu_state' else: support_request = SupportRequest(user_id=user.id, message=message.text[:300]) session.add(support_request) session.commit() bot.send_message( message.chat.id, DICTIONARY[user.language]['sent_support_request_msg']) return True, 'main_menu_state' return False, ''
def main_menu_state(message, user, is_entry=False): if is_entry: bot.send_message(message.chat.id, DICTIONARY[user.language]['main_menu_msg'], reply_markup=get_main_menu_keyboard(user.language)) else: if message.text == DICTIONARY[user.language]['wanna_take_btn']: return True, 'want_take_pet_state' elif message.text == DICTIONARY[user.language]['wanna_leave_btn']: return True, 'add_pet_kind_state' elif message.text == DICTIONARY[user.language]['lost_btn']: return True, 'lost_pet_state' elif message.text == DICTIONARY[user.language]['wanna_help_btn']: return True, 'help_state' elif message.text == DICTIONARY[user.language]['info_btn']: bot.send_message(message.chat.id, DICTIONARY[user.language]['in_progress_msg']) elif message.text == DICTIONARY['ww']['admin_command']: return True, 'admin_state' else: bot.send_message(message.chat.id, DICTIONARY[user.language]['use_keyboard_msg'], reply_markup=get_main_menu_keyboard( user.language)) return False, ''
def add_pet_photo_state(message, user, is_entry=False): if is_entry: bot.send_message(message.chat.id, DICTIONARY[user.language]['add_pet_photo_msg'], reply_markup=get_back_menu_keyboard(user.language)) else: if message.text == DICTIONARY[user.language]['back_menu_btn']: pet = Pet.objects(pet_id=user.current_pet).first() if pet: pet.delete() return True, 'main_menu_state' else: if message.photo: pet = Pet.objects(pet_id=user.current_pet).first() pet.photo_link = message.photo[-1].file_id pet.save() bot.send_message(message.chat.id, DICTIONARY[user.language]['ok_msg']) return True, 'add_pet_confirmation_state' else: bot.send_message( message.chat.id, DICTIONARY[user.language]['photo_not_valid_msg']) return True, 'add_pet_photo_state' return False, ''
def admin_edit_cities_state(message, user, is_entry=False): current_cities = session.query(City.name).all() current_cities = [i for obj in current_cities for i in obj] if is_entry: bot.send_message( message.chat.id, 'Щоб змінити список міст, скопіюйте наступне повідомлення, та відредагуйте його.\nПоточний список міст:', reply_markup=get_back_keyboard(language=user.language)) bot.send_message(message.chat.id, '\n'.join(current_cities)) else: if message.text == DICTIONARY[user.language]['back_btn']: return True, 'admin_settings_state' else: new_cities = [] for city in message.text.split('\n'): if city not in current_cities: new_cities.append({ 'name': city, 'is_regional_center': False }) session.execute( insert(City).values(new_cities).on_conflict_do_nothing()) session.commit() bot.send_message(message.chat.id, 'Список міст оновлено') return True, 'admin_settings_state' return False, ''
def add_admin_state(message, user, is_entry=False): if is_entry: bot.send_message( message.chat.id, DICTIONARY[user.language]['add_admin_msg'], reply_markup=get_back_keyboard(language=user.language)) else: if message.text == DICTIONARY[user.language]['back_btn']: return True, 'admin_menu_state' else: # get admin username admin_username = message.text admin_username = admin_username.replace('@', '') print(f'{admin_username=}') # check if user with such username exists in db obj = session.query(User).filter_by( username=admin_username).first() if obj: print('user already exists') if obj.role == Roles.admin: print('admin already exists') bot.send_message( message.chat.id, DICTIONARY[user.language]['admin_exists_msg']) else: print('set admin role to user') # set role to admin obj.role = Roles.admin obj.save() session.commit() bot.send_message( message.chat.id, DICTIONARY[user.language]['admin_exists_msg']) else: print('create new admin user') # create new user with admin role admin = User(username=admin_username, role=Roles.admin, state='set_language_state') session.add(admin) session.commit() bot.send_message( message.chat.id, DICTIONARY[user.language]['admin_created_msg']) return True, 'admin_menu_state' return False, ''
def admin_menu_state(message, user, is_entry=False): if is_entry: bot.send_message( message.chat.id, DICTIONARY[user.language]['admin_menu_msg'], reply_markup=get_admin_menu_keyboard(language=user.language)) else: if message.text == DICTIONARY[user.language]['support_btn']: bot.send_message( message.chat.id, DICTIONARY[user.language]['admin_support_menu_msg']) # send list of support requests with inline button "Respond" per each s_requests = get_support_requests() for obj in s_requests: bot.send_message( message.chat.id, text=obj.message, reply_markup=get_admin_support_request_keyboard( obj_id=obj.id, language=user.language)) return False, 'admin_menu_state' elif message.text == DICTIONARY[user.language]['clear_events_btn']: # deleted_events = delete_events() sleep(15) bot.send_message( message.chat.id, "1 подія була видалена з бази" # DICTIONARY[user.language]['clear_events_success_msg'].format(deleted_events) ) return False, 'admin_menu_state' elif message.text == DICTIONARY[user.language]['add_admin_btn']: return True, 'add_admin_state' elif message.text == DICTIONARY[user.language]['settings_btn']: return True, 'admin_settings_state' if message.text == DICTIONARY[user.language]['mainmenu_msg']: return True, 'main_menu_state' else: bot.send_message(message.chat.id, DICTIONARY[user.language]['no_button']) return False, ''
def add_pet_confirmation_state(message, user, is_entry=False): if is_entry: pet = Pet.objects(pet_id=user.current_pet).first() message_answer = DICTIONARY[user.language]['print_info_msg'].format( pet.kind, (pet.name if pet.name is not None else DICTIONARY[user.language]['add_pet_no_name_btn']), (DICTIONARY[user.language]['female_pet_btn'] if pet.sex else DICTIONARY[user.language]['male_pet_btn']), pet.breed, pet.age, pet.description, pet.user_id) bot.send_photo(message.chat.id, photo=pet.photo_link, caption=message_answer, parse_mode='HTML', reply_markup=get_add_pet_confirmation_keyboard( user.language)) else: if message.text == DICTIONARY[user.language]['yes_btn']: pet = Pet.objects(pet_id=user.current_pet).first() # pet.view = True pet.save() bot.send_photo( ADMIN_CHAT_ID, photo=pet.photo_link, caption=DICTIONARY[user.language]['print_info_msg'].format( pet.kind, (pet.name if pet.name is not None else DICTIONARY[user.language]['add_pet_no_name_btn']), (DICTIONARY[user.language]['female_pet_btn'] if pet.sex else DICTIONARY[user.language]['male_pet_btn']), pet.breed, pet.age, pet.description, pet.user_id), reply_markup=get_moder_keyboard(language=user.language, callback_data=pet.pet_id), parse_mode='html') bot.send_message(message.chat.id, DICTIONARY[user.language]['add_ok_pet_msg']) return True, 'main_menu_state' elif message.text == DICTIONARY[user.language]['no_btn']: pet = Pet.objects(pet_id=user.current_pet).first() if pet: pet.delete() bot.send_message(message.chat.id, DICTIONARY[user.language]['add_no_pet_msg']) return True, 'main_menu_state' elif message.text == DICTIONARY[user.language]['back_menu_btn']: pet = Pet.objects(pet_id=user.current_pet).first() if pet: pet.delete() return True, 'main_menu_state' else: bot.send_message(message.chat.id, DICTIONARY[user.language]['use_keyboard_msg'], reply_markup=get_add_pet_confirmation_keyboard( user.language)) return False, ''