4
4
5
5
from astrbot .api .platform import Platform , AstrBotMessage , MessageMember , PlatformMetadata , MessageType
6
6
from astrbot .api .event import MessageChain
7
- from astrbot .api .message_components import *
7
+ from astrbot .api .message_components import Plain , Image , Record
8
8
from astrbot .core .platform .astr_message_event import MessageSesion
9
9
from astrbot .api .platform import register_platform_adapter
10
10
11
- from telegram import Update
11
+ from telegram import Update , File
12
12
from telegram .ext import ApplicationBuilder , ContextTypes , filters
13
13
from telegram .constants import ChatType
14
14
from telegram .ext import MessageHandler as TelegramMessageHandler
@@ -48,7 +48,10 @@ def meta(self) -> PlatformMetadata:
48
48
@override
49
49
async def run (self ):
50
50
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
+ )
52
55
self .application .add_handler (message_handler )
53
56
await self .application .initialize ()
54
57
await self .application .start ()
@@ -62,24 +65,28 @@ async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
62
65
63
66
async def convert_message (self , update : Update , context : ContextTypes .DEFAULT_TYPE ) -> AstrBotMessage :
64
67
message = AstrBotMessage ()
65
-
66
68
# 获得是群聊还是私聊
67
69
if update .effective_chat .type == ChatType .PRIVATE :
68
70
message .type = MessageType .FRIEND_MESSAGE
69
71
else :
70
72
message .type = MessageType .GROUP_MESSAGE
71
-
72
-
73
- plain_text = update .message .text
73
+ message .group_id = update .effective_chat .id
74
74
message .message_id = str (update .message .message_id )
75
- message .message_str = plain_text
76
75
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 )
80
77
message .self_id = str (context .bot .id )
81
78
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
+
83
90
84
91
await self .handle_msg (message )
85
92
0 commit comments