def __repr__(self): _repr = "{klass}({msg})".format(klass=self.__class__.__name__, msg=repr(self._data)) if six.PY2: return to_binary(_repr) else: return to_text(_repr)
def encode(cls, text): length = len(text) padding_count = cls.block_size - length % cls.block_size if padding_count == 0: padding_count = cls.block_size padding = to_binary(chr(padding_count)) return text + padding * padding_count
def __repr__(self): _repr = '{klass}({name})'.format( klass=self.__class__.__name__, name=repr(self.name) ) if six.PY2: return to_binary(_repr) else: return to_text(_repr)
def __repr__(self): _repr = '{klass}({code}, {msg}'.format( klass=self.__class__.__name__, code=self.errcode, msg=self.errmsg ) if six.PY2: return to_binary(_repr) else: return to_text(_repr)
def __str__(self): if six.PY2: return to_binary('Error code: {code}, message: {msg}'.format( code=self.errcode, msg=self.errmsg )) else: return to_text('Error code: {code}, message: {msg}'.format( code=self.errcode, msg=self.errmsg ))
def delete_account(self, account): """ 删除客服账号 详情请参考 http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html :param account: 完整客服账号,格式为:账号前缀@公众号微信号 :return: 返回的 JSON 数据包 """ params_data = [ 'access_token={0}'.format(quote(self.access_token)), 'kf_account={0}'.format(quote(to_binary(account), safe=b'/@')), ] params = '&'.join(params_data) return self._get( 'https://api.weixin.qq.com/customservice/kfaccount/del', params=params )
def _encrypt_message(self, msg, nonce, timestamp=None, crypto_class=None): from sdk.wechatpy import BaseReply xml = """<xml> <Encrypt><![CDATA[{encrypt}]]></Encrypt> <MsgSignature><![CDATA[{signature}]]></MsgSignature> <TimeStamp>{timestamp}</TimeStamp> <Nonce><![CDATA[{nonce}]]></Nonce> </xml>""" if isinstance(msg, BaseReply): msg = msg.render() timestamp = timestamp or to_binary(int(time.time())) pc = crypto_class(self.key) encrypt = to_text(pc.encrypt(msg, self._id)) signature = _get_signature(self.token, timestamp, nonce, encrypt) return to_text( xml.format(encrypt=encrypt, signature=signature, timestamp=timestamp, nonce=nonce))
def update_account(self, account, nickname, password): """ 更新客服账号 详情请参考 http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html :param account: 完整客服账号,格式为:账号前缀@公众号微信号 :param nickname: 客服昵称,最长6个汉字或12个英文字符 :param password: 客服账号登录密码 :return: 返回的 JSON 数据包 """ password = to_binary(password) password = hashlib.md5(password).hexdigest() return self._post( 'https://api.weixin.qq.com/customservice/kfaccount/update', data={ 'kf_account': account, 'nickname': nickname, 'password': password } )
def __str__(self): if six.PY2: return to_binary(self.render()) else: return to_text(self.render())
def __init__(self, token, encoding_aes_key, _id): encoding_aes_key = to_binary(encoding_aes_key + '=') self.key = base64.b64decode(encoding_aes_key) assert len(self.key) == 32 self.token = token self._id = _id
def __base64_decode(self, text): return to_text(base64.b64decode(to_binary(text)))