def __init__(self, instance_id=None): super().__init__(instance_id) alice = EFBChat(self) alice.chat_name = "Alice" alice.chat_uid = "alice" alice.chat_type = ChatType.User self.alice = alice bob = EFBChat(self) bob.chat_name = "Bob" bob.chat_alias = "Little bobby" bob.chat_uid = "bob" bob.chat_type = ChatType.User self.bob = bob carol = EFBChat(self) carol.chat_name = "Carol" carol.chat_uid = "carol" carol.chat_type = ChatType.User self.carol = carol dave = EFBChat(self) dave.chat_name = "デブ" # Nah, that's a joke dave.chat_uid = "dave" dave.chat_type = ChatType.User self.dave = dave wonderland = EFBChat(self) wonderland.chat_name = "Wonderland" wonderland.chat_uid = "wonderland001" wonderland.chat_type = ChatType.Group wonderland.members = [bob.copy(), carol.copy(), dave.copy()] for i in wonderland.members: i.group = wonderland self.wonderland = wonderland self.chats: List[EFBChat] = [alice, bob, wonderland]
def __init__(self): alice = EFBChat(self) alice.chat_name = "Alice" alice.chat_uid = "alice" alice.chat_type = ChatType.User bob = EFBChat(self) bob.chat_name = "Bob" bob.chat_alias = "Little bobby" bob.chat_uid = "bob" bob.chat_type = ChatType.User carol = EFBChat(self) carol.chat_name = "Carol" carol.chat_uid = "carol" carol.chat_type = ChatType.User dave = EFBChat(self) dave.chat_name = "デブ" # Nah, that's a joke dave.chat_uid = "dave" dave.chat_type = ChatType.User wonderland = EFBChat(self) wonderland.chat_name = "Wonderland" wonderland.chat_uid = "wonderland001" wonderland.chat_type = ChatType.Group wonderland.members = [bob.copy(), carol.copy(), dave.copy()] for i in wonderland.members: i.group = wonderland i.is_chat = False self.chats: List[EFBChat] = [alice, bob, wonderland]
def test_verify(self): channel = MockMasterChannel() with self.subTest("Valid chat"): chat = EFBChat(channel) chat.chat_uid = "00001" chat.chat_name = "Chat" chat.chat_alias = "chaT" chat.chat_type = ChatType.User chat.verify() with self.subTest("Missing UID"): chat = EFBChat(channel) chat.chat_name = "Chat" chat.chat_type = ChatType.User with self.assertRaises(ValueError): chat.verify() with self.subTest("Wrong chat type"): chat = EFBChat(channel) chat.chat_uid = "00001" chat.chat_name = "Chat" chat.chat_type = "user" with self.assertRaises(ValueError): chat.verify()
def wxpy_chat_to_efb_chat(self, chat: wxpy.Chat, recursive=True) -> EFBChat: # self.logger.debug("Converting WXPY chat %r, %sin recursive mode", chat, '' if recursive else 'not ') # self.logger.debug("WXPY chat with ID: %s, name: %s, alias: %s;", chat.puid, chat.nick_name, chat.alias) if chat is None: return self.MISSING_USER if chat.puid in self.efb_chat_objs: return self.efb_chat_objs[chat.puid] efb_chat = EFBChat(self.channel) efb_chat.chat_uid = chat.puid or "__invalid__" efb_chat.chat_name = ews_utils.wechat_string_unescape(chat.nick_name) efb_chat.chat_alias = None efb_chat.chat_type = ChatType.System efb_chat.vendor_specific = {'is_mp': False} if isinstance(chat, wxpy.Member): efb_chat.chat_type = ChatType.User efb_chat.is_chat = False efb_chat.chat_alias = chat.name # self.logger.debug("[WXPY: %s] Display name: %s;", chat.puid, chat.display_name) if recursive: efb_chat.group = self.wxpy_chat_to_efb_chat(chat.group, False) elif isinstance(chat, wxpy.Group): efb_chat.chat_type = ChatType.Group for i in chat.members: efb_chat.members.append(self.wxpy_chat_to_efb_chat(i, False)) efb_chat.members[-1].group = efb_chat elif isinstance(chat, wxpy.MP): efb_chat.chat_type = ChatType.User efb_chat.vendor_specific['is_mp'] = True elif isinstance(chat, wxpy.User): efb_chat.chat_type = ChatType.User efb_chat.chat_alias = chat.remark_name or efb_chat.chat_alias # self.logger.debug("[WXPY: %s] Remark name: %s;", chat.puid, chat.remark_name) if chat == chat.bot.self: efb_chat.self() efb_chat.chat_alias = efb_chat.chat_alias and ews_utils.wechat_string_unescape( efb_chat.chat_alias) efb_chat.vendor_specific.update(self.generate_vendor_specific(chat)) if efb_chat.vendor_specific.get('is_muted', False): efb_chat.notification = EFBChatNotificationState.MENTIONS # self.logger.debug('WXPY chat %s converted to EFBChat %s', chat.puid, efb_chat) if chat.puid: self.efb_chat_objs[chat.puid] = efb_chat return efb_chat
def test_verify_valid_chat_middleware(middleware): chat = EFBChat(middleware=middleware) chat.chat_uid = "00001" chat.chat_name = "Chat" chat.chat_alias = "chaT" chat.chat_type = ChatType.User chat.verify()
def exit_callback(self): self.logger.debug('Calling exit callback...') if self._stop_polling_event.is_set(): return msg = EFBMsg() chat = EFBChat(self).system() chat.chat_type = ChatType.System chat.chat_name = self._("EWS User Auth") msg.chat = msg.author = chat msg.deliver_to = coordinator.master msg.text = self._( "WeChat server has logged you out. Please log in again when you are ready." ) msg.uid = "__reauth__.%s" % int(time.time()) msg.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 = EFBMsgCommands([ EFBMsgCommand(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 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 = EFBMsg() chat = EFBChat(self).system() chat.chat_type = ChatType.System chat.chat_name = self._("EWS User Auth") msg.chat = msg.author = chat msg.deliver_to = coordinator.master msg.text = self._("WeChat server has logged you out. Please log in again when you are ready.") msg.uid = f"__reauth__.{int(time.time())}" msg.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 = EFBMsgCommands( [EFBMsgCommand(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 test_verify_valid_chat(slave_channel): chat = EFBChat(slave_channel) chat.chat_uid = "00001" chat.chat_name = "Chat" chat.chat_alias = "chaT" chat.chat_type = ChatType.User chat.verify()
def test_verify_wrong_chat_type(slave_channel): chat = EFBChat(slave_channel) chat.chat_uid = "00001" chat.chat_name = "Chat" chat.chat_type = "user" with pytest.raises(ValueError): chat.verify()
def test_copy(slave_channel): chat = EFBChat(slave_channel) chat.chat_uid = "00001" chat.chat_name = "Chat" chat.chat_alias = "chaT" chat.chat_type = ChatType.User copy = chat.copy() assert chat == copy assert chat is not copy
def get_efb_chats(self): chat_list = requests.get(user_url % (self.hostname, self.port)).json() result = [] for chat_item in chat_list: efb_chat = EFBChat(self.channel) efb_chat.chat_uid = chat_item["userId"] title = chat_item["title"] if title.startswith("[Group] "): efb_chat.chat_name = title[8:] efb_chat.chat_type = ChatType.Group else: efb_chat.chat_name = title efb_chat.chat_type = ChatType.User efb_chat.chat_alias = None efb_chat.is_chat = True efb_chat.vendor_specific = {'is_anonymous': False} result.append(efb_chat) return result
def test_copy(self): channel = MockMasterChannel() chat = EFBChat(channel) chat.chat_uid = "00001" chat.chat_name = "Chat" chat.chat_alias = "chaT" chat.chat_type = ChatType.User copy = chat.copy() assert chat == copy assert chat is not copy
def test_copy(self): channel = MockMasterChannel() chat = EFBChat(channel) chat.chat_uid = "00001" chat.chat_name = "Chat" chat.chat_alias = "chaT" chat.chat_type = ChatType.User copy = chat.copy() self.assertEqual(chat, copy) self.assertIsNot(chat, copy)
def test_pickle(slave_channel): chat = EFBChat(slave_channel) chat.chat_uid = "00001" chat.chat_name = "Chat" chat.chat_alias = "chaT" chat.chat_type = ChatType.User chat_dup = pickle.loads(pickle.dumps(chat)) for attr in ("module_name", "module_id", "channel_emoji", "chat_name", "chat_type", "chat_alias", "chat_uid", "is_chat"): assert getattr(chat, attr) == getattr(chat_dup, attr)
def build_efb_chat_as_anonymous_user(self, context): efb_chat = EFBChat(self.channel) anonymous_data = context['anonymous'] efb_chat.chat_uid = 'anonymous' + '_' + anonymous_data['flag'] efb_chat.chat_name = '[Anonymous] ' + anonymous_data['name'] efb_chat.chat_type = ChatType.User efb_chat.is_chat = False efb_chat.vendor_specific = { 'is_anonymous': True, 'anonymous_id': anonymous_data['id'] } efb_chat.group = self.build_efb_chat_as_group(context) return efb_chat
def wxpy_chat_to_efb_chat(self, chat: wxpy.Chat, recursive=True) -> Optional[EFBChat]: self.logger.debug("Converting WXPY chat %r, %sin recursive mode", chat, '' if recursive else 'not ') self.logger.debug("WXPY chat with ID: %s, name: %s, alias: %s;", chat.puid, chat.nick_name, chat.alias) if chat is None: return self.MISSING_USER efb_chat = EFBChat(self.channel) efb_chat.chat_uid = chat.puid or "__invalid__" efb_chat.chat_name = ews_utils.wechat_string_unescape(chat.nick_name) efb_chat.chat_alias = None efb_chat.chat_type = ChatType.System efb_chat.vendor_specific = {'is_mp': False, 'wxpy_object': chat} if isinstance(chat, wxpy.Member): efb_chat.chat_type = ChatType.User efb_chat.is_chat = False efb_chat.chat_alias = chat.display_name or efb_chat.chat_alias self.logger.debug("[WXPY: %s] Display name: %s;", chat.puid, chat.display_name) if recursive: efb_chat.group = self.wxpy_chat_to_efb_chat(chat.group, False) elif isinstance(chat, wxpy.Group): efb_chat.chat_type = ChatType.Group for i in chat.members: efb_chat.members.append(self.wxpy_chat_to_efb_chat(i, False)) efb_chat.members[-1].group = efb_chat elif isinstance(chat, wxpy.MP): efb_chat.chat_type = ChatType.User efb_chat.vendor_specific['is_mp'] = True elif isinstance(chat, wxpy.User): efb_chat.chat_type = ChatType.User efb_chat.chat_alias = chat.remark_name or efb_chat.chat_alias self.logger.debug("[WXPY: %s] Remark name: %s;", chat.puid, chat.remark_name) if chat == chat.bot.self: efb_chat.self() efb_chat.chat_alias = efb_chat.chat_alias and ews_utils.wechat_string_unescape(efb_chat.chat_alias) self.logger.debug('WXPY chat %s converted to EFBChat %s', chat.puid, efb_chat) return efb_chat
def get_chat_from_db(self, channel_id: str, chat_id: str) -> Optional[EFBChat]: d = self.db.get_slave_chat_info(slave_channel_id=channel_id, slave_chat_uid=chat_id) if d: chat = EFBChat(coordinator.slaves[channel_id]) chat.chat_name = d.slave_chat_name chat.chat_alias = d.slave_chat_alias chat.chat_uid = d.slave_chat_uid chat.chat_type = ChatType(d.slave_chat_type) return chat else: chat = coordinator.slaves[channel_id].get_chat(chat_id) if chat: self._db_update_slave_chats_cache([chat]) return chat
def send_msg_to_master(self, context): self.logger.debug(repr(context)) msg = EFBMsg() efb_chat = EFBChat(self.channel).system() efb_chat.chat_type = ChatType.System efb_chat.chat_name = context['event_description'] msg.chat = msg.author = efb_chat msg.deliver_to = coordinator.master msg.type = MsgType.Text msg.uid = "__{context[uid_prefix]}__.{uni_id}".format( context=context, uni_id=str(int(time.time()))) if 'message' in context: msg.text = context['message'] if 'commands' in context: msg.commands = EFBMsgCommands(context['commands']) coordinator.send_message(msg)
def build_efb_chat_as_user(self, context, is_chat): efb_chat = EFBChat(self.channel) uid = context['user_id'] efb_chat.chat_uid = 'private' + '_' + str(uid) chat_name = '' if 'nickname' not in context: i: dict = self.channel.QQClient.get_stranger_info(uid) chat_name = i['nickname'] else: chat_name = context['nickname'] efb_chat.chat_name = chat_name efb_chat.chat_alias = None if 'alias' not in context else context[ 'alias'] efb_chat.chat_type = ChatType.User efb_chat.is_chat = is_chat efb_chat.vendor_specific = {'is_anonymous': False} if not is_chat and context['message_type'] != 'private': efb_chat.group = self.build_efb_chat_as_group(context) return efb_chat
def get_chat_from_db(self, channel_id: ModuleID, chat_id: ChatID) -> Optional[EFBChat]: if channel_id not in coordinator.slaves: return None d = self.db.get_slave_chat_info(slave_channel_id=channel_id, slave_chat_uid=chat_id) if d: chat = EFBChat(coordinator.slaves[channel_id]) chat.chat_name = d.slave_chat_name chat.chat_alias = d.slave_chat_alias chat.chat_uid = d.slave_chat_uid chat.chat_type = ChatType(d.slave_chat_type) return chat else: try: chat = coordinator.slaves[channel_id].get_chat(chat_id) if chat: self._db_update_slave_chats_cache([chat]) return chat return None except EFBChatNotFound: return None
def build_efb_chat_as_group(self, context): efb_chat = EFBChat(self.channel) efb_chat.chat_type = ChatType.Group is_discuss = False if context['message_type'] == 'group' else True chat_uid = context['discuss_id'] if is_discuss else context['group_id'] if not is_discuss: efb_chat.chat_uid = 'group' + '_' + str(chat_uid) i = self.channel.QQClient.get_group_info(chat_uid) if i is not None: efb_chat.chat_name = i[ 'group_name'] if 'group_name' not in context else context[ 'group_name'] else: efb_chat.chat_name = chat_uid efb_chat.vendor_specific = {'is_discuss': False} # todo Add user to efb_chat.member else: efb_chat.chat_uid = 'discuss' + '_' + str(chat_uid) efb_chat.chat_name = 'Discuss Group' + '_' + str(chat_uid) # todo Find a way to distinguish from different discuss group efb_chat.vendor_specific = {'is_discuss': True} return efb_chat
def test_verify_missing_uid(slave_channel): chat = EFBChat(slave_channel) chat.chat_name = "Chat" chat.chat_type = ChatType.User with pytest.raises(ValueError): chat.verify()
def build_efb_chat_as_system_user(self, context): # System user only! efb_chat = EFBChat(self.channel).system() efb_chat.chat_type = ChatType.System efb_chat.chat_name = context['event_description'] return efb_chat
import pickle from tempfile import NamedTemporaryFile from unittest import mock import pytest from ehforwarderbot import EFBMsg, EFBChat, ChatType, MsgType, coordinator from ehforwarderbot.message import EFBMsgLinkAttribute, EFBMsgLocationAttribute, EFBMsgStatusAttribute, EFBMsgCommands, \ EFBMsgCommand from .mocks.master import MockMasterChannel coordinator.master = MockMasterChannel() chat = EFBChat(channel=coordinator.master) chat.chat_name = "Chat 0" chat.chat_uid = "0" chat.chat_type = ChatType.User media_types = (MsgType.Image, MsgType.Audio, MsgType.File, MsgType.Sticker) def test_verify_text_msg(): msg = EFBMsg() msg.deliver_to = coordinator.master msg.author = chat msg.chat = chat msg.type = MsgType.Text msg.text = "Message" msg.verify() @pytest.mark.parametrize("media_type", media_types, ids=str)