django+python微信开发之二-封装消息类

###消息的类型
微信开发中有三种消息类型,参考书籍:《微信公众平台应用开发:方法、技巧与案例》

  • 请求消息,指用户发送给公众账号的消息,然后由微信公众账号平台post给你的sae服务器,包括文本消息、图片消息、语音消息、视频消息、地理位置消息和链接消息。
  • 事件,是指用户对公众账号做出某种操作时,微信服务器会将对应的事件推送给公众账号服务器。五类事件分别是关注公众账号、取消关注公众账号、扫描带参数二维码、同意上报地理位置并进入公众账号和点击自定义菜单,这5种操作所对应的事件分别为关注事件、取消关注事件、扫描带参数二维码事件、上报地理位置事件和自定义菜单事件。
  • 响应消息,响应消息是指公众账号回复给用户的消息,响应消息包括文本消息、图片消息、语音消息、视频消息、音乐消息和图文消息。 流程如下:
    用户(发送消息)–>微信公众平台账号(发送post请求或事件)–>你的sae django服务器(处理请求并且返回响应消息)–>微信公众平台账号(处理)–>用户接收

###封装请求消息类wechatMessage.py

# -*- coding:utf-8 -*-
"""
# Author: Pegasus Wang ([email protected], http://ningning.today)
# Created Time : Wed Feb 18 18:18:10 2015

# File Name: wechatMessage.py
# Description:

# :copyright: (c) 2015 by Pegasus Wang.
# :license: MIT, see LICENSE for more details.
"""


class BaseMessage(object):
    """base message class"""
    def __init__(self):
        self.ToUserName = u''
        self.FromUserName = u''
        self.CreateTime = 0L
        self.MsgType = u''
        self.MsgId = 0L

    def getToUserName(self):
        return self.ToUserName

    def setToUserName(self, toUserName):
        self.ToUserName = toUserName

    def getFromUserName(self):
        return self.FromUserName

    def setFromUserName(self, fromUserName):
        self.FromUserName = fromUserName

    def getCreateTime(self):
        return self.CreateTime

    def setCreateTime(self, createTime):
        self.CreateTime = createTime

    def getMsgType(self):
        return self.MsgType

    def setMsgType(self, msgType):
        self.MsgType = msgType

    def getMsgId(self):
        return self.MsgId

    def setMsgId(self, msgId):
        self.MsgId = msgId


class TextMessage(BaseMessage):
    """text message class"""
    def __init__(self):
        super(TextMessage, self).__init__()
        self.Content = u''

    def getContent(self):
        return self.Content

    def setContent(self, content):
        self.Content = content


class ImageMessage(BaseMessage):
    """image message class"""
    def __init__(self):
        super(ImageMessage, self).__init__()
        self.PicUrl = u''

    def getPicUrl(self):
        return self.PicUrl

    def setPicUrl(self, picUrl):
        self.PicUrl = picUrl


class VoiceMessage(BaseMessage):
    """voice message class"""
    def __init__(self):
        super(VoiceMessage, self).__init__()
        self.MediaId = u''
        self.Format = u''
        self.Recognition = u''

    def getMediaId(self):
        return self.MediaId

    def setMediaId(self, mediaId):
        self.MediaId = mediaId

    def getFormat(self):
        return self.Format

    def setFormat(self, format):
        self.Format = format

    def getRecognition(self):
        return self.Recognition

    def setRecognition(self, recognition):
        self.Recognition = recognition


class VideoMessage(BaseMessage):
    """video message class"""
    def __init__(self):
        super(VideoMessage, self).__init__()
        self.MediaId = u''
        self.ThumbMediaId = u''

    def getMediaId(self):
        return self.MediaId

    def setMediaId(self, mediaId):
        self.MediaId = mediaId

    def getThumbMediaId(self):
        return self.ThumbMediaId

    def setThumbMediaId(self, thumbMediaId):
        self.ThumbMediaId = thumbMediaId


class LocationMessage(BaseMessage):
    """location message class"""
    def __init__(self):
        super(LocationMessage, self).__init__()
        self.Location_X = u''
        self.Location_Y = u''
        self.Scale = u''
        self.Label = u''

    def getLocation_X(self):
        return self.Location_X

    def setLocation_X(self, location_X):
        self.Location_X = location_X

    def getLocation_Y(self):
        return self.Location_Y

    def setLocation_Y(self, location_Y):
        self.Location_Y = location_Y

    def getScale(self):
        return self.Scale

    def setScale(self, scale):
        self.Scale = scale

    def getLabel(self):
        return self.Label

    def setLabel(self, label):
        self.Label = label


class LinkMessage(BaseMessage):
    """link message class"""
    def __init__(self):
        super(LinkMessage, self).__init__()
        self.Title = u''
        self.Description = u''
        self.Url = u''

    def getTitle(self):
        return self.Title

    def setTitle(self, title):
        self.Title = title

    def getDescription(self):
        return self.Description

    def setDescription(self, description):
        self.Description = description

    def getUrl(self):
        return self.Url

    def setUrl(self, url):
        self.Url = url

这里有一个BaseMessage作为消息基类,包含了所有消息类型共有的成员,不同的消息类型作为子类,添加自己的成员,代码也很简单。


###封装事件类wechatEvent.py

# -*- coding:utf-8 -*-
"""
# Author: Pegasus Wang ([email protected], http://ningning.today)
# Created Time : Wed Feb 18 18:29:06 2015

# File Name: wechatEvent.py
# Description:

# :copyright: (c) 2015 by Pegasus Wang.
# :license: MIT, see LICENSE for more details.
"""


class BaseEvent(object):
    """base event class"""
    def __init__(self):
        self.ToUserName = u''
        self.FromUserName = u''
        self.CreateTime = 0L
        self.MsgType = u''
        self.Event = u''

    def getToUserName(self):
        return self.ToUserName

    def setToUserName(self, toUserName):
        self.ToUserName = toUserName

    def getFromUserName(self):
        return self.FromUserName

    def setFromUserName(self, fromUserName):
        self.FromUserName = fromUserName

    def getCreateTime(self):
        return self.CreateTime

    def setCreateTime(self, createTime):
        self.CreateTime = createTime

    def getMsgType(self):
        return self.MsgType

    def setMsgType(self, msgType):
        self.MsgType = msgType

    def getEvent(self):
        return self.Event

    def setMsgId(self, event):
        self.Event = event


class SubscribeEvent(BaseEvent):
    """subscribe event class"""
    def __init__(self):
        super(SubscribeEvent, self).__init__()


class QRCodeEvent(BaseEvent):
    """scan QR code event class"""
    def __init__(self):
        super(QRCodeEvent, self).__init__()
        self.EventKey = u''
        self.Ticket = u''

    def getEventKey(self):
        return self.EventKey

    def setEventKey(self, eventKey):
        self.EventKey = eventKey

    def getTicket(self):
        return self.Ticket

    def setTicket(self, ticket):
        self.Ticket = ticket


class LocationEvent(BaseEvent):
    """upload location event class"""
    def __init__(self):
        super(LocationEvent, self).__init__()
        self.Latitude = u''
        self.Longitude = u''
        self.Precision = u''

    def getLatitude(self):
        return self.Latitude

    def setLatitude(self, latitude):
        self.Latitude = latitude

    def getLongitude(self):
        return self.Longitude

    def setLongitude(self, longtitude):
        self.Longitude = longtitude

    def getPrecision(self):
        return self.Precision

    def setPrecision(self, precision):
        self.Precision = precision


class MenuEvent(BaseEvent):
    """customize menu class"""
    def __init__(self):
        super(MenuEvent, self).__init__()
        self.EventKey = u''

    def getEventKey(self):
        return self.EventKey

    def setEventKey(self, eventKey):
        self.EventKey = eventKey

一个事件基类,子类是不同的事件类型。


###封装响应消息类wechatReply.py

# -*- coding:utf-8 -*-
"""
# Author: Pegasus Wang ([email protected], http://ningning.today)
# Created Time : Wed Feb 18 18:58:39 2015

# File Name: wechatReply.py
# Description:

# :copyright: (c) 2015 by Pegasus Wang.
# :license: MIT, see LICENSE for more details.
"""


class BaseReply(object):
    """base reply message class"""
    def __init__(self):
        self.ToUserName = u''
        self.FromUserName = u''
        self.CreateTime = 0L
        self.MsgType = u''

    def getToUserName(self):
        return self.ToUserName

    def setToUserName(self, toUserName):
        self.ToUserName = toUserName

    def getFromUserName(self):
        return self.FromUserName

    def setFromUserName(self, fromUserName):
        self.FromUserName = fromUserName

    def getCreateTime(self):
        return self.CreateTime

    def setCreateTime(self, createTime):
        self.CreateTime = createTime

    def getMsgType(self):
        return self.MsgType

    def setMsgType(self, msgType):
        self.MsgType = msgType


class TextReply(BaseReply):
    """text reply message class"""
    def __init__(self):
        super(TextReply, self).__init__()
        self.Content = u''

    def getContent(self):
        return self.Content

    def setContent(self, content):
        self.Content = content


class Image(object):
    """image class"""
    def __init__(self):
        self.MediaId = u''

    def getMediaId(self):
        return self.MediaId

    def setMediaId(self, mediaId):
        self.MediaId = mediaId


class ImageReply(BaseReply):
    """image reply message class"""
    def __init__(self):
        super(ImageReply, self).__init__()
        self.Image = Image()

    def getImage(self):
        return self.Image

    def setImage(self, image):
        self.Image = image


class Voice(object):
    """voice class"""
    def __init__(self):
        self.MediaId = u''

    def getMediaId(self):
        return self.MediaId

    def setMediaId(self, mediaId):
        self.MediaId = mediaId


class VoiceReply(BaseReply):
    """voice reply message class"""
    def __init__(self):
        super(VoiceReply, self).__init__()
        self.Voice = Voice()

    def getVoice(self):
        return self.Voice

    def setVoice(self, voice):
        self.Voice = voice


class Video(object):
    """video class"""
    def __init__(self):
        self.MediaId = u''
        self.ThumbMediaId = u''

    def getMediaId(self):
        return self.MediaId

    def setMediaId(self, mediaId):
        self.MediaId = mediaId

    def getThumbMediaId(self):
        return self.ThumbMediaId

    def setThumbMediaId(self, thumbMediaId):
        self.ThumbMediaId = thumbMediaId


class VideoReply(BaseReply):
    """video reply messag class"""
    def __init__(self):
        super(VideoReply, self).__init__()
        self.Video = Video()

    def getVideo(self):
        return self.Video

    def setVideo(self, video):
        self.Video = video


class Music(object):
    """music class"""
    def __init__(self):
        self.Title = u''
        self.Description = u''
        self.MusicUrl = u''
        self.HQMusicUrl = u''
        self.ThumbMediaId = u''

    def getTitle(self):
        return self.Title

    def setTitle(self, title):
        self.Title = title

    def getDescription(self):
        return self.Description

    def setDescription(self, description):
        self.Description = description

    def getMusicUrl(self):
        return self.MusicUrl

    def setMusicUrl(self, musicUrl):
        self.MusicUrl = musicUrl

    def getHQMusicUrl(self):
        return self.HQMusicUrl

    def setHQMusicUrl(self, musicUrl):
        self.HQMusicUrl = musicUrl

    def getThumbMediaId(self):
        return self.ThumbMediaId

    def setThumbMediaId(self, thumbMediaId):
        self.ThumbMediaId = thumbMediaId


class MusicReply(BaseReply):
    """music reply message class"""
    def __init__(self):
        super(MusicReply, self).__init__()
        self.Music = Music()

    def getMusic(self):
        return self.Music

    def setMusic(self, music):
        self.Music = music


class Article(object):
    """article class"""
    def __init__(self):
        self.Title = u''
        self.Description = u''
        self.PicUrl = u''
        self.Url = u''

    def getTitle(self):
        return self.Title

    def setTitle(self, title):
        self.Title = title

    def getDescription(self):
        return self.Description

    def setDescription(self, description):
        self.Description = description

    def getPicUrl(self):
        return self.PicUrl

    def setPicUrl(self, picUrl):
        self.PicUrl = picUrl

    def getUrl(self):
        return self.Url

    def setUrl(self, url):
        self.Url = url


class NewsReply(BaseReply):
    """news(image and text) reply message class"""
    def __init__(self):
        super(NewsReply, self).__init__()
        self.ArticleCount = 0
        self.Articles = []

    def getArticleCount(self):
        return self.ArticleCount

    def setArticleCount(self, articleCount):
        self.ArticleCount = articleCount

    def getArticles(self):
        return self.Articles

    def setArticles(self, articles):
        self.Articles = articles

这里也有一个响应消息基类,子类是各种消息类型。参考微信开发文档:被动回复用户消息


消息类型封装就完成了,代码很简单,消息格式需要参考下微信开发文档来定义适当的类。接下来是编写消息处理类。