Exemplo n.º 1
0
    def SendTo(self, contact, content):        
        if not isinstance(contact, QContact):
            return '错误:消息接受者必须为一个 QContact 对象'
        
        if contact.ctype.endswith('-member'):
            return '错误:不能给群成员或讨论组成员发消息'
        
        if not isinstance(content, str):
            return '错误:只能发送字符串消息'

        if not content:
            return '错误:不允许发送空消息'

        ctype = 'buddy' if contact.ctype.endswith('member') else contact.ctype

        result = '向 %s 发消息成功' % contact
        while content:
            front, content = Partition(content, 600)
            try:
                self.send(ctype, contact.uin, front)
            except Exception as e:
                result = '错误:向 %s 发消息失败 %s' % (str(contact), e)
                ERROR(result, exc_info=(not isinstance(e, RequestError)))
                break
            INFO('%s:%s' % (result, front))

        return result
Exemplo n.º 2
0
    def SendTo(self, contact, content, resendOn1202=True):
        result = None

        if not hasattr(contact, 'ctype'):
            result = '错误:消息接受者必须为一个 QContact 对象'

        if contact.ctype.endswith('-member'):
            result = '错误:不能给群成员或讨论组成员发消息'

        if PY3:
            if isinstance(content, str):
                content = content
            elif isinstance(content, bytes):
                content = content.decode('utf8')
            else:
                result = '错误:消息内容必须为 str 或 bytes 对象'
        else:
            if isinstance(content, str):
                content = content
            elif isinstance(content, unicode):
                content = content.encode('utf8')
            else:
                result = '错误:消息内容必须为 str 或 unicode 对象'

        if not content:
            result = '错误:不允许发送空消息'

        if result:
            ERROR(result)
            return result

        epCodes = resendOn1202 and [0] or [0, 1202]

        result = '向 %s 发消息成功' % contact
        while content:
            front, content = Partition(content)
            try:
                self.send(contact.ctype, contact.uin, front, epCodes)
            except Exception as e:
                result = '错误:向 %s 发消息失败 %s' % (str(contact), e)
                ERROR(result, exc_info=(not isinstance(e, RequestError)))
                break
            else:
                INFO('%s:%s' % (result, front))
        return result