fix: [slack&discord&telegram] handle special characters in config names

This commit is contained in:
ChanningHe
2026-02-04 14:09:40 +09:00
parent 636f338ed7
commit 1147930f3f
3 changed files with 16 additions and 7 deletions
+8 -4
View File
@@ -2,6 +2,7 @@ import asyncio
import re import re
import threading import threading
from typing import Optional, List, Dict, Any, Tuple, Union from typing import Optional, List, Dict, Any, Tuple, Union
from urllib.parse import quote
import discord import discord
from discord import app_commands 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/" 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}" self._ds_url = f"{base_ds_url}?token={settings.API_TOKEN}"
if kwargs.get("name"): 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}") logger.debug(f"[Discord] 消息回调 URL: {self._ds_url}")
intents = discord.Intents.default() intents = discord.Intents.default()
@@ -548,10 +551,11 @@ class Discord:
""" """
Resolve the channel to send messages to. Resolve the channel to send messages to.
Priority order: Priority order:
1. chat_id (original channel where user sent the message) - for contextual replies 1. `chat_id` (original channel where user sent the message) - for contextual replies
2. userid (DM) - for private conversations 2. `userid` mapping (channel where user last sent a message) - for contextual replies
3. Configured _channel_id (broadcast channel) - for system notifications 3. Configured `_channel_id` (broadcast channel) - for system notifications
4. Any available text channel in configured guild - fallback 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}, " logger.debug(f"[Discord] _resolve_channel: userid={userid}, chat_id={chat_id}, "
f"_channel_id={self._channel_id}, _guild_id={self._guild_id}") f"_channel_id={self._channel_id}, _guild_id={self._guild_id}")
+4 -1
View File
@@ -1,6 +1,7 @@
import re import re
from threading import Lock from threading import Lock
from typing import List, Optional from typing import List, Optional
from urllib.parse import quote
import requests import requests
from slack_bolt import App from slack_bolt import App
@@ -42,7 +43,9 @@ class Slack:
# 标记消息来源 # 标记消息来源
if kwargs.get("name"): 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") @slack_app.event("message")
+4 -2
View File
@@ -2,7 +2,7 @@ import asyncio
import re import re
import threading import threading
from typing import Optional, List, Dict, Callable 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 import TeleBot, apihelper
from telebot.types import BotCommand, InlineKeyboardMarkup, InlineKeyboardButton, InputMediaPhoto from telebot.types import BotCommand, InlineKeyboardMarkup, InlineKeyboardButton, InputMediaPhoto
@@ -65,7 +65,9 @@ class Telegram:
# 标记渠道来源 # 标记渠道来源
if kwargs.get("name"): 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']) @_bot.message_handler(commands=['start', 'help'])
def send_welcome(message): def send_welcome(message):