5
5
import ConfigParser
6
6
import json
7
7
8
-
9
8
class TulingWXBot (WXBot ):
10
9
def __init__ (self ):
11
10
WXBot .__init__ (self )
12
11
13
12
self .tuling_key = ""
13
+ self .robot_switch = True
14
14
15
15
try :
16
16
cf = ConfigParser .ConfigParser ()
@@ -35,13 +35,35 @@ def tuling_auto_reply(self, uid, msg):
35
35
else :
36
36
result = respond ['text' ].replace ('<br>' , ' ' )
37
37
38
+ print ' ROBOT:' , result
38
39
return result
39
40
else :
40
41
return u"知道啦"
41
42
43
+ def button (self , msg ):
44
+ msg_data = msg ['content' ]['data' ]
45
+ STOP = [u'退下' ,u'走开' ,u'关闭' ,u'关掉' ,u'休息' ,u'滚开' ]
46
+ START = [u'出来' ,u'启动' ,u'工作' ]
47
+ if self .robot_switch :
48
+ for i in STOP :
49
+ if i in msg_data :
50
+ self .robot_switch = False
51
+ self .send_msg_by_uid (u'[Robot]' + u'机器人已关闭!' , msg ['to_user_id' ])
52
+ else :
53
+ for i in START :
54
+ if i in msg_data :
55
+ self .robot_switch = True
56
+
42
57
def handle_msg_all (self , msg ):
43
- if msg ['msg_type_id' ] == 4 and msg ['content' ]['type' ] == 0 : # text message from contact
44
- self .send_msg_by_uid (self .tuling_auto_reply (msg ['user' ]['id' ], msg ['content' ]['data' ]), msg ['user' ]['id' ])
58
+ #print 'MSG ID:', msg['msg_type_id']
59
+ if msg ['msg_type_id' ] == 1 and msg ['content' ]['type' ] == 0 : # replay to self
60
+ self .button (msg )
61
+ if self .robot_switch :
62
+ self .send_msg_by_uid (u'[Robot]' + self .tuling_auto_reply (msg ['user' ]['id' ], msg ['content' ]['data' ]), msg ['to_user_id' ])
63
+ elif msg ['msg_type_id' ] == 4 and msg ['content' ]['type' ] == 0 : # text message from contact
64
+ self .button (msg )
65
+ if self .robot_switch :
66
+ self .send_msg_by_uid (u'[Robot]' + self .tuling_auto_reply (msg ['user' ]['id' ], msg ['content' ]['data' ]), msg ['user' ]['id' ])
45
67
elif msg ['msg_type_id' ] == 3 : # group message
46
68
if msg ['content' ]['data' ].find ('@' ) >= 0 : # someone @ another
47
69
my_names = self .get_group_member_name (msg ['user' ]['id' ], self .user ['UserName' ])
@@ -86,8 +108,10 @@ def main():
86
108
bot = TulingWXBot ()
87
109
bot .DEBUG = True
88
110
bot .conf ['qr' ] = 'png'
111
+
89
112
bot .run ()
90
113
91
114
92
115
if __name__ == '__main__' :
93
116
main ()
117
+
0 commit comments