Files
bot-on-anything/channel/wechat/wechat_mp_service_channel.py
2023-02-20 23:39:33 +08:00

38 lines
1.4 KiB
Python

import werobot
from config import channel_conf
from common import const
from common.log import logger
from channel.channel import Channel
from concurrent.futures import ThreadPoolExecutor
robot = werobot.WeRoBot(token=channel_conf(const.WECHAT_MP).get('token'))
thread_pool = ThreadPoolExecutor(max_workers=8)
@robot.text
def hello_world(msg):
logger.info('[WX_Public] receive public msg: {}, userId: {}'.format(msg.content, msg.source))
return WechatServiceAccount().handle(msg)
class WechatServiceAccount(Channel):
def startup(self):
logger.info('[WX_Public] Wechat Public account service start!')
robot.config['PORT'] = channel_conf(const.WECHAT_MP).get('port')
robot.config["APP_ID"] = channel_conf(const.WECHAT_MP).get('app_id')
robot.config["APP_SECRET"] = channel_conf(const.WECHAT_MP).get('app_secret')
robot.config['HOST'] = '0.0.0.0'
robot.run()
def handle(self, msg, count=0):
context = {}
context['from_user_id'] = msg.source
thread_pool.submit(self._do_send, msg.content, context)
return "正在思考中..."
def _do_send(self, query, context):
reply_text = super().build_reply_content(query, context)
logger.info('[WX_Public] reply content: {}'.format(reply_text))
client = robot.client
client.send_text_message(context['from_user_id'], reply_text)