55import ConfigParser
66import json
77
8-
98class TulingWXBot (WXBot ):
109 def __init__ (self ):
1110 WXBot .__init__ (self )
1211
1312 self .tuling_key = ""
13+ self .robot_switch = True
1414
1515 try :
1616 cf = ConfigParser .ConfigParser ()
@@ -35,13 +35,35 @@ def tuling_auto_reply(self, uid, msg):
3535 else :
3636 result = respond ['text' ].replace ('<br>' , ' ' )
3737
38+ print ' ROBOT:' , result
3839 return result
3940 else :
4041 return u"知道啦"
4142
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+
4257 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' ])
4567 elif msg ['msg_type_id' ] == 3 : # group message
4668 if msg ['content' ]['data' ].find ('@' ) >= 0 : # someone @ another
4769 my_names = self .get_group_member_name (msg ['user' ]['id' ], self .user ['UserName' ])
@@ -86,8 +108,10 @@ def main():
86108 bot = TulingWXBot ()
87109 bot .DEBUG = True
88110 bot .conf ['qr' ] = 'png'
111+
89112 bot .run ()
90113
91114
92115if __name__ == '__main__' :
93116 main ()
117+
0 commit comments