mirror of
https://github.com/zhayujie/bot-on-anything.git
synced 2026-02-11 17:15:55 +08:00
ceshi
This commit is contained in:
@@ -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()
|
||||
|
||||
94
channel/wechat/wechat_com_channel.py
Normal file
94
channel/wechat/wechat_com_channel.py
Normal file
@@ -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'
|
||||
@@ -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"
|
||||
|
||||
@@ -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"],
|
||||
|
||||
Reference in New Issue
Block a user