forked from doraemonext/wechat-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.py
129 lines (105 loc) · 4.12 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
# -*- coding: utf-8 -*-
# From: https://github.com/whtsky/WeRoBot/blob/develop/werobot/messages.py
from .exceptions import ParseError
MESSAGE_TYPES = {}
def handle_for_type(type):
def register(f):
MESSAGE_TYPES[type] = f
return f
return register
class WechatMessage(object):
def __init__(self, message):
self.id = int(message.pop('MsgId', 0))
self.target = message.pop('ToUserName', None)
self.source = message.pop('FromUserName', None)
self.time = int(message.pop('CreateTime', 0))
self.__dict__.update(message)
@handle_for_type('text')
class TextMessage(WechatMessage):
def __init__(self, message):
self.content = message.pop('Content', '')
super(TextMessage, self).__init__(message)
@handle_for_type('image')
class ImageMessage(WechatMessage):
def __init__(self, message):
try:
self.picurl = message.pop('PicUrl')
self.media_id = message.pop('MediaId')
except KeyError:
raise ParseError()
super(ImageMessage, self).__init__(message)
@handle_for_type('video')
class VideoMessage(WechatMessage):
def __init__(self, message):
try:
self.media_id = message.pop('MediaId')
self.thumb_media_id = message.pop('ThumbMediaId')
except KeyError:
raise ParseError()
super(VideoMessage, self).__init__(message)
@handle_for_type('shortvideo')
class ShortVideoMessage(WechatMessage):
def __init__(self, message):
try:
self.media_id = message.pop('MediaId')
self.thumb_media_id = message.pop('ThumbMediaId')
except KeyError:
raise ParseError()
super(ShortVideoMessage, self).__init__(message)
@handle_for_type('location')
class LocationMessage(WechatMessage):
def __init__(self, message):
try:
location_x = message.pop('Location_X')
location_y = message.pop('Location_Y')
self.location = (float(location_x), float(location_y))
self.scale = int(message.pop('Scale'))
self.label = message.pop('Label')
except KeyError:
raise ParseError()
super(LocationMessage, self).__init__(message)
@handle_for_type('link')
class LinkMessage(WechatMessage):
def __init__(self, message):
try:
self.title = message.pop('Title')
self.description = message.pop('Description')
self.url = message.pop('Url')
except KeyError:
raise ParseError()
super(LinkMessage, self).__init__(message)
@handle_for_type('event')
class EventMessage(WechatMessage):
def __init__(self, message):
message.pop('type')
try:
self.type = message.pop('Event').lower()
if self.type == 'subscribe' or self.type == 'scan':
self.key = message.pop('EventKey', None)
self.ticket = message.pop('Ticket', None)
elif self.type in ['click', 'view', 'scancode_push', 'scancode_waitmsg',
'pic_sysphoto', 'pic_photo_or_album', 'pic_weixin', 'location_select']:
self.key = message.pop('EventKey')
elif self.type == 'location':
self.latitude = float(message.pop('Latitude'))
self.longitude = float(message.pop('Longitude'))
self.precision = float(message.pop('Precision'))
elif self.type == 'templatesendjobfinish':
self.status = message.pop('Status')
except KeyError:
raise ParseError()
super(EventMessage, self).__init__(message)
@handle_for_type('voice')
class VoiceMessage(WechatMessage):
def __init__(self, message):
try:
self.media_id = message.pop('MediaId')
self.format = message.pop('Format')
self.recognition = message.pop('Recognition', None)
except KeyError:
raise ParseError()
super(VoiceMessage, self).__init__(message)
class UnknownMessage(WechatMessage):
def __init__(self, message):
self.type = 'unknown'
super(UnknownMessage, self).__init__(message)