Beispiel #1
0
 def card(self):
     """
     * 好友请求中的请求用户
     * 名片消息中的推荐用户
     """
     if self.type in (CARD, FRIENDS):
         return User(self.raw.get('RecommendInfo'), self.bot)
Beispiel #2
0
    def __init__(self, raw, bot):
        self.raw = raw

        self.bot = bot
        self.type = self.raw.get('Type')

        self.is_at = self.raw.get('isAt')
        self.file_name = self.raw.get('FileName')
        self.img_height = self.raw.get('ImgHeight')
        self.img_width = self.raw.get('ImgWidth')
        self.play_length = self.raw.get('PlayLength')
        self.url = self.raw.get('Url')
        self.voice_length = self.raw.get('VoiceLength')
        self.id = self.raw.get('NewMsgId')

        self.text = None
        self.get_file = None
        self.create_time = None
        self.location = None
        self.card = None

        text = self.raw.get('Text')
        if callable(text):
            self.get_file = text
        else:
            self.text = text

        # noinspection PyBroadException
        try:
            self.create_time = datetime.fromtimestamp(
                self.raw.get('CreateTime'))
        except:
            pass

        if self.type == MAP:
            try:
                self.location = ETree.fromstring(
                    self.raw['OriContent']).find('location').attrib
                try:
                    self.location['x'] = float(self.location['x'])
                    self.location['y'] = float(self.location['y'])
                    self.location['scale'] = int(self.location['scale'])
                    self.location['maptype'] = int(self.location['maptype'])
                except (KeyError, ValueError):
                    pass
                self.text = self.location.get('label')
            except (TypeError, KeyError, ValueError, ETree.ParseError):
                pass
        elif self.type in (CARD, FRIENDS):
            self.card = User(self.raw.get('RecommendInfo'), self.bot)
            self.text = self.card.raw.get('Content')

        # 将 msg.sender.send* 方法绑定到 msg.reply*,例如 msg.sender.send_img => msg.reply_img
        for method in '', '_image', '_file', '_video', '_msg', '_raw_msg':
            setattr(self, 'reply' + method,
                    getattr(self.sender, 'send' + method))