def less_suburbans_handler(call_back): from_to_stations = call_back.message.text.split("\n\n")[0].split(" => ") from_station_title = from_to_stations[0] to_station_title = from_to_stations[1] from_station = all_stations[from_station_title] to_station = all_stations[to_station_title] server_datetime = datetime.today() + server_timedelta data = get_yandex_timetable_data(from_station, to_station, server_datetime) answer = data["answer"] update_keyboard = InlineKeyboardMarkup(True) if data["is_OK"]: if data["is_tomorrow"]: inline_answer = emoji["warning"] + " На сегодня нет электричек" bot.answer_callback_query(call_back.id, inline_answer, show_alert=True) update_keyboard.row(*[ InlineKeyboardButton(text=name, callback_data=name) for name in ["Все на завтра"] ]) else: update_keyboard.row(*[ InlineKeyboardButton(text=name, callback_data=name) for name in ["Оставшиеся", "Обновить"] ]) try: bot.edit_message_text(text=answer, chat_id=call_back.message.chat.id, message_id=call_back.message.message_id, parse_mode="HTML", reply_markup=update_keyboard) except ApiException: pass
def change_home_station_handler(call_back): if "домашнюю" in call_back.message.text: type_station = "Домашняя" is_home = True else: type_station = "Университетская" is_home = False current_station = func.get_station_code(call_back.message.chat.id, is_home) current_another_station = func.get_station_code(call_back.message.chat.id, not is_home) station_title = func.get_key_by_value(all_stations, call_back.data) answer = "{0} станция изменена на <b>{1}</b>\n".format(type_station, station_title) func.change_station(call_back.message.chat.id, call_back.data, is_home=is_home) if call_back.data == current_another_station: func.change_station(call_back.message.chat.id, current_station, is_home=not is_home) inline_answer = "{0} Изменены обе станции!".format(emoji["warning"]) bot.answer_callback_query(callback_query_id=call_back.id, text=inline_answer, show_alert=True) bot.edit_message_text(text=answer, chat_id=call_back.message.chat.id, message_id=call_back.message.message_id, parse_mode="HTML")
def show_full_info(call_back): answer = special_thanks bot.edit_message_text(text=answer, chat_id=call_back.message.chat.id, message_id=call_back.message.message_id, parse_mode="HTML", disable_web_page_preview=True) inline_answer = "И тебе :)" bot.answer_callback_query(call_back.id, inline_answer, cache_time=1)
def button_handler(call): if call.from_user.id == my_id: try: if call.data[0] == 'l': ids = call.data[2:].split('_') source_id = int(ids[0]) post_id = int(ids[1]) if vk.likes.isLiked(type='post', owner_id=source_id, item_id=post_id )['liked'] == 0: #Если запись не лайкнута if call.data[1] == 'n': #И кнопка соотвествует vk.likes.add( type='post', owner_id=source_id, item_id=post_id) #То лайкаем, и меняем кнопку bot.edit_message_reply_markup( my_id, call.message.message_id, reply_markup=create_markup(call.data[2:], liked=True)) bot.answer_callback_query(call.id, 'Лайкнуто') elif call.data[ 1] == 'y': #А если кнопка лжет, приводим кнопку в соответствие и уведомляем. Ниже аналогично bot.edit_message_reply_markup( my_id, call.message.message_id, reply_markup=create_markup(call.data[2:], liked=False)) bot.answer_callback_query( call.id, 'А ты уже убрал лайк в вк, лол') else: if call.data[1] == 'y': vk.likes.delete(type='post', owner_id=source_id, item_id=post_id) bot.edit_message_reply_markup( my_id, call.message.message_id, reply_markup=create_markup(call.data[2:], liked=False)) bot.answer_callback_query(call.id, 'Лайк убран') elif call.data[1] == 'n': bot.edit_message_reply_markup( my_id, call.message.message_id, reply_markup=create_markup(call.data[2:], liked=True)) bot.answer_callback_query( call.id, 'А ты уже лайкнул в вк, лол') elif call.data[0] == 'p': p.add('https://vk.com/wall' + call.data[1:]) bot.answer_callback_query(call.id, 'Добавлено в покет') except Exception as e: alarm(e)
def build_trail_handler(call_back): bot_msg = bot.edit_message_text(text="{0}\U00002026".format( choice(loading_text["ya_timetable"])), chat_id=call_back.message.chat.id, message_id=call_back.message.message_id) from_station_title = call_back.message.text.split("\n")[0].split(": ")[-1] to_station_title = call_back.message.text.split("\n")[1].split(": ")[-1] from_station = all_stations[from_station_title] to_station = all_stations[to_station_title] if call_back.data == "Завтра": server_datetime = datetime.combine( (datetime.today() + timedelta(days=1)).date(), time()) limit = 7 else: server_datetime = datetime.today() + server_timedelta limit = 3 data = get_yandex_timetable_data(from_station, to_station, server_datetime, limit) answer = data["answer"] update_keyboard = InlineKeyboardMarkup(True) if data["is_OK"]: if call_back.data == "Завтра" or data["is_tomorrow"]: if data["is_tomorrow"]: inline_answer = emoji["warning"] + " На сегодня нет электричек" bot.answer_callback_query(call_back.id, inline_answer, show_alert=True) update_keyboard.row(*[ InlineKeyboardButton(text=name, callback_data=name) for name in ["Все на завтра"] ]) else: update_keyboard.row(*[ InlineKeyboardButton(text=name, callback_data=name) for name in ["Оставшиеся", "Обновить"] ]) bot.edit_message_text(text=answer, chat_id=call_back.message.chat.id, message_id=bot_msg.message_id, reply_markup=update_keyboard, parse_mode="HTML")
def all_week_schedule_handler(call_back): user_id = call_back.message.chat.id bot_msg = bot.edit_message_text(text="{0}\U00002026".format( choice(loading_text["schedule"])), chat_id=call_back.message.chat.id, message_id=call_back.message.message_id) if call_back.data == "Текущее": json_week = func.get_json_week_data(user_id) else: json_week = func.get_json_week_data(user_id, next_week=True) inline_answer = json_week["WeekDisplayText"] bot.answer_callback_query(call_back.id, inline_answer, cache_time=1) is_send = False if len(json_week["Days"]): for day in json_week["Days"]: full_place = func.is_full_place(call_back.message.chat.id) answer = func.create_schedule_answer(day, full_place, call_back.message.chat.id) if "Выходной" in answer: continue if json_week["Days"].index(day) == 0 or not is_send: try: bot.edit_message_text(text=answer, chat_id=user_id, message_id=bot_msg.message_id, parse_mode="HTML") except ApiException: func.send_long_message(bot, answer, user_id) else: func.send_long_message(bot, answer, user_id) is_send = True if not is_send or not len(json_week["Days"]): answer = "{0} Выходная неделя".format(emoji["sleep"]) bot.edit_message_text(text=answer, chat_id=user_id, message_id=bot_msg.message_id)
def callback_inline(call): response = call.data chat_id = call.from_user.id response = response.split(' ') if 'delete' in response: try: # request to delete looks like 'delete <number>' # to get the number we split it py space and take second element number = response[2] if response[1] == 'published': delete_published_offer(chat_id, number) else: delete_taken_offer(chat_id, number) bot.answer_callback_query( callback_query_id=call.id, text=delet_offer_from_published.format(number)) except KeyError as e: bot.answer_callback_query(callback_query_id=call.id) print('error') if 'take' in response: try: # request to take looks like 'take <user_id> <number>' # to get the id of employer and number of offer we split it py space and take first and second element user_id = int(response[1]) number = response[2] if not user_exists(chat_id): new_user(chat_id, message.from_user.username) take_offer(chat_id, user_id, number) # message to person who will deliver bot.answer_callback_query(callback_query_id=call.id) bot.send_message(chat_id, performer_message.format(get_alias(user_id))) # message to person who will recieve bot.send_message( user_id, recipient_message.format(number, get_alias(chat_id))) except KeyError as e: bot.answer_callback_query(callback_query_id=call.id) print('error')
def return_lesson(call_back): data = func.get_hide_lessons_data(call_back.message.chat.id) ids_keyboard = InlineKeyboardMarkup(row_width=5) if len(data): answer = "Вот список скрытых тобой занятий:\n\n" for lesson in data: answer += "<b>id: {0}</b>\n".format(lesson[0]) if lesson[1] != "all": answer += "<b>Название</b>: {0}\n".format(lesson[1]) if lesson[2] != "all": answer += "<b>Типы</b>: {0}\n".format(lesson[2]) if lesson[3] != "all": answer += "<b>Дни</b>: {0}\n".format(lesson[3]) if lesson[4] != "all": answer += "<b>Время</b>: {0}\n".format(lesson[4]) if lesson[5] != "all": answer += "<b>Преподаватели</b>: {0}\n".format(lesson[5]) answer += "\n" ids_keyboard.row( *[InlineKeyboardButton( text=name, callback_data=name[:max_inline_button_text_len] ) for name in ["{0} - {1}".format(lesson[0], lesson[1])]] ) ids_keyboard.row( *[InlineKeyboardButton(text=name, callback_data=name) for name in ["Отмена", "Вернуть всё"]] ) answer += "Выбери то, которое хочешь вернуть:" else: answer = "Скрытых занятий нет" if len(answer) >= 3000: answers = answer.split("\n\n") for answer in answers[:-1]: bot.send_message(call_back.message.chat.id, answer, parse_mode="HTML") answer = answers[-1] lesson_ids = [lesson[0] for lesson in data] if len(lesson_ids) < 31: ids_keyboard = InlineKeyboardMarkup(row_width=5) ids_keyboard.add( *[InlineKeyboardButton(text=name, callback_data=name) for name in lesson_ids] ) bot.send_message(call_back.message.chat.id, answer, reply_markup=ids_keyboard, parse_mode="HTML") else: inline_answer = "Их слишком много..." bot.answer_callback_query(call_back.id, inline_answer, cache_time=1) for i in range(int(ceil(len(lesson_ids) / 30))): ids_keyboard = InlineKeyboardMarkup(row_width=5) ids_keyboard.add( *[InlineKeyboardButton(text=name, callback_data=name) for name in lesson_ids[:30]] ) bot.send_message(call_back.message.chat.id, answer, reply_markup=ids_keyboard, parse_mode="HTML") lesson_ids = lesson_ids[30:] ids_keyboard = InlineKeyboardMarkup(row_width=5) ids_keyboard.row(*[InlineKeyboardButton( text=name, callback_data=name) for name in ["Вернуть всё"]]) bot.send_message(call_back.message.chat.id, answer, reply_markup=ids_keyboard, parse_mode="HTML") else: bot.edit_message_text(text=answer, chat_id=call_back.message.chat.id, message_id=call_back.message.message_id, parse_mode="HTML", reply_markup=ids_keyboard)
def get_query_handler(call): if call.data == CALLBACK_CULTURE_BUT1: bot.answer_callback_query(callback_query_id=call.id, text=INLINE_CULTURE_BUT1_TEXT) bot.send_message(call.message.chat.id, NECESSARY_MESSAGE + NECESSARY_IMAGE, reply_markup=necessary_keyboard, parse_mode='html') elif call.data == CALLBACK_CULTURE_BUT2: bot.answer_callback_query(callback_query_id=call.id, text=INLINE_CULTURE_BUT2_TEXT) bot.send_message(call.message.chat.id, HOW_TO_FORM_MESSAGE + HOW_TO_FORM_IMAGE, reply_markup=how_to_form_keyboard, parse_mode='html') elif call.data == CALLBACK_CULTURE_BUT3: bot.answer_callback_query(callback_query_id=call.id, text=INLINE_CULTURE_BUT3_TEXT) bot.send_message(call.message.chat.id, ECO_HABITS_MESSAGE + ECO_HABITS_IMAGE, reply_markup=eco_habits_keyboard, parse_mode='html') elif call.data == CALLBACK_DICT_BUT1: bot.answer_callback_query(callback_query_id=call.id, text=INLINE_DICT_BUT1_TEXT) string = '' for term, ref in sorted(dictionary.items()): if (term[0] >= INLINE_DICT_BUT1_TEXT[0] and term[0] <= INLINE_DICT_BUT1_TEXT[4]): string += '<a href="' + ref[1] + '">' + term + '</a>\n' else: continue bot.send_message(call.message.chat.id, string, reply_markup=inline_dict_keyboard, parse_mode='html') elif call.data == CALLBACK_DICT_BUT2: bot.answer_callback_query(callback_query_id=call.id, text=INLINE_DICT_BUT2_TEXT) string = '' for term, ref in sorted(dictionary.items()): if (term[0] >= INLINE_DICT_BUT2_TEXT[0] and term[0] <= INLINE_DICT_BUT2_TEXT[4]): string += '<a href="' + ref[1] + '">' + term + '</a>\n' else: continue bot.send_message(call.message.chat.id, string, reply_markup=inline_dict_keyboard, parse_mode='html') elif call.data == CALLBACK_DICT_BUT3: bot.answer_callback_query(callback_query_id=call.id, text=INLINE_DICT_BUT3_TEXT) string = '' for term, ref in sorted(dictionary.items()): if (term[0] >= INLINE_DICT_BUT3_TEXT[0] and term[0] <= INLINE_DICT_BUT3_TEXT[4]): string += '<a href="' + ref[1] + '">' + term + '</a>\n' else: continue bot.send_message(call.message.chat.id, string, reply_markup=inline_dict_keyboard, parse_mode='html') elif call.data == CALLBACK_DICT_BUT4: bot.answer_callback_query(callback_query_id=call.id, text=INLINE_DICT_BUT4_TEXT) string = '' for term, ref in sorted(dictionary.items()): if (term[0] >= INLINE_DICT_BUT4_TEXT[0] and term[0] <= INLINE_DICT_BUT4_TEXT[4]): string += '<a href="' + ref[1] + '">' + term + '</a>\n' else: continue bot.send_message(call.message.chat.id, string, reply_markup=inline_dict_keyboard, parse_mode='html') elif call.data == CALLBACK_DICT_BUT5: bot.answer_callback_query(callback_query_id=call.id, text=INLINE_DICT_BUT5_TEXT) string = '' for term, ref in sorted(dictionary.items()): if (term[0] >= INLINE_DICT_BUT5_TEXT[0] and term[0] <= INLINE_DICT_BUT5_TEXT[4]): string += '<a href="' + ref[1] + '">' + term + '</a>\n' else: continue bot.send_message(call.message.chat.id, string, reply_markup=inline_dict_keyboard, parse_mode='html') elif call.data == CALLBACK_DICT_BUT6: bot.answer_callback_query(callback_query_id=call.id, text=INLINE_DICT_BUT6_TEXT) string = '' for term, ref in sorted(dictionary.items()): if (term[0] >= INLINE_DICT_BUT6_TEXT[0] and term[0] <= INLINE_DICT_BUT6_TEXT[4]): string += '<a href="' + ref[1] + '">' + term + '</a>\n' else: continue bot.send_message(call.message.chat.id, string, reply_markup=inline_dict_keyboard, parse_mode='html') bot.edit_message_reply_markup(call.message.chat.id, call.message.message_id)
def answer_callback(text, call_id, show_alert=False): bot.answer_callback_query(call_id, text, show_alert=show_alert)