bot.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. def tuling_auto_reply(self, uid, msg):
  19. if self.tuling_key:
  20. url = "http://www.tuling123.com/openapi/api"
  21. user_id = uid.replace('@', '')[:30]
  22. body = {'key': self.tuling_key, 'info': msg.encode('utf8'), 'userid': user_id}
  23. r = requests.post(url, data=body)
  24. respond = json.loads(r.text)
  25. result = ''
  26. if respond['code'] == 100000:
  27. result = respond['text'].replace('<br>', ' ')
  28. result = result.replace(u'\xa0', u' ')
  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. result = result.replace(u'\xa0', u' ')
  38. print ' ROBOT:', result
  39. return result
  40. else:
  41. return u"知道啦"
  42. def auto_switch(self, msg):
  43. msg_data = msg['content']['data']
  44. stop_cmd = [u'退下', u'走开', u'关闭', u'关掉', u'休息', u'滚开']
  45. start_cmd = [u'出来', u'启动', u'工作']
  46. if self.robot_switch:
  47. for i in stop_cmd:
  48. if i == msg_data:
  49. self.robot_switch = False
  50. self.send_msg_by_uid(u'[Robot]' + u'机器人已关闭!', msg['to_user_id'])
  51. else:
  52. for i in start_cmd:
  53. if i == msg_data:
  54. self.robot_switch = True
  55. self.send_msg_by_uid(u'[Robot]' + u'机器人已开启!', msg['to_user_id'])
  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. elif msg['msg_type_id'] == 4 and msg['content']['type'] == 0: # text message from contact
  62. self.send_msg_by_uid(self.tuling_auto_reply(msg['user']['id'], msg['content']['data']), msg['user']['id'])
  63. elif msg['msg_type_id'] == 3 and msg['content']['type'] == 0: # group text message
  64. if 'detail' in msg['content']:
  65. my_names = self.get_group_member_name(msg['user']['id'], self.my_account['UserName'])
  66. if my_names is None:
  67. my_names = {}
  68. if 'NickName' in self.my_account and self.my_account['NickName']:
  69. my_names['nickname2'] = self.my_account['NickName']
  70. if 'RemarkName' in self.my_account and self.my_account['RemarkName']:
  71. my_names['remark_name2'] = self.my_account['RemarkName']
  72. is_at_me = False
  73. for detail in msg['content']['detail']:
  74. if detail['type'] == 'at':
  75. for k in my_names:
  76. if my_names[k] and my_names[k] == detail['value']:
  77. is_at_me = True
  78. break
  79. if is_at_me:
  80. src_name = msg['content']['user']['name']
  81. reply = 'to ' + src_name + ': '
  82. if msg['content']['type'] == 0: # text message
  83. reply += self.tuling_auto_reply(msg['content']['user']['id'], msg['content']['desc'])
  84. else:
  85. reply += u"对不起,只认字,其他杂七杂八的我都不认识,,,Ծ‸Ծ,,"
  86. self.send_msg_by_uid(reply, msg['user']['id'])
  87. def main():
  88. bot = TulingWXBot()
  89. bot.DEBUG = True
  90. bot.conf['qr'] = 'png'
  91. bot.run()
  92. if __name__ == '__main__':
  93. main()