newYearBot.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. from wxbot import *
  4. import ConfigParser
  5. import json
  6. import logging
  7. import logging.config
  8. class TulingWXBot(WXBot):
  9. def __init__(self):
  10. WXBot.__init__( )
  11. self.tuling_key = ""
  12. self.
  13. self.robot_switch = True
  14. try:
  15. cf = ConfigParser.ConfigParser()
  16. cf.read('conf/conf.ini')
  17. self.qrtype = cf.get('qrtype', 'key')
  18. except Exception:
  19. pass
  20. logging.info('qrtype:'+self.tuling_key)
  21. # 自动回复函数self:发送方用户id, uid, msg
  22. def auto_reply(self, uid, msg):
  23. return u"新年快乐,我是天天,主人马上就来!"
  24. def auto_switch(self, msg):
  25. msg_data = msg['content']['data']
  26. stop_cmd = [u'退下', u'走开', u'关闭', u'关掉', u'休息', u'滚开']
  27. start_cmd = [u'出来', u'启动', u'工作']
  28. if self.robot_switch:
  29. for i in stop_cmd:
  30. if i == msg_data:
  31. self.robot_switch = False
  32. self.send_msg_by_uid(u'[Robot]' + u'机器人已关闭!', msg['to_user_id'])
  33. else:
  34. for i in start_cmd:
  35. if i == msg_data:
  36. self.robot_switch = True
  37. self.send_msg_by_uid(u'[Robot]' + u'机器人已开启!', msg['to_user_id'])
  38. # 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'}}
  39. def handle_msg_all(self, msg):
  40. if not self.robot_switch and msg['msg_type_id'] != 1:
  41. return
  42. if msg['msg_type_id'] == 1 and msg['content']['type'] == 0: # reply to self
  43. self.auto_switch(msg)
  44. # msg_type_id=4 发送文字
  45. elif msg['msg_type_id'] == 4 and msg['content']['type'] == 0: # text message from contact
  46. self.send_msg_by_uid(self.tuling_auto_reply(msg['user']['id'], msg['content']['data']), msg['user']['id'])
  47. elif msg['msg_type_id'] == 3 and msg['content']['type'] == 0: # group text message
  48. if 'detail' in msg['content']:
  49. my_names = self.get_group_member_name(self.my_account['UserName'], msg['user']['id'])
  50. if my_names is None:
  51. my_names = {}
  52. if 'NickName' in self.my_account and self.my_account['NickName']:
  53. my_names['nickname2'] = self.my_account['NickName']
  54. if 'RemarkName' in self.my_account and self.my_account['RemarkName']:
  55. my_names['remark_name2'] = self.my_account['RemarkName']
  56. is_at_me = False
  57. for detail in msg['content']['detail']:
  58. if detail['type'] == 'at':
  59. for k in my_names:
  60. if my_names[k] and my_names[k] == detail['value']:
  61. is_at_me = True
  62. break
  63. if is_at_me:
  64. src_name = msg['content']['user']['name']
  65. reply = 'to ' + src_name + ': '
  66. if msg['content']['type'] == 0: # text message
  67. reply += self.tuling_auto_reply(msg['content']['user']['id'], msg['content']['desc'])
  68. else:
  69. reply += u"对不起,只认字,其他杂七杂八的我都不认识,,,Ծ‸Ծ,,"
  70. self.send_msg_by_uid(reply, msg['user']['id'])
  71. else:
  72. src_name = msg['user']['name']
  73. reply = 'to ' + src_name + ': '
  74. reply += u"主人马上就来!"
  75. #self.send_msg_by_uid(reply, msg['user']['id'])
  76. '''
  77. 主函数,执行TulingWXBot类里面的run方法
  78. '''
  79. def main():
  80. logging.config.fileConfig("conf/logger.conf")
  81. logger=logging.getLogger("example01")
  82. bot = TulingWXBot()
  83. bot.DEBUG = True
  84. bot.conf['qr'] = bot.qrtype
  85. bot.run()
  86. if __name__ == '__main__':
  87. main()