def update(self, group_id, name): """ 修改分组名 详情请参考 http://mp.weixin.qq.com/wiki/0/56d992c605a97245eb7e617854b169fc.html :param group_id: 分组id,由微信分配 :param name: 分组名字(30个字符以内) :return: 返回的 JSON 数据包 使用示例:: from wechatpy import WeChatClient client = WeChatClient('appid', 'secret') res = client.group.update(1234, 'New Name') """ name = to_text(name) return self._post( 'groups/update', data={ 'group': { 'id': int(group_id), 'name': name } } )
def parse_message(xml): """ 解析微信服务器推送的 XML 消息 :param xml: XML 消息 :return: 解析成功返回对应的消息或事件,否则返回 ``UnknownMessage`` """ if not xml: return message = xmltodict.parse(to_text(xml))['xml'] message_type = message['MsgType'].lower() if message_type in ('event', 'device_event'): event_type = message['Event'].lower() # special event type for device_event if message_type == 'device_event': event_type = 'device_{event}'.format(event=event_type) if event_type == 'subscribe' and message.get('EventKey'): event_key = message['EventKey'] if event_key.startswith(('scanbarcode|', 'scanimage|')): event_type = 'subscribe_scan_product' message['Event'] = event_type else: # Scan to subscribe with scene id event event_type = 'subscribe_scan' message['Event'] = event_type message['EventKey'] = event_key.replace('qrscene_', '') message_class = EVENT_TYPES.get(event_type, UnknownMessage) else: message_class = MESSAGE_TYPES.get(message_type, UnknownMessage) return message_class(message)
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 __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 _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 get_unauthorized(self, msg, signature, timestamp, nonce): """ 处理取消授权通知 :params msg: 加密内容 :params signature: 消息签名 :params timestamp: 时间戳 :params nonce: 随机数 """ content = self.crypto.decrypt_message(msg, signature, timestamp, nonce) message = xmltodict.parse(to_text(content))['xml'] return ComponentUnauthorizedMessage(message)
def cache_component_verify_ticket(self, msg, signature, timestamp, nonce): """ 处理 wechat server 推送的 component_verify_ticket消息 :params msg: 加密内容 :params signature: 消息签名 :params timestamp: 时间戳 :params nonce: 随机数 """ content = self.crypto.decrypt_message(msg, signature, timestamp, nonce) message = xmltodict.parse(to_text(content))['xml'] o = ComponentVerifyTicketMessage(message) self.session.set(o.type, o.verify_ticket, 600)
def _decrypt_message(self, msg, signature, timestamp, nonce, crypto_class=None): if not isinstance(msg, dict): import xmltodict msg = xmltodict.parse(to_text(msg))['xml'] encrypt = msg['Encrypt'] _signature = _get_signature(self.token, timestamp, nonce, encrypt) if _signature != signature: raise InvalidSignatureException() pc = crypto_class(self.key) return pc.decrypt(encrypt, self._id)
def create(self, name): """ 创建分组 详情请参考 http://mp.weixin.qq.com/wiki/0/56d992c605a97245eb7e617854b169fc.html :param name: 分组名字(30个字符以内) :return: 返回的 JSON 数据包 使用示例:: from wechatpy import WeChatClient client = WeChatClient('appid', 'secret') res = client.group.create('New Group') """ name = to_text(name) return self._post( 'groups/create', data={'group': {'name': name}} )
def get(self, key, default=None): key = self.key_name(key) value = self.redis.get(key) if value is None: return default return json.loads(to_text(value))
def __str__(self): if six.PY2: return to_binary(self.render()) else: return to_text(self.render())
def __to_text(self, value): return to_text(value)
def __base64_decode(self, text): return to_text(base64.b64decode(to_binary(text)))