def exit_callback(self): # Don't send prompt if there's nowhere to send. if not getattr(coordinator, 'master', None): raise Exception( self._("Web WeChat logged your account out before master channel is ready.")) self.logger.debug('Calling exit callback...') if self._stop_polling_event.is_set(): return msg = Message( chat=self.user_auth_chat, author=self.user_auth_chat.other, deliver_to=coordinator.master, text=self._( "WeChat server has logged you out. Please log in again when you are ready."), uid=f"__reauth__.{uuid4()}", type=MsgType.Text, ) on_log_out = self.flag("on_log_out") on_log_out = on_log_out if on_log_out in ( "command", "idle", "reauth") else "command" if on_log_out == "command": msg.type = MsgType.Text msg.commands = MessageCommands( [MessageCommand(name=self._("Log in again"), callable_name="reauth", kwargs={"command": True})]) elif on_log_out == "reauth": if self.flag("qr_reload") == "console_qr_code": msg.text += "\n" + self._("Please check your log to continue.") self.reauth() coordinator.send_message(msg)
def make_status_message(self, msg_base: Message = None, mid: MessageID = None) -> Message: if mid is not None: msg = self.message_cache[mid] elif msg_base is not None: msg = msg_base else: raise ValueError reply = Message( type=MsgType.Text, chat=msg.chat, author=msg.chat.make_system_member(uid=ChatID("filter_info"), name="Filter middleware", middleware=self), deliver_to=coordinator.master, ) if mid: reply.uid = mid else: reply.uid = str(uuid.uuid4()) status = self.filter_reason(msg) if not status: # Blue circle emoji status = "\U0001F535 This chat is not filtered." else: # Red circle emoji status = "\U0001F534 " + status reply.text = "Filter status for chat {chat_id} from {module_id}:\n" \ "\n" \ "{status}\n".format( module_id=msg.chat.module_id, chat_id=msg.chat.id, status=status ) command = MessageCommand(name="%COMMAND_NAME%", callable_name="toggle_filter_by_chat_id", kwargs={ "mid": reply.uid, "module_id": msg.chat.module_id, "chat_id": msg.chat.id }) if self.is_chat_filtered_by_id(msg.chat): command.name = "Unfilter by chat ID" command.kwargs['value'] = False else: command.name = "Filter by chat ID" command.kwargs['value'] = True reply.commands = MessageCommands([command]) return reply
def test_pickle_commands_attribute(): commands = MessageCommands([ MessageCommand(name="Command 1", callable_name="command_1", args=(1, 2, 3), kwargs={"four": 4, "five": 5}), MessageCommand(name="Command 2", callable_name="command_2", args=("1", "2", "3"), kwargs={"four": "4", "five": "5"}) ]) commands_dup = pickle.loads(pickle.dumps(commands)) for cmd in range(len(commands)): for attr in ("name", "callable_name", "args", "kwargs"): assert getattr(commands[cmd], attr) == \ getattr(commands_dup[cmd], attr)
def test_verify_message_command(base_message): msg = base_message msg.type = MsgType.Text command = MessageCommand(name="Command 1", callable_name="command_1") msg.commands = MessageCommands([command]) msg.verify() with pytest.raises(ValueError): # noinspection PyTypeChecker MessageCommand("name", "callable_name", args="args", kwargs="kwargs") # type: ignore
def wechat_friend_msg(self, msg: wxpy.Message) -> Message: # TRANSLATORS: Gender of contact txt = self.generate_card_info(msg) return Message( text=txt, type=MsgType.Text, commands=MessageCommands([ MessageCommand( name=self._("Accept friend request"), callable_name="accept_friend", kwargs={"username": msg.card.user_name} ) ]) )
def wechat_friend_msg(self, msg: wxpy.Message) -> Message: gender = { 1: self._("M"), 2: self._("F") }.get(msg.card.sex, msg.card.sex) txt = (self._("Card: {user.nick_name}\n" "From: {user.province}, {user.city}\n" "Bio: {user.signature}\n" "Gender: {gender}")) txt = txt.format(user=msg.card, gender=gender) return Message(text=txt, type=MsgType.Text, commands=MessageCommands([ MessageCommand( name=self._("Accept friend request"), callable_name="accept_friend", kwargs={"username": msg.card.user_name}) ]))
def build_message_commands() -> MessageCommands: return MessageCommands([ MessageCommand("Ping!", "command_ping"), MessageCommand("Bam", "command_bam"), ])