def start(bot, update, *args, **kwargs): user = User(telegram_user=update.effective_user) category = post_helper.get_category_id_from_cache(user.user_id) redis.clean_all(user.user_id, 'start') if user.is_username_blank(): update.message.reply_text(conversations.MAIN_PANEL_USERNAME_MISSING) return stages.END access_pass = auth_helper.auth(user) if access_pass['is_admin']: pass elif access_pass['is_blacklist']: update.message.reply_text(conversations.MAIN_PANEL_YELLOWCOW) return stages.END ticket = post_helper.reset_cache(user.user_id, user.username) logger.info(ticket.to_dict()) update.message.reply_text(conversations.MAIN_PANEL_REMINDER) time.sleep(0.3) bot.send_message( text=conversations.POST_TICKET_START.format_map(ticket.to_human_readable()), chat_id=user.user_id, reply_markup=KEYBOARDS.post_ticket_keyboard_markup.get(category)) return stages.POST_SELECT_FIELD
def start(bot, update, *args, **kwargs): user = User(telegram_user=update.effective_user) redis.clean_all(user.user_id, 'start') if user.is_username_blank(): update.message.reply_text(conversations.MAIN_PANEL_USERNAME_MISSING) return stages.END access_pass = auth_helper.auth(user) if access_pass['is_admin']: pass elif access_pass['is_blacklist']: update.message.reply_text(conversations.MAIN_PANEL_YELLOWCOW) return stages.END update.message.reply_text(conversations.MAIN_PANEL_REMINDER) time.sleep(0.3) query = search_helper.reset_cache(user.user_id) bot.send_message(text=conversations.SEARCH_TICKET_START.format_map( query.to_human_readable()), chat_id=user.user_id, reply_markup=KEYBOARDS.search_ticket_keyboard_markup) return stages.SEARCH_SELECT_FIELD
def before_all(self): engine = create_engine('sqlite://') metadata = MetaData(bind=engine) self.db = UsersModel(engine, metadata) # Create Table self.db.metadata.drop_all() self.db.metadata.create_all() self.db.role = 'writer' self.admin_user = User(user_profile=dict(user_id=1, username='******', last_name='pytest', first_name='unittest', is_bot=False, language_code='ZH')) self.admin_user.admin_role = True self.db.raw_insert(self.admin_user.to_dict()) self.blacklist_user = User(user_profile=dict(user_id=2, username='******', last_name='pytest', first_name='unittest', is_bot=False, language_code='ZH')) self.blacklist_user.blacklist = True self.db.raw_insert(self.blacklist_user.to_dict())
def start(bot, update, *args, **kwargs): user = User(telegram_user=update.effective_user) redis.clean_all(user.user_id, 'start') if user.is_username_blank(): update.message.reply_text(conversations.MAIN_PANEL_USERNAME_MISSING) return stages.END access_pass = auth_helper.auth(user) if access_pass['is_admin']: pass elif access_pass['is_blacklist']: update.message.reply_text(conversations.MAIN_PANEL_YELLOWCOW) return stages.END update.message.reply_text(conversations.MAIN_PANEL_REMINDER) time.sleep(0.3) tickets = query_helper.search_by_user_id(user.user_id) if tickets: bot.send_message( text=update_helper.tickets_tostr( [ticket.to_human_readable() for ticket in tickets], conversations.TICKET), chat_id=user.user_id, reply_markup=update_helper.list_tickets_on_reply_keyboard(tickets)) return stages.UPDATE_SELECT_TICKET bot.send_message(chat_id=user.user_id, text=conversations.NONE_RECORD) return stages.SEARCH_SELECT_FIELD
def test_convert_telegram_user_to_user(self): tg_user = TelegramUser(id=123456789, first_name='test', last_name='pytest', username='******', language_code='ZH', is_bot=False) user = User(telegram_user=tg_user) assert user.user_id == tg_user.id assert user.username == tg_user.username assert user.is_username_blank() is False
class Test(unittest.TestCase): @pytest.fixture(autouse=True, scope='function') def before_all(self): engine = create_engine('sqlite://') metadata = MetaData(bind=engine) self.db = UsersModel(engine, metadata) # Create Table self.db.metadata.drop_all() self.db.metadata.create_all() self.db.role = 'writer' self.admin_user = User(user_profile=dict(user_id=1, username='******', last_name='pytest', first_name='unittest', is_bot=False, language_code='ZH')) self.admin_user.admin_role = True self.db.raw_insert(self.admin_user.to_dict()) self.blacklist_user = User(user_profile=dict(user_id=2, username='******', last_name='pytest', first_name='unittest', is_bot=False, language_code='ZH')) self.blacklist_user.blacklist = True self.db.raw_insert(self.blacklist_user.to_dict()) def test_new_user(self): new_user = User(user_profile=dict(user_id=123456789, username='******', last_name='pytest', first_name='unittest', is_bot=False, language_code='ZH')) profile = self.db.auth(new_user) assert profile['is_admin'] is False assert profile['is_blacklist'] is False def test_admin_user(self): profile = self.db.auth(self.admin_user) assert profile['is_admin'] assert profile['is_blacklist'] is False def test_blacklist_user(self): profile = self.db.auth(self.blacklist_user) assert profile['is_admin'] is False assert profile['is_blacklist']
def select_field(bot, update, *arg, **kwargs): callback_data = update.callback_query.data message = update.callback_query.message user = User(telegram_user=update.effective_user) if not update_helper.save_last_choice(user_id=user.user_id, field=callback_data): ticket = update_helper.load_drafting_ticket(ticket_id=callback_data) bot.send_message(text=conversations.UPDATE_YOURS.format_map( ticket.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.update_ticket_keyboard_markup) return stages.UPDATE_SELECT_FIELD if callback_data == 'check': ticket_in_cache = update_helper.load_drafting_ticket( user_id=user.user_id) bot.edit_message_text( text=conversations.UPDATE_CHECK.format_map( ticket_in_cache.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.before_submit_post_keyboard_markup) return stages.UPDATE_BEFORE_SUBMIT else: bot.edit_message_text( text=conversations.UPDATE_INFO.format_map( dict(message=TICKET_MAPPING.get(callback_data))), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.conditions_keyboard_mapping.get( callback_data)) return stages.UPDATE_FILL_VALUE
def fill_in_field(bot, update, *args, **kwargs): callback_data = update.callback_query.data message = update.callback_query.message user = User(telegram_user=update.effective_user) category = post_helper.get_category_id_from_cache(user_id=user.user_id) if re.match(r'\d+||^([A-Z0-9]){2}$', callback_data): ticket = post_helper.update_cache(user.user_id, callback_data) logger.info(ticket.to_dict()) try: bot.edit_message_text( text=conversations.POST_TICKET_START.format_map(ticket.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.post_ticket_keyboard_markup.get(category)) except BadRequest: bot.send_message( text=conversations.POST_TICKET_START.format_map(ticket.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.post_ticket_keyboard_markup.get(category)) return stages.POST_SELECT_FIELD ticket = post_helper.load_drafting_ticket(user.user_id) choice = post_helper.load_last_choice(user.user_id) keyboard = KEYBOARDS.conditions_keyboard_mapping.get(choice) text = '\n\n'.join([conversations.TYPE_IN_ERROR, conversations.POST_TICKET_INFO.format(message=TICKET_MAPPING.get(choice))]) try: bot.edit_message_text(text=text, chat_id=user.user_id, message_id=message.message_id, reply_markup=keyboard) except BadRequest: bot.send_message(text=text, chat_id=user.user_id, message_id=message.message_id, reply_markup=keyboard)
def fill_in_field(bot, update, *args, **kwargs): callback_data = update.callback_query.data message = update.callback_query.message user = User(telegram_user=update.effective_user) if re.match(r'\d+', callback_data): query = search_helper.update_cache(user.user_id, callback_data) bot.edit_message_text( chat_id=user.user_id, message_id=message.message_id, text=conversations.SEARCH_TICKET_START.format_map( query.to_human_readable()), reply_markup=KEYBOARDS.search_ticket_keyboard_markup, parse_mode=telegram.ParseMode.MARKDOWN) return stages.SEARCH_SELECT_FIELD query = search_helper.load_drafting_query(user.user_id) choice = search_helper.load_last_choice(user.user_id) bot.sendMessage(chat_id=user.user_id, text=conversations.TYPE_IN_ERROR) bot.sendMessage( chat_id=user.user_id, message_id=message.message_id, text=conversations.SEARCH_TICKET_START.format_map( query.to_human_readable()), reply_markup=KEYBOARDS.conditions_keyboard_mapping.get(choice), parse_mode=telegram.ParseMode.MARKDOWN)
def submit(bot, update, *args, **kwargs): callback_data = update.callback_query.data user = User(telegram_user=update.effective_user) message = update.callback_query.message # Kick banned user out! if auth_helper.auth(user)['is_blacklist']: try: bot.edit_message_text(text=conversations.MAIN_PANEL_YELLOWCOW, chat_id=user.user_id, message_id=message.message_id) except BadRequest: bot.send_message(text=conversations.MAIN_PANEL_YELLOWCOW, chat_id=user.user_id, message_id=message.message_id) return stages.END if callback_data == 'submit': ticket = update_helper.load_drafting_ticket(user_id=user.user_id) ticket_helper.update_ticket(ticket) try: bot.edit_message_text(text=conversations.UPDATE_INTO_DB, chat_id=user.user_id, message_id=message.message_id) except BadRequest: bot.send_message(text=conversations.UPDATE_INTO_DB, chat_id=user.user_id, message_id=message.message_id) return stages.END
def get_user_profile(self, user_id: int) -> User: stmt = select(['*']).where(self.table.c.user_id == user_id) row = self.execute(stmt).fetchone() if row: user_profile = dict( zip([col.key for col in self.table.columns], row)) return User(user_profile=user_profile) return None
def fill_type_in_field(bot, update, *args, **kwargs): user = User(telegram_user=update.effective_user) ticket = update_helper.update_cache(user_id=user.user_id, value=update.message.text) update.message.reply_text( text=conversations.UPDATE_YOURS.format_map(ticket.to_human_readable()), reply_markup=KEYBOARDS.update_ticket_keyboard_markup) return stages.UPDATE_SELECT_FIELD
def fill_type_in_field(bot, update, *args, **kwargs): user = User(telegram_user=update.effective_user) text = update.message.text ticket = post_helper.update_cache(user.user_id, text) category = post_helper.get_category_id_from_cache(user_id=user.user_id) update.message.reply_text( text=conversations.POST_TICKET_START.format_map(ticket.to_human_readable()), reply_markup=KEYBOARDS.post_ticket_keyboard_markup.get(category)) return stages.POST_SELECT_FIELD
def auth(self, user: User) -> dict: # check isExisted and blacklist auth_result = self._auth(user) if auth_result: self.raw_update(self.table.c.user_id == user.user_id, dict(updated_at=int(time.time()))) return auth_result self.raw_upsert(user.to_dict()) return self._auth(user)
def send_chart(bot, update, *args, **kwargs): user = User(telegram_user=update.effective_user) bot.send_chat_action(chat_id=user.user_id, action=chataction.ChatAction.TYPING) bot.send_message( chat_id=user.user_id, text=conversations.STATS.format( updated_at=datetime.fromtimestamp(int(time.time())).astimezone( TIMEZONE).strftime('%Y-%m-%d %H:%M:%S'))) return stages.END
def test_new_user(self): new_user = User(user_profile=dict(user_id=123456789, username='******', last_name='pytest', first_name='unittest', is_bot=False, language_code='ZH')) profile = self.db.auth(new_user) assert profile['is_admin'] is False assert profile['is_blacklist'] is False
def list_events(bot, update, *args, **kwargs): user = User(telegram_user=update.effective_user) events = event_helper.list_all_events() if events: bot.send_message(text=conversations.SUPPORT_LIST_EVENTS, chat_id=user.user_id, reply_markup=event_helper.generate_keyboard(events)) else: bot.send_message(chat_id=user.user_id, text=conversations.SUPPORT_NONE_EVENTS) return stages.END
def info(bot, update, *args, **kwargs): user = User(telegram_user=update.effective_user) bot.send_photo(chat_id=user.user_id, photo=conversations.OFFICIAL_POSTER, caption=conversations.INFO, parse_mode=ParseMode.MARKDOWN) time.sleep(0.5) bot.send_photo(chat_id=user.user_id, photo=conversations.SEATING_PLAN, parse_mode=ParseMode.MARKDOWN) return stages.END
def test_user_object_init(self): profile = User(user_profile=TELEGRAM_INFO).to_dict() assert profile['user_id'] == TELEGRAM_INFO['user_id'] assert profile['username'] == TELEGRAM_INFO['username'] assert profile['last_name'] == TELEGRAM_INFO['last_name'] assert profile['first_name'] == TELEGRAM_INFO['first_name'] assert profile['is_bot'] == TELEGRAM_INFO['is_bot'] assert profile['language_code'] == TELEGRAM_INFO['language_code'] assert profile['is_admin'] is False assert profile['is_blacklist'] is False
def test_new_user_auth(self): telegram_user_profile = dict( user_id=123456789, username='******', last_name='pytest', first_name='test', is_bot=False, language_code='ZH') profile_in_db = self.user_table.auth(User(user_profile=telegram_user_profile)) assert profile_in_db['is_admin'] is False assert profile_in_db['is_blacklist'] is False
def select_ticket(bot, update, *args, **kwargs): user = User(telegram_user=update.effective_user) message = update.callback_query.message callback_data = update.callback_query.data ticket = query_helper.search_by_ticket_id(ticket_id=callback_data) update_helper.save_drafting_ticket(user.user_id, ticket) bot.edit_message_text(text=conversations.UPDATE_YOURS.format_map( ticket.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.update_ticket_keyboard_markup) return stages.UPDATE_SELECT_FIELD
def test_username_blank(self): profile = User(user_profile=TELEGRAM_INFO) assert profile.is_username_blank() TELEGRAM_INFO.update(dict(username='******')) profile = User(user_profile=TELEGRAM_INFO) assert profile.is_username_blank() is False
def test_set_user_role(self): admin_user = User(user_profile=TELEGRAM_INFO) admin_user.admin_role = True assert admin_user.admin_role admin_user = User(user_profile=TELEGRAM_INFO) admin_user.blacklist = True assert admin_user.blacklist
def start(bot, update, *args, **kwargs): user = User(telegram_user=update.effective_user) redis.clean_all(user.user_id, 'start') if user.is_username_blank(): update.message.reply_text(conversations.MAIN_PANEL_USERNAME_MISSING) return stages.END access_pass = auth_helper.auth(user) if access_pass['is_admin']: pass elif access_pass['is_blacklist']: update.message.reply_text(conversations.MAIN_PANEL_YELLOWCOW) return stages.END update.message.reply_text(conversations.MAIN_PANEL_REMINDER) time.sleep(0.3) bot.send_message(text=conversations.QUICK_SEARCH_START, chat_id=user.user_id, reply_markup=KEYBOARDS.quick_search_start_keyboard_markup, parse_mode=telegram.ParseMode.MARKDOWN) return stages.QUICK_SEARCH_MODE_SELECTION
def fill_in_field(bot, update, *args, **kwargs): callback_data = update.callback_query.data message = update.callback_query.message if message: user = User(telegram_user=update.effective_user) ticket = update_helper.update_cache(user_id=user.user_id, value=callback_data) bot.edit_message_text( text=conversations.UPDATE_YOURS.format_map( ticket.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.update_ticket_keyboard_markup) return stages.UPDATE_SELECT_FIELD # FIXME: Why return stage.UPDATE_FILL_VALUE return stages.UPDATE_FILL_VALUE
def select_field(bot, update, *args, **kwargs): callback_data = update.callback_query.data logger.debug(callback_data) message = update.callback_query.message user = User(telegram_user=update.effective_user) if not post_helper.save_last_choice(user_id=user.user_id, field=callback_data): try: ticket = post_helper.load_drafting_ticket(user.user_id) except Exception: logger.warning("cache miss") ticket = post_helper.reset_cache(user.user_id, user.username) category = post_helper.get_category_id_from_cache(user.user_id) bot.send_message( text=conversations.POST_TICKET_START.format_map(ticket.to_human_readable()), chat_id=user.user_id, reply_markup=KEYBOARDS.post_ticket_keyboard_markup.get(category)) return stages.POST_SELECT_FIELD category = post_helper.get_category_id_from_cache(user_id=user.user_id) logger.debug(category) if callback_data == 'reset': ticket = post_helper.reset_cache(user_id=user.user_id, username=user.username) try: bot.edit_message_text( text=conversations.POST_TICKET_START.format_map(ticket.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.post_ticket_keyboard_markup.get(category), parse_mode=telegram.ParseMode.MARKDOWN) except BadRequest: bot.send_message( text=conversations.POST_TICKET_START.format_map(ticket.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.post_ticket_keyboard_markup.get(category), parse_mode=telegram.ParseMode.MARKDOWN) return stages.POST_SELECT_FIELD if callback_data == 'check': ticket = post_helper.load_drafting_ticket(user.user_id) check_result = ticket.validate() check_wishlist = ticket.validate_wishlist() category = post_helper.get_category_id_from_cache(user.user_id) if check_result['status']: if check_wishlist['status'] is False: ticket = ticket.fill_full_wishlist() post_helper.save_drafting_ticket(user.user_id, ticket) try: bot.edit_message_text( text=conversations.POST_TICKET_CHECK.format_map(ticket.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.before_submit_post_keyboard_markup) except BadRequest: bot.send_message( text=conversations.POST_TICKET_CHECK.format_map(ticket.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.before_submit_post_keyboard_markup) return stages.POST_BEFORE_SUBMIT bot.send_message( text=conversations.TYPE_IN_WARNING.format(error_message=check_result['info']), chat_id=user.user_id, message_id=message.message_id) bot.send_message( text=conversations.POST_TICKET_START.format_map(ticket.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.post_ticket_keyboard_markup.get(category)) return stages.POST_SELECT_FIELD if callback_data == 'section': ticket = post_helper.load_drafting_ticket(user.user_id) category = post_helper.get_category_id_from_cache(user_id=user.user_id) if ticket.price_id: try: bot.edit_message_text( text=conversations.POST_TICKET_INFO.format(message=TICKET_MAPPING.get(callback_data)), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.conditions_keyboard_mapping.get(callback_data)[ticket.price_id]) except BadRequest: bot.send_message( text=conversations.POST_TICKET_INFO.format(message=TICKET_MAPPING.get(callback_data)), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.conditions_keyboard_mapping.get(callback_data)[ticket.price_id]) return stages.POST_FILL_VALUE try: bot.edit_message_text( text=conversations.POST_TICKET_SECTION, chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.post_ticket_keyboard_markup.get(category)) except BadRequest: bot.send_message( text=conversations.POST_TICKET_SECTION, chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.post_ticket_keyboard_markup.get(category)) return stages.POST_SELECT_FIELD try: bot.edit_message_text( text=conversations.POST_TICKET_INFO.format(message=TICKET_MAPPING.get(callback_data)), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.conditions_keyboard_mapping.get(callback_data)) except BadRequest: bot.send_message( text=conversations.POST_TICKET_INFO.format(message=TICKET_MAPPING.get(callback_data)), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.conditions_keyboard_mapping.get(callback_data)) return stages.POST_FILL_VALUE
def submit(bot, update, *args, **kwargs): callback_data = update.callback_query.data message = update.callback_query.message user = User(telegram_user=update.effective_user) category = post_helper.get_category_id_from_cache(user_id=user.user_id) if callback_data == 'reset': ticket = post_helper.reset_cache(user_id=user.user_id, username=user.username) try: bot.edit_message_text( text=conversations.POST_TICKET_START.format_map(ticket.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.post_ticket_keyboard_markup.get(category)) except BadRequest: bot.send_message( text=conversations.POST_TICKET_START.format_map(ticket.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.post_ticket_keyboard_markup.get(category)) return stages.POST_SELECT_FIELD if callback_data == 'submit': # Kick banned user out! if auth_helper.auth(user)['is_blacklist']: update.message.reply_text(conversations.MAIN_PANEL_YELLOWCOW) return stages.END ticket = post_helper.load_drafting_ticket(user.user_id) # Chekc username again. Force overwrite # FIXME: Debug why username is lost. if ticket.username == '': ticket.username = user.username if ticket_helper.save_ticket(ticket): try: bot.edit_message_text( text=conversations.POST_TICKET_INTO_DB, chat_id=user.user_id, message_id=message.message_id) except BadRequest: bot.send_message( text=conversations.POST_TICKET_INTO_DB, chat_id=user.user_id, message_id=message.message_id) else: try: bot.edit_message_text( text=conversations.POST_TICKET_ERROR, chat_id=user.user_id, message_id=message.message_id) except BadRequest: bot.send_message( text=conversations.POST_TICKET_ERROR, chat_id=user.user_id, message_id=message.message_id) # Send Ticket to Channel bot.send_message( text=conversations.NEW_TICKET.format_map(ticket.to_human_readable()), chat_id=TELEGRAM_API_CONFIG['subscribe_channel_name'], parse_mode=telegram.ParseMode.MARKDOWN) return stages.END
import unittest import pytest import sqlalchemy from sqlalchemy import MetaData from mayday.db.tables.users import UsersModel from mayday.objects.user import User USER = User(user_profile=dict(user_id=123456789, username='******', first_name='unittest', last_name='ttset')) @pytest.mark.usefixtures() class TestCase(unittest.TestCase): @pytest.fixture(autouse=True, scope='function') def before_all(self): engine = sqlalchemy.create_engine('sqlite://') metadata = MetaData(bind=engine) self.db = UsersModel(engine, metadata) # Create Table self.db.metadata.drop_all() self.db.metadata.create_all() self.db.role = 'writer' def test_get_auth(self): result = self.db.auth(USER) assert result
def select_field(bot, update, *args, **kwargs): callback_data = update.callback_query.data message = update.callback_query.message user = User(telegram_user=update.effective_user) if not search_helper.save_last_choice(user.user_id, field=callback_data): try: query = search_helper.load_drafting_query(user.user_id) except Exception: logger.warning("cache miss") query = search_helper.reset_cache(user.user_id) bot.send_message(text=conversations.SEARCH_TICKET_START.format_map( query.to_human_readable()), chat_id=user.user_id, reply_markup=KEYBOARDS.search_ticket_keyboard_markup) return stages.SEARCH_SELECT_FIELD if callback_data == 'check': query = search_helper.load_drafting_query(user.user_id) check_result = query.validate() if check_result['status']: bot.edit_message_text( text=conversations.SEARCH_CHECK.format_map( query.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.before_submit_search_keyboard_markup, parse_mode=telegram.ParseMode.MARKDOWN) return stages.SEARCH_BEFORE_SUBMIT bot.send_message(text=conversations.TYPE_IN_WARNING.format( error_message=check_result['info']), chat_id=user.user_id, message_id=message.message_id) bot.send_message(text=conversations.SEARCH_TICKET_START.format_map( query.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.search_ticket_keyboard_markup, parse_mode=telegram.ParseMode.MARKDOWN) return stages.SEARCH_SELECT_FIELD if callback_data == 'reset': query = search_helper.reset_cache(user.user_id) try: bot.edit_message_text( text=conversations.SEARCH_TICKET_START.format_map( query.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.search_ticket_keyboard_markup, parse_mode=telegram.ParseMode.MARKDOWN) except BadRequest: bot.send_message( text=conversations.SEARCH_TICKET_START.format_map( query.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.search_ticket_keyboard_markup, parse_mode=telegram.ParseMode.MARKDOWN) return stages.SEARCH_SELECT_FIELD bot.edit_message_text( text=conversations.SEARCH_TICKET_INFO.format( message=TICKET_MAPPING.get(callback_data)), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.conditions_keyboard_mapping.get(callback_data)) return stages.SEARCH_FILL_VALUE
def submit(bot, update, *args, **kwargs): callback_data = update.callback_query.data message = update.callback_query.message user = User(telegram_user=update.effective_user) if callback_data == 'reset': query = search_helper.reset_cache(user.user_id) bot.edit_message_text( text=conversations.SEARCH_TICKET_START.format_map( query.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.search_ticket_keyboard_markup, parse_mode=telegram.ParseMode.MARKDOWN) return stages.SEARCH_SELECT_FIELD if callback_data == 'back': query = search_helper.load_drafting_query(user.user_id) bot.edit_message_reply_markup( text=conversations.SEARCH_TICKET_START.format_map( query.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.search_ticket_keyboard_markup, parse_mode=telegram.ParseMode.MARKDOWN) return stages.SEARCH_SELECT_FIELD if callback_data == 'quick_search': query = search_helper.load_drafting_query(user.user_id) query_helper.save_quick_search(query) bot.edit_message_text( text=conversations.QUICK_SEARCH_INSERT_SUCESS.format_map( query.to_human_readable()), chat_id=user.user_id, message_id=message.message_id, reply_markup=KEYBOARDS.before_submit_post_keyboard_markup) return stages.SEARCH_BEFORE_SUBMIT if callback_data == 'submit': # Kick banned user out! if auth_helper.auth(user)['is_blacklist']: update.message.reply_text(conversations.MAIN_PANEL_YELLOWCOW) return stages.END query = search_helper.load_drafting_query(user.user_id) bot.send_chat_action(chat_id=user.user_id, action=chataction.ChatAction.TYPING) tickets = query_helper.search_by_query(query) if (tickets and len(tickets) <= 25): bot.edit_message_text(text=conversations.SEARCH_WITH_RESULTS, chat_id=user.user_id, message_id=message.message_id) time.sleep(0.2) for trait in query_helper.split_tickets_traits(tickets): bot.send_message(text=search_helper.tickets_tostr( trait, conversations.TICKET), chat_id=user.user_id, message_id=message.message_id) time.sleep(0.2) return stages.END elif len(tickets) > 25: bot.edit_message_text(text=conversations.SEARCH_TOO_MUCH_TICKETS, chat_id=user.user_id, message_id=message.message_id) return stages.END else: bot.edit_message_text(text=conversations.SEARCH_WITHOUT_TICKETS, chat_id=user.user_id, message_id=message.message_id) return stages.END