def _show_article(bot: TeleBot, message: Message, botan_key: str) -> None: """Entry point for article stores.""" sent = bot.send_message(message.chat.id, "Введите артикул интересующего товара:") bot.register_next_step_handler(sent, _find_articles) for id in chats_with_searching_articles: if id == message.chat.id: return chats_with_searching_articles.append(message.chat.id) if not botan_track(botan_key, message.chat.id, message, 'Обработка команды'): logging.error('botan problem')
class Bot: """control bot""" SUCCESS_USERS_ID = [] def __init__(self): self.__bot = TeleBot(API_token) self.__parser = Parser() @self.__bot.message_handler(commands=["help"]) def __show_help(message): """show info about bot through /help command""" if message.from_user.id in self.SUCCESS_USERS_ID: self.__bot.send_message( message.chat.id, "Bot for getting info about science videos on Youtube. For start work write /parse.", ) else: self.__bot.send_message( message.chat.id, "Bot for getting info about science videos on Youtube. For more info, auth through /start.", ) @self.__bot.message_handler(commands=["start"]) def __start_work(message): """start interaction with user and check his status""" if message.from_user.id in self.SUCCESS_USERS_ID: self.__bot.send_message(message.chat.id, "Access is allowed!") else: message_from_user = self.__bot.reply_to( message, """Entry password: """) self.__bot.register_next_step_handler(message_from_user, __auth_to_bot) def __auth_to_bot(message): """checks the password for correctness""" if message.text == password: self.__bot.send_message(message.chat.id, "Access is allowed! Choose command.") self.SUCCESS_USERS_ID.append(message.from_user.id) else: self.__bot.send_message(message.chat.id, "Access denied! Try again /start.") @self.__bot.message_handler(commands=["parse"]) def __parsing(message): """start parsing and show result""" if message.from_user.id in self.SUCCESS_USERS_ID: self.__bot.send_message(message.chat.id, "Parsing start. Please, await!") info_about_videos = self.__get_info_about_videos() for info_about_video in info_about_videos: self.__bot.send_message( message.chat.id, f"Title: {info_about_video['title']}") self.__bot.send_message( message.chat.id, f"Views: {info_about_video['views']}") self.__bot.send_message( message.chat.id, f"Date: {info_about_video['date']}") self.__bot.send_message( message.chat.id, f"Like_bar: {info_about_video['like_bar']}") self.__bot.send_message( message.chat.id, f"Channel: {info_about_video['channel']}") self.__bot.send_message( message.chat.id, f"Link: {info_about_video['link']}") self.__bot.send_message(message.chat.id, "-" * 10) sleep(3) else: self.__bot.send_message(message.chat.id, "Access denied! Try again /start.") def __get_info_about_videos(self): """return parsing result""" return self.__parser.start_parse() def start_bot(self): """start bot working""" self.__bot.polling()
class BotTelegram(object): def __init__(self, token, proxy): apihelper.proxy = { 'https': 'socks5://' + proxy['LOGIN'] + ':' + proxy['PSW'] + '@' + proxy['IP'] + ':' + proxy['PORT'] } self.bot = TeleBot(token) def start_and_work(self): # Actions after /start command @self.bot.message_handler(commands=['start']) def send_welcom(message): self.markup = types.ReplyKeyboardMarkup(resize_keyboard=True) self.markup.row(botText.NEW_TEAM, botText.SIGNIN_TEAM) answer = self.bot.send_message(message.from_user.id, botText.START, reply_markup=self.markup) self.bot.register_next_step_handler(answer, process_choose_sigin_step) database.register_user(message.chat) # Actions after choice: new team OR choose team def process_choose_sigin_step(message): markup = types.ReplyKeyboardRemove(selective=False) if message.text == botText.NEW_TEAM: answer = self.bot.send_message(message.from_user.id, botText.NAME_TEAM, reply_markup=markup) self.bot.register_next_step_handler(answer, process_name_command_step) elif message.text == botText.SIGNIN_TEAM: self.bot.send_message(message.from_user.id, botText.CHOOSE_TEAM, reply_markup=markup) team_list = database.take_teams() title_team_buttons = types.InlineKeyboardMarkup() for title in team_list: butt = types.InlineKeyboardButton(text=title, callback_data=title) title_team_buttons.add(butt) answer = self.bot.send_message(message.chat.id, "Список команд:", reply_markup=title_team_buttons) self.bot.register_next_step_handler(answer, callback_inline) else: self.bot.reply_to(message, botText.ERROR) @self.bot.callback_query_handler(func=lambda call: True) def callback_inline(call): try: print(call.message.chat.id, call.data) database.choose_team(call.message.chat.id, call.data) answer = self.bot.send_message(chat_id=call.message.chat.id, text="Введи пароль:") self.bot.register_next_step_handler(answer, process_login_step) except: self.bot.send_message(call.chat.id, botText.ERROR_PSW) # Enter team name def process_name_command_step(message): database.register_team(message.from_user.id, message.text) answer = self.bot.send_message(message.from_user.id, 'Придумай простой пароль:') self.bot.register_next_step_handler(answer, process_password_command_step) # Enter team password def process_password_command_step(message): database.inputPSW_team(message.from_user.id, message.text) self.bot.send_message(message.from_user.id, botText.REG_TEAM) # Team authorization def process_login_step(message): if database.login_team(message.chat.id, message.text): self.bot.send_message(message.from_user.id, botText.LOGIN_TEAM) else: database.clear_team_for_user(message.chat.id) self.bot.send_message(message.from_user.id, botText.ERROR_PSW) # Actions after /coords command @self.bot.message_handler(commands=['coords']) def send_message(message): answer = self.bot.send_message(message.from_user.id, botText.POINT) self.bot.register_next_step_handler(answer, process_adding_coords) # Enter coordinates. If form doesn't correct, enter again def process_adding_coords(message): result = database.check_register(message.chat.id) if result == None: self.bot.send_message(message.from_user.id, botText.ERROR_REG) print('reg_error from:'+' '+message.chat.id) else: result = database.coordinates_verify(message.chat.id) key_tuple = (float(message.text.split(', ')[0]), float(message.text.split(', ')[1])) if key_tuple in result: self.bot.send_message(message.from_user.id, botText.WRONG_POINT) else: if re.match(r'\d*.\d*, \d*.\d*', message.text): database.input_point(message.from_user.id, message.text.split(', ')) answer = self.bot.send_message(message.from_user.id, botText.HEIGHT) self.bot.register_next_step_handler(answer, process_adding_height) elif message.entities: # Приостанавливаем ввод координат, если введена любая команда self.bot.send_message(message.from_user.id, botText.WHATSUP) else: answer = self.bot.send_message(message.from_user.id, botText.TRY_AGAIN) self.bot.register_next_step_handler(answer, process_adding_coords) # Enter point height def process_adding_height(message): database.input_height(message.from_user.id, message.text) self.bot.send_message(message.from_user.id, botText.SAVE_POINT) @self.bot.message_handler(commands=['showpoints']) def show_points(message): coords_list = database.show_points(message.chat.id) coords_buttons = types.InlineKeyboardMarkup() for array in coords_list: string = str(array[0]) + ', ' + str(array[1]) + ', ' + str(array[2]) butt = types.InlineKeyboardButton(text=string, callback_data=str(array[3])) coords_buttons.add(butt) self.bot.send_message(message.chat.id, "Список точек:", reply_markup=coords_buttons) self.bot.polling(none_stop=True)