diff --git a/channel/channel_factory.py b/channel/channel_factory.py index 1346499..320fa3a 100644 --- a/channel/channel_factory.py +++ b/channel/channel_factory.py @@ -25,6 +25,10 @@ def create_channel(channel_type): from channel.wechat.wechat_mp_service_channel import WechatServiceAccount return WechatServiceAccount() + elif channel_type == const.WECHAT_COM: + from channel.wechat.wechat_com_channel import WechatEnterpriseChannel + return WechatEnterpriseChannel() + elif channel_type == const.QQ: from channel.qq.qq_channel import QQChannel return QQChannel() diff --git a/channel/wechat/wechat_com_channel.py b/channel/wechat/wechat_com_channel.py new file mode 100644 index 0000000..a37ae25 --- /dev/null +++ b/channel/wechat/wechat_com_channel.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python +# -*- coding=utf-8 -*- +""" +@time: 2023/4/10 22:24 +@Project :bot-on-anything +@file: wechat_com_channel.py + +""" +from channel.channel import Channel +from concurrent.futures import ThreadPoolExecutor +from common.log import logger +from config import conf + +from wechatpy.enterprise.crypto import WeChatCrypto +from wechatpy.enterprise import WeChatClient +from wechatpy.exceptions import InvalidSignatureException +from wechatpy.enterprise.exceptions import InvalidCorpIdException +from wechatpy.enterprise import parse_message +from flask import Flask, request, abort + +thread_pool = ThreadPoolExecutor(max_workers=8) +app = Flask(__name__) + + +@app.route('/wechat', methods=['GET', 'POST']) +def handler_msg(): + return WechatEnterpriseChannel().handle() + + +_conf = conf().get("wechat_com") + + +class WechatEnterpriseChannel(Channel): + def __init__(self): + self.CorpId = _conf.get('wechat_corp_id') + self.Secret = _conf.get('secret') + self.AppId = _conf.get('appid') + self.TOKEN = _conf.get('wechat_token') + self.EncodingAESKey = _conf.get('wechat_encoding_aes_key') + self.crypto = WeChatCrypto(self.TOKEN, self.EncodingAESKey, self.CorpId) + self.client = WeChatClient(self.CorpId, self.Secret, self.AppId) + + def startup(self): + # start message listener + app.run(host='0.0.0.0', port=8888) + + def send(self, msg, receiver): + logger.info('[WXCOM] sendMsg={}, receiver={}'.format(msg, receiver)) + self.client.message.send_text(self.AppId, receiver, msg) + + def _do_send(self, query, reply_user_id): + try: + if not query: + return + context = dict() + context['from_user_id'] = reply_user_id + reply_text = super().build_reply_content(query, context) + if reply_text: + self.send(reply_text, reply_user_id) + except Exception as e: + logger.exception(e) + + def handle(self): + query_params = request.args + signature = query_params.get('msg_signature', '') + timestamp = query_params.get('timestamp', '') + nonce = query_params.get('nonce', '') + if request.method == 'GET': + # 处理验证请求 + echostr = query_params.get('echostr', '') + try: + echostr = self.crypto.check_signature(signature, timestamp, nonce, echostr) + except InvalidSignatureException: + abort(403) + print(echostr) + return echostr + elif request.method == 'POST': + try: + message = self.crypto.decrypt_message( + request.data, + signature, + timestamp, + nonce + ) + except (InvalidSignatureException, InvalidCorpIdException): + abort(403) + msg = parse_message(message) + if msg.type == 'text': + reply = '收到,思考中...' + thread_pool.submit(self._do_send, msg.content, msg.source) + else: + reply = 'Can not handle this for now' + self.client.message.send_text(self.AppId, msg.source, reply) + return 'success' diff --git a/common/const.py b/common/const.py index bd28a5c..e0fa6e8 100644 --- a/common/const.py +++ b/common/const.py @@ -3,6 +3,7 @@ TERMINAL = "terminal" WECHAT = "wechat" WECHAT_MP = "wechat_mp" WECHAT_MP_SERVICE = "wechat_mp_service" +WECHAT_COM = "wechat_com" QQ = "qq" GMAIL = "gmail" TELEGRAM = "telegram" diff --git a/config-template.json b/config-template.json index 8b61dc2..d02addd 100644 --- a/config-template.json +++ b/config-template.json @@ -45,6 +45,13 @@ "token": "YOUR TOKEN", "port": "80" }, + "wechat_com": { + "wechat_token": "6a7948ff36d8", + "wechat_encoding_aes_key":"jMQA7bDP4u52dILb1W6IGib5c1FooFrHJpEuTczQQeT", + "wechat_corp_id":"ww288c216b922e23a1", + "appid":"1000002", + "secret":"UMPISp6qiaKheLdv1src9nNaKsCWSKE_-5ujU_l2QdY" + }, "gmail": { "subject_keyword": ["bot", "@bot"],