Exemplo n.º 1
0
 def wechat_text_msg(self, msg: wxpy.Message) -> EFBMsg:
     if msg.chat.user_name == "newsapp" and msg.text.startswith(
             "<mmreader>"):
         return self.wechat_newsapp_msg(msg)
     if msg.text.startswith(
             "http://weixin.qq.com/cgi-bin/redirectforward?args="):
         return self.wechat_location_msg(msg)
     efb_msg = EFBMsg()
     efb_msg.text = ews_utils.wechat_string_unescape(msg.text)
     efb_msg.type = MsgType.Text
     if msg.is_at:
         found = False
         for i in re.finditer("@([^@]*)(?=\u2005|$)", msg.text):
             if i.groups()[0] in (self.bot.self.name,
                                  msg.chat.self.display_name):
                 found = True
                 efb_msg.substitutions = EFBMsgSubstitutions(
                     {i.span(): EFBChat(self.channel).self()})
         if not found:
             append = "@" + self.bot.self.name
             efb_msg.substitutions = EFBMsgSubstitutions({
                 (len(msg.text) + 1, len(msg.text) + 1 + len(append)):
                 EFBChat(self.channel).self()
             })
             efb_msg.text += " " + append
     return efb_msg
Exemplo n.º 2
0
    def qq_group_broadcast_wrapper(self, data):
        try:
            at_list = {}
            content_data = json.loads(data['content'])
            text_data = base64.b64decode(content_data['mannounce']['text']).decode("UTF-8")
            title_data = base64.b64decode(content_data['mannounce']['title']).decode("UTF-8")
            text = "[群公告] 【{title}】\n{text}".format(title=title_data, text=text_data)

            if text == '':
                substitution_begin = len(text)
                substitution_end = len(text) + len('@all') + 1
                text += '@all '
            else:
                substitution_begin = len(text) + 1
                substitution_end = len(text) + len('@all') + 2
                text += ' @all '
            at_list[(substitution_begin, substitution_end)] = EFBChat(self.inst.channel).self()

            if 'pic' in content_data['mannounce']:  # Picture Attached
                # Assuming there's only one picture
                data['url'] = "http://gdynamic.qpic.cn/gdynamic/{}/628".format(
                    content_data['mannounce']['pic'][0]['url'])
                efb_message = self.qq_image_wrapper(data)[0]
                efb_message.text = text
                efb_message.substitutions = EFBMsgSubstitutions(at_list)
                return efb_message
            else:
                return self.qq_text_simple_wrapper(text, at_list)
        except Exception:
            return self.qq_group_broadcast_alternative_wrapper(data)
Exemplo n.º 3
0
    def qq_group_broadcast_alternative_wrapper(self, data):
        try:
            at_list = {}
            content_data = json.loads(data['content'])
            group_id = content_data['mannounce']['gc']
            notice_raw_data = self.inst.coolq_api_query("_get_group_notice",
                                                        group_id=group_id)
            notice_data = json.loads(notice_raw_data)
            title_data = html.unescape(notice_data[0]['msg']['title'])
            text_data = html.unescape(notice_data[0]['msg']['text'])
            text = "[群公告] 【{title}】\n{text}".format(title=title_data, text=text_data)

            if text == '':
                substitution_begin = len(text)
                substitution_end = len(text) + len('@all') + 1
                text += '@all '
            else:
                substitution_begin = len(text) + 1
                substitution_end = len(text) + len('@all') + 2
                text += ' @all '
            at_list[(substitution_begin, substitution_end)] = EFBChat(self.inst.channel).self()

            if 'pics' in html.unescape(notice_data[0]['msg']):  # Picture Attached
                # Assuming there's only one picture
                data['url'] = "http://gdynamic.qpic.cn/gdynamic/{}/628".format(notice_data[0]['msg']['pics'][0]['id'])
                efb_message = self.qq_image_wrapper(data)[0]
                efb_message.text = text
                efb_message.substitutions = EFBMsgSubstitutions(at_list)
                return efb_message
            else:
                return self.qq_text_simple_wrapper(text, at_list)
        except Exception:
            return None
Exemplo n.º 4
0
 def qq_text_simple_wrapper(self, text: str, ats: dict):  # This cute function only accepts string!
     efb_msg = EFBMsg()
     efb_msg.type = MsgType.Text
     efb_msg.text = text
     if ats:  # This is used to replace specific text with @blahblah
         # And Milkice really requires a brain check
         efb_msg.substitutions = EFBMsgSubstitutions(ats)
     return efb_msg
Exemplo n.º 5
0
def test_substitution(base_message, chat):
    base_message.substitutions = EFBMsgSubstitutions({(0, 3): chat})
    base_message.verify()

    with pytest.raises(TypeError):
        EFBMsgSubstitutions([chat])

    with pytest.raises(TypeError):
        EFBMsgSubstitutions({(1, 2, 3): chat})

    with pytest.raises(TypeError):
        EFBMsgSubstitutions({(1, 2, 3): chat.chat_uid})

    with pytest.raises(TypeError):
        EFBMsgSubstitutions({(2, 1): chat})

    with pytest.raises(ValueError):
        EFBMsgSubstitutions({(1, 3): chat, (2, 4): chat})