You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
3.0KB

  1. """
  2. wechat channel
  3. """
  4. import itchat
  5. import json
  6. from itchat.content import *
  7. from channel.channel import Channel
  8. from concurrent.futures import ThreadPoolExecutor
  9. from common.log import logger
  10. thead_pool = ThreadPoolExecutor(max_workers=8)
  11. @itchat.msg_register(TEXT)
  12. def handler_single_msg(msg):
  13. WechatChannel().handle(msg)
  14. @itchat.msg_register(TEXT, isGroupChat=True)
  15. def handler_group_msg(msg):
  16. WechatChannel().handle_group(msg)
  17. group_white_list = ['学就完事了', '小宝群', '全天乱斗模式', '戒赌吧', '命苦还要快乐', '攒钱让姐妹当小三的组织',
  18. '快乐家人', '技术沙龙', '流动性混子', '计算机学习交流', '如何评价']
  19. class WechatChannel(Channel):
  20. def __init__(self):
  21. pass
  22. def startup(self):
  23. # login by scan QRCode
  24. itchat.auto_login(enableCmdQR=2)
  25. # start message listener
  26. itchat.run()
  27. def handle(self, msg):
  28. logger.info("[WX]receive msg: " + json.dumps(msg, ensure_ascii=False))
  29. from_user_id = msg['FromUserName']
  30. other_user_id = msg['User']['UserName']
  31. content = msg['Text']
  32. if from_user_id == other_user_id and (content.lower().startswith('bot') or content.lower().startswith('@bot')):
  33. str_list = content.split('bot', 1)
  34. if len(str_list) == 2:
  35. content = str_list[1].strip()
  36. thead_pool.submit(self._do_send, content, from_user_id)
  37. def handle_group(self, msg):
  38. logger.info("[WX]receive group msg: " + json.dumps(msg, ensure_ascii=False))
  39. group_id = msg['User']['UserName']
  40. group_name = msg['User'].get('NickName', None)
  41. if not group_name:
  42. return ""
  43. origin_content = msg['Content']
  44. content = msg['Content']
  45. content_list = content.split(' ', 1)
  46. context_special_list = content.split('\u2005', 1)
  47. if len(context_special_list) == 2:
  48. content = context_special_list[1]
  49. elif len(content_list) == 2:
  50. content = content_list[1]
  51. if group_name in group_white_list and (msg['IsAt'] or origin_content.lower().startswith('@bot')):
  52. thead_pool.submit(self._do_send_group, content, msg)
  53. def send(self, msg, receiver):
  54. # time.sleep(random.randint(1, 3))
  55. logger.info('[WX] sendMsg={}, receiver={}'.format(msg, receiver))
  56. itchat.send(msg, toUserName=receiver)
  57. def _do_send(self, send_msg, reply_user_id):
  58. context = dict()
  59. context['from_user_id'] = reply_user_id
  60. content = super().build_reply_content(send_msg, context)
  61. if content:
  62. self.send("[bot] " + content, reply_user_id)
  63. def _do_send_group(self, content, msg):
  64. context = dict()
  65. context['from_user_id'] = msg['ActualUserName']
  66. reply_text = super().build_reply_content(content, context)
  67. reply_text = '@' + msg['ActualNickName'] + ' ' + reply_text
  68. if reply_text:
  69. self.send(reply_text, msg['User']['UserName'])