|
@@ -0,0 +1,99 @@
|
|
|
+#!/usr/bin/env python
|
|
|
+# coding: utf-8
|
|
|
+
|
|
|
+from wxbot import *
|
|
|
+import ConfigParser
|
|
|
+import json
|
|
|
+import logging
|
|
|
+import logging.config
|
|
|
+
|
|
|
+class TulingWXBot(WXBot):
|
|
|
+ def __init__(self):
|
|
|
+ WXBot.__init__( )
|
|
|
+
|
|
|
+ self.tuling_key = ""
|
|
|
+ self.
|
|
|
+ self.robot_switch = True
|
|
|
+
|
|
|
+ try:
|
|
|
+ cf = ConfigParser.ConfigParser()
|
|
|
+ cf.read('conf/conf.ini')
|
|
|
+ self.qrtype = cf.get('qrtype', 'key')
|
|
|
+ except Exception:
|
|
|
+ pass
|
|
|
+ logging.info('qrtype:'+self.tuling_key)
|
|
|
+
|
|
|
+# 自动回复函数self:发送方用户id, uid, msg
|
|
|
+ def auto_reply(self, uid, msg):
|
|
|
+ return u"新年快乐,我是天天,主人马上就来!"
|
|
|
+
|
|
|
+ def auto_switch(self, msg):
|
|
|
+ msg_data = msg['content']['data']
|
|
|
+ stop_cmd = [u'退下', u'走开', u'关闭', u'关掉', u'休息', u'滚开']
|
|
|
+ start_cmd = [u'出来', u'启动', u'工作']
|
|
|
+ if self.robot_switch:
|
|
|
+ for i in stop_cmd:
|
|
|
+ if i == msg_data:
|
|
|
+ self.robot_switch = False
|
|
|
+ self.send_msg_by_uid(u'[Robot]' + u'机器人已关闭!', msg['to_user_id'])
|
|
|
+ else:
|
|
|
+ for i in start_cmd:
|
|
|
+ if i == msg_data:
|
|
|
+ self.robot_switch = True
|
|
|
+ self.send_msg_by_uid(u'[Robot]' + u'机器人已开启!', msg['to_user_id'])
|
|
|
+# msg dict: {'content': {'data': u'\u53bb', 'type': 0}, 'msg_id': u'3808887025959048251', 'msg_type_id': 4, 'to_user_id': u'@97745d23f2d3bbe5241cede14a3d8baa45809a1842e762b5a33fc1353f6efa3a', 'user': {'id': u'@f95a572b1284a0f75dd550a4ed9a93d788ded42a856db023cd3494faab623311', 'name': u'\u5929\u95ee'}}
|
|
|
+ def handle_msg_all(self, msg):
|
|
|
+ if not self.robot_switch and msg['msg_type_id'] != 1:
|
|
|
+ return
|
|
|
+ if msg['msg_type_id'] == 1 and msg['content']['type'] == 0: # reply to self
|
|
|
+ self.auto_switch(msg)
|
|
|
+ # msg_type_id=4 发送文字
|
|
|
+ elif msg['msg_type_id'] == 4 and msg['content']['type'] == 0: # text message from contact
|
|
|
+ self.send_msg_by_uid(self.tuling_auto_reply(msg['user']['id'], msg['content']['data']), msg['user']['id'])
|
|
|
+ elif msg['msg_type_id'] == 3 and msg['content']['type'] == 0: # group text message
|
|
|
+ if 'detail' in msg['content']:
|
|
|
+ my_names = self.get_group_member_name(self.my_account['UserName'], msg['user']['id'])
|
|
|
+ if my_names is None:
|
|
|
+ my_names = {}
|
|
|
+ if 'NickName' in self.my_account and self.my_account['NickName']:
|
|
|
+ my_names['nickname2'] = self.my_account['NickName']
|
|
|
+ if 'RemarkName' in self.my_account and self.my_account['RemarkName']:
|
|
|
+ my_names['remark_name2'] = self.my_account['RemarkName']
|
|
|
+
|
|
|
+ is_at_me = False
|
|
|
+ for detail in msg['content']['detail']:
|
|
|
+ if detail['type'] == 'at':
|
|
|
+ for k in my_names:
|
|
|
+ if my_names[k] and my_names[k] == detail['value']:
|
|
|
+ is_at_me = True
|
|
|
+ break
|
|
|
+ if is_at_me:
|
|
|
+ src_name = msg['content']['user']['name']
|
|
|
+ reply = 'to ' + src_name + ': '
|
|
|
+ if msg['content']['type'] == 0: # text message
|
|
|
+ reply += self.tuling_auto_reply(msg['content']['user']['id'], msg['content']['desc'])
|
|
|
+ else:
|
|
|
+ reply += u"对不起,只认字,其他杂七杂八的我都不认识,,,Ծ‸Ծ,,"
|
|
|
+ self.send_msg_by_uid(reply, msg['user']['id'])
|
|
|
+ else:
|
|
|
+ src_name = msg['user']['name']
|
|
|
+ reply = 'to ' + src_name + ': '
|
|
|
+ reply += u"主人马上就来!"
|
|
|
+ #self.send_msg_by_uid(reply, msg['user']['id'])
|
|
|
+
|
|
|
+'''
|
|
|
+主函数,执行TulingWXBot类里面的run方法
|
|
|
+'''
|
|
|
+def main():
|
|
|
+ logging.config.fileConfig("conf/logger.conf")
|
|
|
+ logger=logging.getLogger("example01")
|
|
|
+ bot = TulingWXBot()
|
|
|
+ bot.DEBUG = True
|
|
|
+ bot.conf['qr'] = bot.qrtype
|
|
|
+
|
|
|
+ bot.run()
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ main()
|
|
|
+
|