diff --git a/app/modules/discord/discord.py b/app/modules/discord/discord.py index 8477c638..f1997c5c 100644 --- a/app/modules/discord/discord.py +++ b/app/modules/discord/discord.py @@ -2,6 +2,7 @@ import asyncio import re import threading from typing import Optional, List, Dict, Any, Tuple, Union +from urllib.parse import quote import discord from discord import app_commands @@ -47,7 +48,9 @@ class Discord: base_ds_url = f"http://127.0.0.1:{settings.PORT}/api/v1/message/" self._ds_url = f"{base_ds_url}?token={settings.API_TOKEN}" if kwargs.get("name"): - self._ds_url = f"{self._ds_url}&source={kwargs.get('name')}" + # URL encode the source name to handle special characters in config names + encoded_name = quote(kwargs.get('name'), safe='') + self._ds_url = f"{self._ds_url}&source={encoded_name}" logger.debug(f"[Discord] 消息回调 URL: {self._ds_url}") intents = discord.Intents.default() @@ -548,10 +551,11 @@ class Discord: """ Resolve the channel to send messages to. Priority order: - 1. chat_id (original channel where user sent the message) - for contextual replies - 2. userid (DM) - for private conversations - 3. Configured _channel_id (broadcast channel) - for system notifications + 1. `chat_id` (original channel where user sent the message) - for contextual replies + 2. `userid` mapping (channel where user last sent a message) - for contextual replies + 3. Configured `_channel_id` (broadcast channel) - for system notifications 4. Any available text channel in configured guild - fallback + 5. `userid` (DM) - for private conversations as a final fallback """ logger.debug(f"[Discord] _resolve_channel: userid={userid}, chat_id={chat_id}, " f"_channel_id={self._channel_id}, _guild_id={self._guild_id}") diff --git a/app/modules/slack/slack.py b/app/modules/slack/slack.py index 3931ecee..16f890c0 100644 --- a/app/modules/slack/slack.py +++ b/app/modules/slack/slack.py @@ -1,6 +1,7 @@ import re from threading import Lock from typing import List, Optional +from urllib.parse import quote import requests from slack_bolt import App @@ -42,7 +43,9 @@ class Slack: # 标记消息来源 if kwargs.get("name"): - self._ds_url = f"{self._ds_url}&source={kwargs.get('name')}" + # URL encode the source name to handle special characters + encoded_name = quote(kwargs.get('name'), safe='') + self._ds_url = f"{self._ds_url}&source={encoded_name}" # 注册消息响应 @slack_app.event("message") diff --git a/app/modules/telegram/telegram.py b/app/modules/telegram/telegram.py index c7588e95..86817b8a 100644 --- a/app/modules/telegram/telegram.py +++ b/app/modules/telegram/telegram.py @@ -2,7 +2,7 @@ import asyncio import re import threading from typing import Optional, List, Dict, Callable -from urllib.parse import urljoin +from urllib.parse import urljoin, quote from telebot import TeleBot, apihelper from telebot.types import BotCommand, InlineKeyboardMarkup, InlineKeyboardButton, InputMediaPhoto @@ -65,7 +65,9 @@ class Telegram: # 标记渠道来源 if kwargs.get("name"): - self._ds_url = f"{self._ds_url}&source={kwargs.get('name')}" + # URL encode the source name to handle special characters + encoded_name = quote(kwargs.get('name'), safe='') + self._ds_url = f"{self._ds_url}&source={encoded_name}" @_bot.message_handler(commands=['start', 'help']) def send_welcome(message):