zoukankan      html  css  js  c++  java
  • 接收用户消息

    微信公众账号能够接收用户发送的六种格式的消息:文本,图片,语音,视频(短视频),地理位置,链接。

        def responseMsg(self):
            encrypt_type = self.request.GET.get('encrypt_type', None)
            pc, postStr = self._encrypt_data(encrypt_type)
            postObj = self._parseMsg(postStr)
            print(postObj)
            msg_type = postObj['MsgType']
            result = ''
            if msg_type == 'text':
                result = self._receiveText(postObj)
            elif msg_type == 'image':
                result = self._receiveImage(postObj)
            elif msg_type == 'voice':
                result = self._receiveVoice(postObj)
            elif msg_type == 'video' or msg_type == 'shortvideo':
                print('视频')
                print(msg_type)
                result = self._receiveVideo(postObj)
            elif msg_type == 'location':
                result = self._receiveLocation(postObj)
            elif msg_type == 'link':
                result = self._receiveLink(postObj)
            elif msg_type == 'event':
                result = self._receiveEvent(postObj)
            else:
                result = '不能识别的 msg type: ' + msg_type
        def _transmitText(self,obj,content):
            xmlTpl ='''
            <xml>
      <ToUserName><![CDATA[{0}]]></ToUserName>
      <FromUserName><![CDATA[{1}]]></FromUserName>
      <CreateTime>{2}</CreateTime>
      <MsgType><![CDATA[text]]></MsgType>
      <Content><![CDATA[{3}]]></Content>
    </xml>
            '''
            result = xmlTpl.format(obj['FromUserName'],obj['ToUserName'],int(time.time()),content)
            return result

    接收消息类:

    '''微信公众号接收到的用户消息类型
    https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_standard_messages.html
    '''
    
    import xml.etree.ElementTree as ET
    
    def parse_xml(web_data):
        if len(web_data) == 0:
            return None
        xmlData = ET.fromstring(web_data)
        msg_type = xmlData.find('MsgType').text
        if msg_type == 'text':
            return ReceiveTextMsg(xmlData)
        elif msg_type == 'image':
            return ReceiveImageMsg(xmlData)
        elif msg_type == 'voice':
            return ReceiveVoiceMsg(xmlData)
        elif msg_type in ('video','shortvideo'):
            return ReceiveVideoMsg(xmlData)
        elif msg_type == 'location':
            return ReceiveLocationMsg(xmlData)
        elif msg_type == 'link':
            return ReceiveLinkMsg(xmlData)
        else:
            print('不能识别的消息类型:'+ msg_type)
            return None
    
    class ReceiveMsg(object):
        '''基类'''
        def __init__(self,xmlData):
            self.ToUserName = xmlData.find('ToUserName').text
            self.FromUserName = xmlData.find('FromUserName').text
            self.CreateTime = xmlData.find('CreateTime').text
            self.MsgType = xmlData.find('MsgType').text
            self.MsgId = xmlData.find('MsgId').text
    
    class ReceiveTextMsg(ReceiveMsg):
        '''文本消息'''
        def __init__(self,xmlData):
            super(ReceiveTextMsg,self).__init__(xmlData)
            self.Content = xmlData.find('Content').text
    
    class ReceiveImageMsg(ReceiveMsg):
        '''图片消息'''
        def __init__(self,xmlData):
            super(ReceiveImageMsg,self).__init__(xmlData)
            self.PicUrl = xmlData.find('PicUrl').text
            self.MediaId = xmlData.find('MediaId').text
    
    class ReceiveVoiceMsg(ReceiveMsg):
        '''语音消息'''
        def __init__(self,xmlData):
            super(ReceiveVoiceMsg,self).__init__(xmlData)
            self.Format = xmlData.find('Format').text
            self.MediaId = xmlData.find('MediaId').text
            self.Recognition = ''
            if xmlData.find('Recognition') is not None:
                self.Recognition = xmlData.find('Recognition').text
    
    class ReceiveVideoMsg(ReceiveMsg):
        '''视频消息和小视频消息'''
        def __init__(self,xmlData):
            super(ReceiveVideoMsg,self).__init__(xmlData)
            self.ThumbMediaId = xmlData.find('ThumbMediaId').text
            self.MediaId = xmlData.find('MediaId').text
    
    class ReceiveLocationMsg(ReceiveMsg):
        '''地理位置消息'''
        def __init__(self,xmlData):
            super(ReceiveLocationMsg,self).__init__(xmlData)
            self.Location_X = xmlData.find('Location_X').text
            self.Location_Y = xmlData.find('Location_Y').text
            self.Scale = xmlData.find('Scale').text
            self.Label = xmlData.find('Label').text
    
    class ReceiveLinkMsg(ReceiveMsg):
        '''链接消息'''
        def __init__(self,xmlData):
            super(ReceiveLinkMsg,self).__init__(xmlData)
            self.Title = xmlData.find('Title').text
            self.Description = xmlData.find('Description').text
            self.Url = xmlData.find('Url').text
    
    class ReceiveEventMsg(ReceiveMsg):
        '''普通事件'''
        def __init__(self, xmlData):
            super(ReceiveEventMsg,self).__init__(xmlData)
            self.Event = xmlData.find('Event').text
            self.EventKey = (False,'')
            if xmlData.find('EventKey') is not None:
                self.EventKey =(True, xmlData.find('EventKey').text)
    
            self.Ticket = (False,'')
            if xmlData.find('Ticket') is not None:
                self.EventKey =(True, xmlData.find('Ticket').text)
    
    class ReveiveLocationEventMsg(ReceiveEventMsg):
        '''上报地理位置事件'''
        def __init__(self,xmlData):
            super(ReveiveLocationEventMsg,self).__init__(xmlData)
            self.Latitude = xmlData.find('Latitude').text
            self.Longitude = xmlData.find('Longitude').text
            self.Precision = xmlData.find('Precision').text
  • 相关阅读:
    获取单选框的值
    HTML5本地存储详解
    设为首页和加入收藏
    用PhotoSwipe制作相册,手势可放大
    iOS 加载本地 HTML 文件 CSS 样式图片无效果
    PhotoSwipe简介
    Flexslider图片轮播、文字图片相结合滑动切换效果
    网页中插入视频的方法----腾讯、优酷为例
    webapp在Android中点击链接的时候会有淡蓝色的遮罩层
    C# 获取北京时间 (根据纪元时间(1970/1/1)转换为DateTime)
  • 原文地址:https://www.cnblogs.com/ahMay/p/12038125.html
Copyright © 2011-2022 走看看