def _parse_float_arg(self, msg: Message, args: List[str]) -> Optional[float]: if len(args) != 1: msg.reply_markdown('Invalid number of arguments!') return None val = parse_float(args[0]) if not val: msg.reply_markdown('Unable to parse float value!') return None return val
def test_effective_message_type(self): test_message = Message(message_id=1, from_user=None, date=None, chat=None) test_message.text = 'Test' assert helpers.effective_message_type(test_message) == 'text' test_message.text = None test_message.sticker = Sticker('sticker_id', 50, 50) assert helpers.effective_message_type(test_message) == 'sticker' test_message.sticker = None test_message.new_chat_members = [User(55, 'new_user', False)] assert helpers.effective_message_type( test_message) == 'new_chat_members' test_update = Update(1) test_message.text = 'Test' test_update.message = test_message assert helpers.effective_message_type(test_update) == 'text' empty_update = Update(2) assert helpers.effective_message_type(empty_update) is None
def send_msg_by_chunks(msg: Message, text: str, delim: str, parse_mode=None, keyboard=None): chunks = [''] for t in text.split(delim): if len(chunks[-1] + t) + 3 < 4096: chunks[-1] += '\n' + t else: chunks[-1] += '```' chunks.append('```' + t) for reply in chunks: msg.reply_text(reply, parse_mode=parse_mode, reply_markup=keyboard)
def process_message(self, message: Message): # Add snippets message.u_id = message.from_user.id message.moved = False if message.text.startswith( '/announce') and message.chat.id == -163122359: announcement = ' '.join(message.text.split()[1:]) Thread(target=self.announce, args=(announcement, )).start() return self.user_set(message.u_id, 'last_message', message.to_json()) current_handler = self.user_get(message.u_id, 'next_handler') or self.default_handler if current_handler == 'main-menu' and message.location: set_location(self, message.u_id, message.location) self.telegram.send_message(message.u_id, 'You location has been changed 🙂') return # Go back to menu haves the largest priority if message.text == self.MENU_BUTTON: tracker = self.call_handler(message, self.default_handler) Thread(target=track_message, args=(self, message, current_handler, tracker)).start() return self.user_set(message.u_id, 'handler', current_handler) self.user_set(message.u_id, 'next_handler', '') try: tracker = self.handlers[current_handler](message, self) except Exception as error: if self.debug: raise error self.logger.error(error) self.user_set(message.u_id, 'handler', self.default_handler) return self.user_set(message.u_id, 'last_interaction', time.time()) Thread(target=track_message, args=(self, message, current_handler, tracker)).start()
def build_test_message(**kwargs): config = dict( message_id=1, from_user=None, date=None, chat=None, ) config.update(**kwargs) return Message(**config)
def __init__(self, data, db=None): self.__update_id = data['update_id'] if data.__contains__('message'): self.__message = Message(data['message'], db=db) else: self.__message = None if data.__contains__('inline_query'): self.__inline_query = None else: self.__inline_query = None if data.__contains__('chosen_inline_result'): self.__chosen_inline_result = None else: self.__chosen_inline_result = None
def interact(self,msg: Message=None,text=None,action=None,filename=None): self.lock.acquire() if msg is not None: if (text is not None) and (text != msg.text): # print('Text:',text,'Msg.text:',msg.text) msg = msg.edit_text(text) elif text is not None: msg = self.context.bot.send_message(chat_id=self.update.effective_chat.id, text=text) if action is not None: self.context.bot.send_chat_action(chat_id=self.update.effective_chat.id, action=action) self.lock.release() if (self.upload_to_chat is True) and (filename is not None): self.context.bot.send_document(chat_id=self.update.effective_chat.id, timeout=600, document=open(filename, 'rb')) # self.context.bot.send_media_group() # self.lock.release() return msg
def test_effective_message_type(self): test_message = Message(message_id=1, from_user=None, date=None, chat=None) test_message.text = 'Test' assert helpers.effective_message_type(test_message) == 'text' test_message.text = None test_message.sticker = Sticker('sticker_id', 50, 50) assert helpers.effective_message_type(test_message) == 'sticker' test_message.sticker = None test_message.new_chat_members = [User(55, 'new_user', False)] assert helpers.effective_message_type(test_message) == 'new_chat_members' test_update = Update(1) test_message.text = 'Test' test_update.message = test_message assert helpers.effective_message_type(test_update) == 'text' empty_update = Update(2) assert helpers.effective_message_type(empty_update) is None
def on_verdict_sent(msg: Message, reply: str, result: int): msg.reply_text('Something went wrong... ' 'Please contact developers' if result == 500 else reply)