bot.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. try:
  11. cf = ConfigParser.ConfigParser()
  12. cf.read('conf.ini')
  13. self.tuling_key = cf.get('main', 'key')
  14. except Exception:
  15. pass
  16. print 'tuling_key:', self.tuling_key
  17. def tuling_auto_reply(self, uid, msg):
  18. if self.tuling_key:
  19. url = "http://www.tuling123.com/openapi/api"
  20. user_id = uid.replace('@', '')[:30]
  21. body = {'key': self.tuling_key, 'info': msg.encode('utf8'), 'userid': user_id}
  22. r = requests.post(url, data=body)
  23. respond = json.loads(r.text)
  24. result = ''
  25. if respond['code'] == 100000:
  26. result = respond['text'].replace('<br>', ' ')
  27. elif respond['code'] == 200000:
  28. result = respond['url']
  29. else:
  30. result = respond['text'].replace('<br>', ' ')
  31. return result
  32. else:
  33. return u"知道啦"
  34. def handle_msg_all(self, msg):
  35. if msg['msg_type_id'] == 4 and msg['content']['type'] == 0: # text message from contact
  36. self.send_msg_by_uid(self.tuling_auto_reply(msg['user']['id'], msg['content']['data']), msg['user']['id'])
  37. elif msg['msg_type_id'] == 3: # group message
  38. if msg['content']['data'].find('@') >= 0: # someone @ another
  39. my_names = self.get_group_member_name(msg['user']['id'], self.user['UserName'])
  40. if my_names is None:
  41. my_names = {}
  42. if 'NickName' in self.user and len(self.user['NickName']) > 0:
  43. my_names['nickname2'] = self.user['NickName']
  44. if 'RemarkName' in self.user and len(self.user['RemarkName']) > 0:
  45. my_names['remark_name2'] = self.user['RemarkName']
  46. is_at_me = False
  47. text_msg = ''
  48. for _ in my_names:
  49. if msg['content']['data'].find('@'+my_names[_]) >= 0:
  50. is_at_me = True
  51. text_msg = msg['content']['data'].replace('@'+my_names[_], '').strip()
  52. break
  53. if is_at_me: # someone @ me
  54. snames = self.get_group_member_name(msg['user']['id'], msg['content']['user']['id'])
  55. src_name = ''
  56. if 'display_name' in snames and len(snames['display_name']) > 0:
  57. src_name = snames['display_name']
  58. elif 'nickname' in snames and len(snames['nickname']) > 0:
  59. src_name = snames['nickname']
  60. elif 'remark_name' in snames and len(snames['remark_name']) > 0:
  61. src_name = snames['remark_name']
  62. if src_name != '':
  63. reply = '@' + src_name + ' '
  64. if msg['content']['type'] == 0: # text message
  65. reply += self.tuling_auto_reply(msg['content']['user']['id'], text_msg)
  66. else:
  67. reply += u"对不起,只认字,其他杂七杂八的我都不认识,,,Ծ‸Ծ,,"
  68. self.send_msg_by_uid(reply, msg['user']['id'])
  69. def main():
  70. bot = TulingWXBot()
  71. bot.DEBUG = True
  72. bot.conf['qr'] = 'png'
  73. bot.run()
  74. if __name__ == '__main__':
  75. main()