def send(self, message: Message): message.to_chat(chat_id=self.chat_id) send_func = self._send if self._should_send_new_message( ) else self._edit try: send_func(message) except: # If the message could not be sent properly, discard it to start with a new one self.new() raise
def get_response_last(self, event, poles, number_of_pole_to_display): pole = poles.get(-number_of_pole_to_display) if pole is None: return Message.create( self.__formatted( _("Invalid {pole} number. Range [1,total_{poles}]"))) text = _("This is the {0} last {pole}").format( number_of_pole_to_display, **self.pole_format_dict) return Message.create(text, chat_id=event.message.chat.id, reply_to_message_id=pole.message_id)\ .with_error_callback(lambda e: self.__deleted_pole_handler(0, pole, event, number_of_pole_to_display))
def process(self, event): message = event.message chat = message.chat new_chat_member = message.new_chat_member if new_chat_member is not None: if new_chat_member.id == self.cache.bot_info.id: reply = Message.create_reply( message, "Hi! I'm " + self.cache.bot_info.first_name + " and have just entered " + chat.title) else: reply = Message.create_reply( message, "Hello " + new_chat_member.first_name + ". Welcome to " + chat.title) self.api.send_message(reply)
def send_current_status(self, handler, prepend=""): status = handler.get_status_string() response = prepend + "Current status of *%s*: *%s*" % (handler.feature, status.upper()) self.api.send_message(Message.create_reply(handler.event.message, response), parse_mode="Markdown")
def process(self, event): from_ = event.message.from_ if from_ is not None and str(from_.id) == self.config.admin_user_id: self._continue(event) else: error_response = "You are not allowed to perform this action (admins only)." self.api.send_message( Message.create_reply(event.message, error_response))
def process(self, event): message = event.message left_chat_member = message.left_chat_member if left_chat_member is not None: if left_chat_member.id != self.cache.bot_info.id: reply = Message.create_reply( message, "" + left_chat_member.first_name + " was kicked by " + message.from_.first_name) self.api.send_message(reply)
def get_response_show(self, event, messages, message_id): message = messages.get(message_id) if message is None: return Message.create("Invalid message_id.\nUse " + event.command + " to get valid message_ids.") user_storage_handler = UserStorageHandler.get_instance(self.state) if not OptOutManager(self.state).should_display_message( event, message.user_id): user = UserFormatter.retrieve_and_format(message.user_id, user_storage_handler) return FormattedText().normal("🙁 Sorry, ").bold(user).normal( " has opted-out from this feature.").build_message() return message.printable_full_message(user_storage_handler)
def full_printable_version(self, user_storage_handler): formatted_user = UserFormatter.retrieve_and_format( self.user_id, user_storage_handler) formatted_date = DateFormatter.format_full(self.date) formatted_duration = TimeFormatter.format(self.duration) formatted_size = SizeFormatter.format(self.file_size) text = "\U0001f446 That is the audio with id: {}\n\n".format( self.message_id) text += "Author: {}\n".format(formatted_user) text += "Sent at: {}\n".format(formatted_date) text += "Duration: {}\n".format(formatted_duration) text += "Size: {}".format(formatted_size) return Message.create(text).reply_to_message( message_id=self.message_id)
def process(self, event): chat = event.message.chat if chat.type == "private": # lets consider private chat members are admins :) self._continue(event) else: user = event.message.from_ if user is not None: chat_member = self.api.no_async.getChatMember(chat_id=chat.id, user_id=user.id) if chat_member.status in ("creator", "administrator"): self._continue(event) else: error_response = "Sorry, this command is only available to group admins." self.api.send_message( Message.create_reply(event.message, error_response))
def _get_response_empty(): return Message.create("I have not seen any audios here.\n" "Send some of them and try again.")
def get_response_show(self, event, voices, message_id): voice = voices.get(message_id) if voice is None: return Message.create("No such audio with that message_id.") user_storage_handler = UserStorageHandler.get_instance(self.state) return voice.full_printable_version(user_storage_handler)
def get_response_empty(): return Message.create( "I have not seen any hashtag in this chat.\n" "Write some and try again (hint: #ThisIsAHashTag).")
def process(self, event): response_text = "Restarting bot...\nCommands might not work while restarting." self.api.no_async.send_message( Message.create_reply(event.message, response_text)) raise KeyboardInterrupt()
def send_message(self, chat, message_id, text): self.api.send_message(Message.create(text, chat.id), reply_to_message_id=message_id)
def get_response_empty(): return Message.create("I have not seen any messages here.\n" "Write some messages and try again.")
def process(self, event): self.api.send_message(Message.create_reply(event.message, self.text))
def process(self, event): response_text = "Bot stopped.\nYou need to launch it manually for it to work again." self.api.no_async.send_message( Message.create_reply(event.message, response_text)) sys.exit(EXIT_STATUS_TO_HALT_BOT)
def get_message(self): return Message.create(self.text)
def get_response_empty(self): return Message.create( self.__formatted( _("I have not seen any {poles} here.\n" "Wait until next day start, make a {pole} and try again.")))
def _send_to(self, api_message: Message, chat_id: int): api_message.to_chat(chat_id=chat_id) self.api.send_message(api_message)
def send_message(self, message: Message, **params): message_params = message.data.copy() message_params.update(params) if self.__should_send_message(message_params): send_func = self.__get_send_func(message.get_type()) return send_func(**message_params)
def _edit(self, message: Message): message.set_message_id(self.message_id) self.api.editMessageText(**message.data)
def build_message(self): return Message.create(self.text, parse_mode=self.mode)
def _send(self, text: str, message: MessageViewModel): api_message = Message.create(text) self._send_message(api_message, message)