bot.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. elif respond['code'] == 200000:
  29. result = respond['url']
  30. else:
  31. result = respond['text'].replace('<br>', ' ')
  32. print ' ROBOT:', result
  33. return result
  34. else:
  35. return u"知道啦"
  36. def auto_switch(self, msg):
  37. msg_data = msg['content']['data']
  38. STOP = [u'退下', u'走开', u'关闭', u'关掉', u'休息', u'滚开']
  39. START = [u'出来', u'启动', u'工作']
  40. if self.robot_switch:
  41. for i in STOP:
  42. if i == msg_data:
  43. self.robot_switch = False
  44. self.send_msg_by_uid(u'[Robot]' + u'机器人已关闭!', msg['to_user_id'])
  45. else:
  46. for i in START:
  47. if i == msg_data:
  48. self.robot_switch = True
  49. self.send_msg_by_uid(u'[Robot]' + u'机器人已开启!', msg['to_user_id'])
  50. def handle_msg_all(self, msg):
  51. if not self.robot_switch and msg['msg_type_id'] != 1:
  52. return
  53. if msg['msg_type_id'] == 1 and msg['content']['type'] == 0: # reply to self
  54. self.auto_switch(msg)
  55. elif msg['msg_type_id'] == 4 and msg['content']['type'] == 0: # text message from contact
  56. self.send_msg_by_uid(self.tuling_auto_reply(msg['user']['id'], msg['content']['data']), msg['user']['id'])
  57. elif msg['msg_type_id'] == 3: # group message
  58. if msg['content']['data'].find('@') >= 0: # someone @ another
  59. my_names = self.get_group_member_name(msg['user']['id'], self.user['UserName'])
  60. if my_names is None:
  61. my_names = {}
  62. if 'NickName' in self.user and len(self.user['NickName']) > 0:
  63. my_names['nickname2'] = self.user['NickName']
  64. if 'RemarkName' in self.user and len(self.user['RemarkName']) > 0:
  65. my_names['remark_name2'] = self.user['RemarkName']
  66. is_at_me = False
  67. text_msg = ''
  68. for _ in my_names:
  69. if msg['content']['data'].find('@'+my_names[_]) >= 0:
  70. is_at_me = True
  71. text_msg = msg['content']['data'].replace('@'+my_names[_], '').strip()
  72. break
  73. if is_at_me: # someone @ me
  74. snames = self.get_group_member_name(msg['user']['id'], msg['content']['user']['id'])
  75. if snames is None:
  76. snames = self.get_account_name(msg['content']['user']['id'])
  77. src_name = ''
  78. if snames is not None:
  79. if 'display_name' in snames and len(snames['display_name']) > 0:
  80. src_name = snames['display_name']
  81. elif 'nickname' in snames and len(snames['nickname']) > 0:
  82. src_name = snames['nickname']
  83. elif 'remark_name' in snames and len(snames['remark_name']) > 0:
  84. src_name = snames['remark_name']
  85. else:
  86. return
  87. if src_name != '':
  88. reply = '@' + src_name + ' '
  89. if msg['content']['type'] == 0: # text message
  90. reply += self.tuling_auto_reply(msg['content']['user']['id'], text_msg)
  91. else:
  92. reply += u"对不起,只认字,其他杂七杂八的我都不认识,,,Ծ‸Ծ,,"
  93. self.send_msg_by_uid(reply, msg['user']['id'])
  94. def main():
  95. bot = TulingWXBot()
  96. bot.DEBUG = True
  97. bot.conf['qr'] = 'png'
  98. bot.run()
  99. if __name__ == '__main__':
  100. main()