mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-07-20 05:27:59 +08:00
refactor(wechatmp): use wechatpy to handle wechatmp messages
feat(wechatmp): add support for image and voice replies
This commit is contained in:
@@ -2,15 +2,14 @@ import time
|
||||
|
||||
import web
|
||||
|
||||
from channel.wechatmp.wechatmp_message import parse_xml
|
||||
from channel.wechatmp.passive_reply_message import TextMsg
|
||||
from channel.wechatmp.wechatmp_message import WeChatMPMessage
|
||||
from bridge.context import *
|
||||
from bridge.reply import ReplyType
|
||||
from channel.wechatmp.common import *
|
||||
from channel.wechatmp.wechatmp_channel import WechatMPChannel
|
||||
from wechatpy import parse_message
|
||||
from common.log import logger
|
||||
from config import conf
|
||||
|
||||
from wechatpy.replies import create_reply
|
||||
|
||||
# This class is instantiated once per query
|
||||
class Query:
|
||||
@@ -21,16 +20,22 @@ class Query:
|
||||
# Make sure to return the instance that first created, @singleton will do that.
|
||||
channel = WechatMPChannel()
|
||||
try:
|
||||
webData = web.data()
|
||||
message = web.data() # todo crypto
|
||||
# logger.debug("[wechatmp] Receive request:\n" + webData.decode("utf-8"))
|
||||
wechatmp_msg = parse_xml(webData)
|
||||
if (
|
||||
wechatmp_msg.msg_type == "text"
|
||||
or wechatmp_msg.msg_type == "voice"
|
||||
# or wechatmp_msg.msg_type == "image"
|
||||
):
|
||||
msg = parse_message(message)
|
||||
if msg.type == "event":
|
||||
logger.info(
|
||||
"[wechatmp] Event {} from {}".format(
|
||||
msg.event, msg.source
|
||||
)
|
||||
)
|
||||
reply_text = subscribe_msg()
|
||||
replyPost = create_reply(reply_text, msg)
|
||||
return replyPost.render()
|
||||
wechatmp_msg = WeChatMPMessage(msg, client=channel.client)
|
||||
if wechatmp_msg.ctype in [ContextType.TEXT, ContextType.IMAGE, ContextType.VOICE]:
|
||||
from_user = wechatmp_msg.from_user_id
|
||||
message = wechatmp_msg.content
|
||||
content = wechatmp_msg.content
|
||||
message_id = wechatmp_msg.msg_id
|
||||
|
||||
logger.info(
|
||||
@@ -39,16 +44,18 @@ class Query:
|
||||
web.ctx.env.get("REMOTE_PORT"),
|
||||
from_user,
|
||||
message_id,
|
||||
message,
|
||||
content,
|
||||
)
|
||||
)
|
||||
if (wechatmp_msg.msg_type == "voice" and conf().get("voice_reply_voice") == True):
|
||||
rtype = ReplyType.VOICE
|
||||
if (msg.type == "voice" and wechatmp_msg.ctype == ContextType.TEXT):
|
||||
origin_ctype = ContextType.VOICE
|
||||
context = channel._compose_context(
|
||||
wechatmp_msg.ctype, content, isgroup=False, origin_ctype=origin_ctype, msg=wechatmp_msg
|
||||
)
|
||||
else:
|
||||
rtype = None
|
||||
context = channel._compose_context(
|
||||
ContextType.TEXT, message, isgroup=False, desire_rtype=rtype, msg=wechatmp_msg
|
||||
)
|
||||
context = channel._compose_context(
|
||||
wechatmp_msg.ctype, content, isgroup=False, msg=wechatmp_msg
|
||||
)
|
||||
if context:
|
||||
# set private openai_api_key
|
||||
# if from_user is not changed in itchat, this can be placed at chat_channel
|
||||
@@ -59,18 +66,6 @@ class Query:
|
||||
channel.produce(context)
|
||||
# The reply will be sent by channel.send() in another thread
|
||||
return "success"
|
||||
|
||||
elif wechatmp_msg.msg_type == "event":
|
||||
logger.info(
|
||||
"[wechatmp] Event {} from {}".format(
|
||||
wechatmp_msg.Event, wechatmp_msg.from_user_id
|
||||
)
|
||||
)
|
||||
content = subscribe_msg()
|
||||
replyMsg = TextMsg(
|
||||
wechatmp_msg.from_user_id, wechatmp_msg.to_user_id, content
|
||||
)
|
||||
return replyMsg.send()
|
||||
else:
|
||||
logger.info("暂且不处理")
|
||||
return "success"
|
||||
|
||||
Reference in New Issue
Block a user