1
1
import time
2
+ from typing import Dict , Optional , Union
3
+
2
4
import openai
5
+ import telegram .helpers
3
6
from graia .ariadne .message .chain import MessageChain
4
- from graia .ariadne .message .element import Image , Plain , Voice
7
+ from graia .ariadne .message .element import Plain
5
8
from loguru import logger
6
- from telegram import Update , constants
9
+ from telegram import Update , constants , Message
10
+ from telegram .constants import ParseMode
7
11
from telegram .ext import ApplicationBuilder , ContextTypes , MessageHandler , filters , CommandHandler
8
12
from telegram .request import HTTPXRequest
9
- from framework .middlewares .ratelimit import manager as ratelimit_manager
10
13
11
14
from constants import config , BotPlatform
15
+ from framework .messages import ImageElement
16
+ from framework .middlewares .ratelimit import manager as ratelimit_manager
17
+ from framework .request import Response , Request
18
+ from framework .tts .tts import TTSResponse , VoiceFormat
12
19
from framework .universal import handle_message
13
20
14
21
@@ -27,32 +34,55 @@ async def on_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None
27
34
logger .debug (f"忽略消息(未满足匹配规则): { update .message .text } " )
28
35
return
29
36
30
- async def response (msg ):
31
- if isinstance (msg , MessageChain ):
32
- for elem in msg :
33
- if isinstance (elem , Plain ):
34
- await update .message .reply_text (str (elem ))
35
- if isinstance (elem , Image ):
36
- await update .message .reply_photo (photo = await elem .get_bytes ())
37
- if isinstance (elem , Voice ):
38
- await update .message .reply_audio (audio = await elem .get_bytes ())
39
- return
40
- if isinstance (msg , str ):
41
- return await update .message .reply_text (msg )
42
- if isinstance (msg , Image ):
43
- return await update .message .reply_photo (photo = await msg .get_bytes ())
44
- if isinstance (msg , Voice ):
45
- await update .message .reply_audio (audio = await msg .get_bytes (), title = "Voice" )
46
- return
47
-
48
- await handle_message (
49
- response ,
50
- f"{ type } -{ update .message .chat .id } " ,
51
- update .message .text .replace (f"@{ bot_username } " , '' ).strip (),
52
- is_manager = update .message .from_user .id == config .telegram .manager_chat ,
53
- nickname = update .message .from_user .full_name or "群友" ,
54
- request_from = BotPlatform .TelegramBot
55
- )
37
+ last_message_item : Optional [Message ] = None
38
+ last_send_text : str = ''
39
+
40
+ async def _response_func (chain : MessageChain , text : str , voice : TTSResponse , image : ImageElement ):
41
+ nonlocal last_message_item , last_send_text
42
+ if text :
43
+ last_send_text += telegram .helpers .escape_markdown (text , 2 )
44
+ if voice :
45
+ await update .message .reply_chat_action (action = telegram .constants .ChatAction .UPLOAD_VOICE )
46
+ last_message_item = await update .message .reply_audio (audio = await voice .transcode (VoiceFormat .Wav ),
47
+ title = "Voice Message" ,
48
+ caption = last_send_text )
49
+ if image :
50
+ await update .message .reply_chat_action (action = telegram .constants .ChatAction .UPLOAD_PHOTO )
51
+ last_message_item = await update .message .reply_photo (photo = await image .get_bytes (),
52
+ caption = last_send_text ,
53
+ parse_mode = ParseMode .MARKDOWN_V2 )
54
+
55
+ elif text :
56
+ await update .message .reply_chat_action (action = telegram .constants .ChatAction .TYPING )
57
+ if last_message_item :
58
+ if last_message_item .text :
59
+ last_message_item = await last_message_item .edit_text (
60
+ text = last_send_text ,
61
+ parse_mode = ParseMode .MARKDOWN_V2
62
+ )
63
+ else :
64
+ last_message_item = await last_message_item .edit_caption (
65
+ caption = last_send_text ,
66
+ parse_mode = ParseMode .MARKDOWN_V2
67
+ )
68
+ else :
69
+ last_message_item = await update .message .reply_text (
70
+ text = last_send_text ,
71
+ parse_mode = ParseMode .MARKDOWN_V2
72
+ )
73
+ last_send_text = ''
74
+
75
+ request = Request ()
76
+ request .session_id = f"{ type } -{ update .message .chat .id } "
77
+ request .user_id = update .message .from_user .id
78
+ request .group_id = update .message .chat_id
79
+ request .nickname = update .message .from_user .full_name or "路人甲"
80
+ request .message = MessageChain ([Plain (update .message .text .replace (f"@{ bot_username } " , '' ).strip ())])
81
+
82
+ response = Response (_response_func )
83
+
84
+ await update .message .reply_chat_action (action = telegram .constants .ChatAction .TYPING )
85
+ await handle_message (request , response )
56
86
57
87
58
88
async def on_check_presets_list (update : Update , context : ContextTypes .DEFAULT_TYPE ):
0 commit comments