newYearBot.py 4.1 KB

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