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