bot.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. from wxbot import *
  4. import ConfigParser
  5. import json
  6. class TulingWXBot(WXBot):
  7. def __init__(self):
  8. WXBot.__init__(self)
  9. self.tuling_key = ""
  10. self.robot_switch = True
  11. try:
  12. cf = ConfigParser.ConfigParser()
  13. cf.read('conf.ini')
  14. self.tuling_key = cf.get('main', 'key')
  15. except Exception:
  16. pass
  17. print 'tuling_key:', self.tuling_key
  18. # 自动回复函数self:发送方用户id, uid, msg
  19. def tuling_auto_reply(self, uid, msg):
  20. if self.tuling_key:
  21. url = "http://www.tuling123.com/openapi/api"
  22. user_id = uid.replace('@', '')[:30]
  23. body = {'key': self.tuling_key, 'info': msg.encode('utf8'), 'userid': user_id}
  24. r = requests.post(url, data=body)
  25. respond = json.loads(r.text)
  26. result = ''
  27. if respond['code'] == 100000:
  28. result = respond['text'].replace('<br>', ' ')
  29. elif respond['code'] == 200000:
  30. result = respond['url']
  31. elif respond['code'] == 302000:
  32. for k in respond['list']:
  33. result = result + u"【" + k['source'] + u"】 " + \
  34. k['article'] + "\t" + k['detailurl'] + "\n"
  35. else:
  36. result = respond['text'].replace('<br>', ' ')
  37. print ' ROBOT:', result
  38. return result
  39. else:
  40. return u"知道啦,我是天问机器人,主人马上就来!"
  41. def auto_switch(self, msg):
  42. msg_data = msg['content']['data']
  43. stop_cmd = [u'退下', u'走开', u'关闭', u'关掉', u'休息', u'滚开']
  44. start_cmd = [u'出来', u'启动', u'工作']
  45. if self.robot_switch:
  46. for i in stop_cmd:
  47. if i == msg_data:
  48. self.robot_switch = False
  49. self.send_msg_by_uid(u'[Robot]' + u'机器人已关闭!', msg['to_user_id'])
  50. else:
  51. for i in start_cmd:
  52. if i == msg_data:
  53. self.robot_switch = True
  54. self.send_msg_by_uid(u'[Robot]' + u'机器人已开启!', msg['to_user_id'])
  55. # 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'}}
  56. def handle_msg_all(self, msg):
  57. if not self.robot_switch and msg['msg_type_id'] != 1:
  58. return
  59. if msg['msg_type_id'] == 1 and msg['content']['type'] == 0: # reply to self
  60. self.auto_switch(msg)
  61. # msg_type_id=4 发送文字
  62. elif msg['msg_type_id'] == 4 and msg['content']['type'] == 0: # text message from contact
  63. self.send_msg_by_uid(self.tuling_auto_reply(msg['user']['id'], msg['content']['data']), msg['user']['id'])
  64. elif msg['msg_type_id'] == 3 and msg['content']['type'] == 0: # group text message
  65. if 'detail' in msg['content']:
  66. my_names = self.get_group_member_name(self.my_account['UserName'], msg['user']['id'])
  67. if my_names is None:
  68. my_names = {}
  69. if 'NickName' in self.my_account and self.my_account['NickName']:
  70. my_names['nickname2'] = self.my_account['NickName']
  71. if 'RemarkName' in self.my_account and self.my_account['RemarkName']:
  72. my_names['remark_name2'] = self.my_account['RemarkName']
  73. is_at_me = False
  74. for detail in msg['content']['detail']:
  75. if detail['type'] == 'at':
  76. for k in my_names:
  77. if my_names[k] and my_names[k] == detail['value']:
  78. is_at_me = True
  79. break
  80. if is_at_me:
  81. src_name = msg['content']['user']['name']
  82. reply = 'to ' + src_name + ': '
  83. if msg['content']['type'] == 0: # text message
  84. reply += self.tuling_auto_reply(msg['content']['user']['id'], msg['content']['desc'])
  85. else:
  86. reply += u"对不起,只认字,其他杂七杂八的我都不认识,,,Ծ‸Ծ,,"
  87. self.send_msg_by_uid(reply, msg['user']['id'])
  88. else:
  89. src_name = msg['content']['user']['name']
  90. reply = 'to ' + src_name + ': '
  91. reply += u"对不起,只认字,其他杂七杂八的我都不认识,,,Ծ‸Ծ,,"
  92. self.send_msg_by_uid(reply, msg['user']['id'])
  93. '''
  94. 主函数,执行TulingWXBot类里面的run方法
  95. '''
  96. def main():
  97. bot = TulingWXBot()
  98. bot.DEBUG = True
  99. bot.conf['qr'] = 'png'
  100. bot.run()
  101. if __name__ == '__main__':
  102. main()