選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

107 行
3.5KB

  1. # encoding:utf-8
  2. """
  3. wechat channel
  4. """
  5. import itchat
  6. import json
  7. from itchat.content import *
  8. from channel.channel import Channel
  9. from concurrent.futures import ThreadPoolExecutor
  10. from common.log import logger
  11. from config import conf
  12. thead_pool = ThreadPoolExecutor(max_workers=8)
  13. @itchat.msg_register(TEXT)
  14. def handler_single_msg(msg):
  15. WechatChannel().handle(msg)
  16. @itchat.msg_register(TEXT, isGroupChat=True)
  17. def handler_group_msg(msg):
  18. WechatChannel().handle_group(msg)
  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. to_user_id = msg['ToUserName']
  31. other_user_id = msg['User']['UserName']
  32. content = msg['Text']
  33. if from_user_id == other_user_id and \
  34. self.check_prefix(content, conf().get('single_chat_prefix')):
  35. str_list = content.split('bot', 1)
  36. if len(str_list) == 2:
  37. content = str_list[1].strip()
  38. thead_pool.submit(self._do_send, content, from_user_id)
  39. elif to_user_id == other_user_id and \
  40. self.check_prefix(content, conf().get('single_chat_prefix')):
  41. str_list = content.split('bot', 1)
  42. if len(str_list) == 2:
  43. content = str_list[1].strip()
  44. thead_pool.submit(self._do_send, content, to_user_id)
  45. def handle_group(self, msg):
  46. logger.info("[WX]receive group msg: " + json.dumps(msg, ensure_ascii=False))
  47. group_name = msg['User'].get('NickName', None)
  48. if not group_name:
  49. return ""
  50. origin_content = msg['Content']
  51. content = msg['Content']
  52. content_list = content.split(' ', 1)
  53. context_special_list = content.split('\u2005', 1)
  54. if len(context_special_list) == 2:
  55. content = context_special_list[1]
  56. elif len(content_list) == 2:
  57. content = content_list[1]
  58. config = conf()
  59. if group_name in config.get('group_name_white_list') \
  60. and (msg['IsAt'] or self.check_prefix(origin_content, config.get('group_chat_prefix'))):
  61. thead_pool.submit(self._do_send_group, content, msg)
  62. def send(self, msg, receiver):
  63. # time.sleep(random.randint(1, 3))
  64. logger.info('[WX] sendMsg={}, receiver={}'.format(msg, receiver))
  65. itchat.send(msg, toUserName=receiver)
  66. def _do_send(self, query, reply_user_id):
  67. if not query:
  68. return
  69. context = dict()
  70. context['from_user_id'] = reply_user_id
  71. reply_text = super().build_reply_content(query, context).strip()
  72. if reply_text:
  73. self.send(conf().get("single_chat_reply_prefix") + reply_text, reply_user_id)
  74. def _do_send_group(self, query, msg):
  75. if not query:
  76. return
  77. context = dict()
  78. context['from_user_id'] = msg['ActualUserName']
  79. reply_text = super().build_reply_content(query, context)
  80. reply_text = '@' + msg['ActualNickName'] + ' ' + reply_text.strip()
  81. if reply_text:
  82. self.send(reply_text, msg['User']['UserName'])
  83. def check_prefix(self, content, prefix_list):
  84. for prefix in prefix_list:
  85. if content.lower().startswith(prefix):
  86. return True
  87. return False