Parcourir la source

fix: avoid channel to generate not support reply

lanvent il y a 3 ans
Parent
commit
1dff630257

+ 2 - 1
channel/channel.py

@@ -4,9 +4,10 @@ Message sending channel abstract class
 
 from bridge.bridge import Bridge
 from bridge.context import Context
-from bridge.reply import Reply
+from bridge.reply import *
 
 class Channel(object):
+    NOT_SUPPORT_REPLYTYPE = [ReplyType.VOICE, ReplyType.IMAGE]
     def startup(self):
         """
         init channel

+ 12 - 6
channel/chat_channel.py

@@ -112,10 +112,10 @@ class ChatChannel(Channel):
             else:
                 context.type = ContextType.TEXT
             context.content = content
-            if 'desire_rtype' not in context and conf().get('always_reply_voice'):
+            if 'desire_rtype' not in context and conf().get('always_reply_voice') and ReplyType.VOICE not in self.NOT_SUPPORT_REPLYTYPE:
                 context['desire_rtype'] = ReplyType.VOICE
         elif context.type == ContextType.VOICE: 
-            if 'desire_rtype' not in context and conf().get('voice_reply_voice'):
+            if 'desire_rtype' not in context and conf().get('voice_reply_voice') and ReplyType.VOICE not in self.NOT_SUPPORT_REPLYTYPE:
                 context['desire_rtype'] = ReplyType.VOICE
 
         return context
@@ -182,19 +182,25 @@ class ChatChannel(Channel):
             reply = e_context['reply']
             desire_rtype = context.get('desire_rtype')
             if not e_context.is_pass() and reply and reply.type:
+                
+                if reply.type in self.NOT_SUPPORT_REPLYTYPE:
+                    logger.error("[WX]reply type not support: " + str(reply.type))
+                    reply.type = ReplyType.ERROR
+                    reply.content = "不支持发送的消息类型: " + str(reply.type)
+
                 if reply.type == ReplyType.TEXT:
                     reply_text = reply.content
-                    if desire_rtype == ReplyType.VOICE:
+                    if desire_rtype == ReplyType.VOICE and ReplyType.VOICE not in self.NOT_SUPPORT_REPLYTYPE:
                         reply = super().build_text_to_voice(reply.content)
                         return self._decorate_reply(context, reply)
                     if context['isgroup']:
                         reply_text = '@' +  context['msg'].actual_user_nickname + ' ' + reply_text.strip()
-                        reply_text = conf().get("group_chat_reply_prefix", "")+reply_text
+                        reply_text = conf().get("group_chat_reply_prefix", "") + reply_text
                     else:
-                        reply_text = conf().get("single_chat_reply_prefix", "")+reply_text
+                        reply_text = conf().get("single_chat_reply_prefix", "") + reply_text
                     reply.content = reply_text
                 elif reply.type == ReplyType.ERROR or reply.type == ReplyType.INFO:
-                    reply.content = str(reply.type)+":\n" + reply.content
+                    reply.content = "["+str(reply.type)+"]\n" + reply.content
                 elif reply.type == ReplyType.IMAGE_URL or reply.type == ReplyType.VOICE or reply.type == ReplyType.IMAGE:
                     pass
                 else:

+ 1 - 0
channel/wechat/wechat_channel.py

@@ -92,6 +92,7 @@ def qrCallback(uuid,status,qrcode):
 
 @singleton
 class WechatChannel(ChatChannel):
+    NOT_SUPPORT_REPLYTYPE = []
     def __init__(self):
         super().__init__()
         self.receivedMsgs = ExpiredDict(60*60*24) 

+ 1 - 1
channel/wechat/wechaty_channel.py

@@ -26,7 +26,7 @@ except Exception as e:
 
 @singleton
 class WechatyChannel(ChatChannel):
-
+    NOT_SUPPORT_REPLYTYPE = []
     def __init__(self):
         super().__init__()
 

+ 1 - 0
channel/wechatmp/wechatmp_channel.py

@@ -28,6 +28,7 @@ import traceback
 
 @singleton
 class WechatMPChannel(ChatChannel):
+    NOT_SUPPORT_REPLYTYPE = [ReplyType.IMAGE, ReplyType.VOICE]
     def __init__(self):
         super().__init__()
         self.cache_dict = dict()

+ 3 - 0
plugins/sdwebui/sdwebui.py

@@ -40,6 +40,9 @@ class SDWebUI(Plugin):
 
         if e_context['context'].type != ContextType.IMAGE_CREATE:
             return
+        channel = e_context['context'].channel
+        if ReplyType.IMAGE in channel.NOT_SUPPORT_REPLYTYPE:
+            return
 
         logger.debug("[SD] on_handle_context. content: %s" %e_context['context'].content)