Skip to content

Commit 6a7fe6b

Browse files
committed
make it reply to self
1 parent 2db6f21 commit 6a7fe6b

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

bot.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import ConfigParser
66
import json
77

8-
98
class 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

92115
if __name__ == '__main__':
93116
main()
117+

wxbot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ def handle_msg(self, r):
422422
message = {'msg_type_id': msg_type_id,
423423
'msg_id': msg['MsgId'],
424424
'content': content,
425+
'to_user_id': msg['ToUserName'],
425426
'user': user}
426427
self.handle_msg_all(message)
427428

0 commit comments

Comments
 (0)