-
Notifications
You must be signed in to change notification settings - Fork 342
/
messages.py
173 lines (140 loc) · 4.17 KB
/
messages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# -*- coding: utf-8 -*-
"""
wechatpy.messages
~~~~~~~~~~~~~~~~~~
This module defines all the messages you can get from WeChat server
:copyright: (c) 2014 by messense.
:license: MIT, see LICENSE for more details.
"""
from __future__ import absolute_import, unicode_literals
import copy
import six
from wechatpy.fields import (
BaseField,
StringField,
IntegerField,
DateTimeField,
FieldDescriptor
)
from wechatpy.utils import to_text, to_binary
MESSAGE_TYPES = {}
def register_message(msg_type):
def register(cls):
MESSAGE_TYPES[msg_type] = cls
return cls
return register
class MessageMetaClass(type):
"""Metaclass for all messages"""
def __new__(cls, name, bases, attrs):
for b in bases:
if not hasattr(b, '_fields'):
continue
for k, v in b.__dict__.items():
if k in attrs:
continue
if isinstance(v, FieldDescriptor):
attrs[k] = copy.deepcopy(v.field)
cls = super(MessageMetaClass, cls).__new__(cls, name, bases, attrs)
cls._fields = {}
for name, field in cls.__dict__.items():
if isinstance(field, BaseField):
field.add_to_class(cls, name)
return cls
class BaseMessage(six.with_metaclass(MessageMetaClass)):
"""Base class for all messages and events"""
type = 'unknown'
id = IntegerField('MsgId', 0)
source = StringField('FromUserName')
target = StringField('ToUserName')
create_time = DateTimeField('CreateTime')
time = IntegerField('CreateTime')
def __init__(self, message):
self._data = 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)
@register_message('text')
class TextMessage(BaseMessage):
"""
文本消息
详情请参阅
http://mp.weixin.qq.com/wiki/10/79502792eef98d6e0c6e1739da387346.html
"""
type = 'text'
content = StringField('Content')
@register_message('image')
class ImageMessage(BaseMessage):
"""
图片消息
详情请参阅
http://mp.weixin.qq.com/wiki/10/79502792eef98d6e0c6e1739da387346.html
"""
type = 'image'
media_id = StringField('MediaId')
image = StringField('PicUrl')
@register_message('voice')
class VoiceMessage(BaseMessage):
"""
语音消息
详情请参阅
http://mp.weixin.qq.com/wiki/10/79502792eef98d6e0c6e1739da387346.html
"""
type = 'voice'
media_id = StringField('MediaId')
format = StringField('Format')
recognition = StringField('Recognition')
@register_message('shortvideo')
class ShortVideoMessage(BaseMessage):
"""
短视频消息
详情请参阅
http://mp.weixin.qq.com/wiki/10/79502792eef98d6e0c6e1739da387346.html
"""
type = 'shortvideo'
media_id = StringField('MediaId')
thumb_media_id = StringField('ThumbMediaId')
@register_message('video')
class VideoMessage(BaseMessage):
"""
视频消息
详情请参阅
http://mp.weixin.qq.com/wiki/10/79502792eef98d6e0c6e1739da387346.html
"""
type = 'video'
media_id = StringField('MediaId')
thumb_media_id = StringField('ThumbMediaId')
@register_message('location')
class LocationMessage(BaseMessage):
"""
地理位置消息
详情请参阅
http://mp.weixin.qq.com/wiki/10/79502792eef98d6e0c6e1739da387346.html
"""
type = 'location'
location_x = StringField('Location_X')
location_y = StringField('Location_Y')
scale = StringField('Scale')
label = StringField('Label')
@property
def location(self):
return self.location_x, self.location_y
@register_message('link')
class LinkMessage(BaseMessage):
"""
链接消息
详情请参阅
http://mp.weixin.qq.com/wiki/10/79502792eef98d6e0c6e1739da387346.html
"""
type = 'link'
title = StringField('Title')
description = StringField('Description')
url = StringField('Url')
class UnknownMessage(BaseMessage):
"""未知消息类型"""
pass