Skip to content
This repository was archived by the owner on Mar 3, 2025. It is now read-only.

Commit 1a9717d

Browse files
committed
feat: 适配 at,回复和语音输入
1 parent 4235124 commit 1a9717d

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

tg_message_adapter.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
from astrbot.api.platform import Platform, AstrBotMessage, MessageMember, PlatformMetadata, MessageType
66
from astrbot.api.event import MessageChain
7-
from astrbot.api.message_components import *
7+
from astrbot.api.message_components import Plain, Image, Record
88
from astrbot.core.platform.astr_message_event import MessageSesion
99
from astrbot.api.platform import register_platform_adapter
1010

11-
from telegram import Update
11+
from telegram import Update, File
1212
from telegram.ext import ApplicationBuilder, ContextTypes, filters
1313
from telegram.constants import ChatType
1414
from telegram.ext import MessageHandler as TelegramMessageHandler
@@ -48,7 +48,10 @@ def meta(self) -> PlatformMetadata:
4848
@override
4949
async def run(self):
5050
self.application = ApplicationBuilder().token(self.config['telegram_token']).build()
51-
message_handler = TelegramMessageHandler(filters.TEXT, self.convert_message)
51+
message_handler = TelegramMessageHandler(
52+
filters=None,
53+
callback=self.convert_message
54+
)
5255
self.application.add_handler(message_handler)
5356
await self.application.initialize()
5457
await self.application.start()
@@ -62,24 +65,28 @@ async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
6265

6366
async def convert_message(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> AstrBotMessage:
6467
message = AstrBotMessage()
65-
6668
# 获得是群聊还是私聊
6769
if update.effective_chat.type == ChatType.PRIVATE:
6870
message.type = MessageType.FRIEND_MESSAGE
6971
else:
7072
message.type = MessageType.GROUP_MESSAGE
71-
72-
73-
plain_text = update.message.text
73+
message.group_id = update.effective_chat.id
7474
message.message_id = str(update.message.message_id)
75-
message.message_str = plain_text
7675
message.session_id = str(update.effective_chat.id)
77-
message.sender = MessageMember(str(update.effective_chat.id), update.effective_chat.effective_name)
78-
message.message = [Plain(plain_text),]
79-
message.message_str = plain_text
76+
message.sender = MessageMember(str(update.effective_user.id), update.effective_user.username)
8077
message.self_id = str(context.bot.id)
8178
message.raw_message = update
82-
79+
message.message_str = ""
80+
81+
if update.message.text:
82+
plain_text = update.message.text
83+
message.message = [Plain(plain_text),]
84+
message.message_str = plain_text
85+
86+
elif update.message.voice:
87+
file = await update.message.voice.get_file()
88+
message.message = [Record(file=file.file_path, url=file.file_path),]
89+
8390

8491
await self.handle_msg(message)
8592

tg_message_event.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from astrbot.api.event import AstrMessageEvent, MessageChain
2-
from astrbot.api.platform import AstrBotMessage, PlatformMetadata
3-
from astrbot.api.message_components import Plain, Image
2+
from astrbot.api.platform import AstrBotMessage, PlatformMetadata, MessageType
3+
from astrbot.api.message_components import Plain, Image, Reply, At
44
from telegram.ext import ExtBot
55

66
class TelegramPlatformEvent(AstrMessageEvent):
@@ -11,18 +11,40 @@ def __init__(self, message_str: str, message_obj: AstrBotMessage, platform_meta:
1111
@staticmethod
1212
async def send_with_client(client: ExtBot, message: MessageChain, user_name: str):
1313
image_path = None
14+
15+
has_reply = False
16+
reply_message_id = None
17+
at_user_id = None
18+
for i in message.chain:
19+
if isinstance(i, Reply):
20+
has_reply = True
21+
reply_message_id = i.id
22+
if isinstance(i, At):
23+
at_user_id = i.name
24+
25+
at_flag = False
1426
for i in message.chain:
27+
payload = {
28+
"chat_id": user_name,
29+
}
30+
if has_reply:
31+
payload["reply_to_message_id"] = reply_message_id
32+
1533
if isinstance(i, Plain):
16-
await client.send_message(chat_id=user_name, text=i.text)
34+
if at_user_id and not at_flag:
35+
i.text = f"@{at_user_id} " + i.text
36+
at_flag = True
37+
await client.send_message(text=i.text, **payload)
1738
elif isinstance(i, Image):
1839
if i.path:
1940
image_path = i.path
2041
else:
2142
image_path = i.file
22-
await client.send_photo(chat_id=user_name, photo=image_path)
23-
24-
43+
await client.send_photo(photo=image_path, **payload)
2544

2645
async def send(self, message: MessageChain):
27-
await self.send_with_client(self.client, message, self.get_sender_id())
46+
if self.get_message_type() == MessageType.GROUP_MESSAGE:
47+
await self.send_with_client(self.client, message, self.message_obj.group_id)
48+
else:
49+
await self.send_with_client(self.client, message, self.get_sender_id())
2850
await super().send(message)

0 commit comments

Comments
 (0)