feat: add inline mode handling
This commit is contained in:
parent
c4d49694a2
commit
01b282f1b0
4 changed files with 163 additions and 0 deletions
|
@ -10,6 +10,7 @@ from aiogram.filters import CommandStart, Command
|
|||
from aiogram.client.session.aiohttp import AiohttpSession
|
||||
from aiogram import F
|
||||
|
||||
from core.inline import handle_inline_query
|
||||
from core.mc import handle_mc_status_command
|
||||
from core.middleware.rikki import RikkiMiddleware
|
||||
from core.post_to_fedi import router as fedi_router
|
||||
|
@ -77,6 +78,7 @@ class TelegramAdapter:
|
|||
# repeater 模块
|
||||
repeater_router.message(F.chat.type.in_({'group', 'supergroup'}))(MessageRepeater().handle_message)
|
||||
router.message(F.text == '我是笨蛋')(handle_baka)
|
||||
router.inline_query()(handle_inline_query)
|
||||
# 捕获所有其他消息
|
||||
dummy_router.message(F.chat.type.in_({'group', 'supergroup'}))(dummy_handler)
|
||||
|
||||
|
|
149
core/inline.py
Normal file
149
core/inline.py
Normal file
|
@ -0,0 +1,149 @@
|
|||
from aiogram.enums import ParseMode
|
||||
from aiogram.types import InlineQuery, InlineQueryResultArticle, InputTextMessageContent
|
||||
|
||||
|
||||
async def handle_inline_query(query: InlineQuery):
|
||||
"""
|
||||
Handle inline queries.
|
||||
This function is called when an inline query is received.
|
||||
It can be used to provide inline results based on the query.
|
||||
"""
|
||||
print(f"Received inline query")
|
||||
query_text = query.query
|
||||
if query_text == "":
|
||||
await query.answer(results=[
|
||||
InlineQueryResultArticle(
|
||||
id="1",
|
||||
title="你还没什么都没输入呢",
|
||||
input_message_content=InputTextMessageContent(
|
||||
message_text=f"难说!",
|
||||
parse_mode=ParseMode.MARKDOWN
|
||||
),
|
||||
description=f"或许你可以试试 'search' 什么的"
|
||||
),
|
||||
], cache_time=0)
|
||||
return
|
||||
|
||||
if query_text.startswith("search"):
|
||||
search_query = query_text.replace("search", "").strip()
|
||||
if search_query:
|
||||
await query.answer(results=[
|
||||
InlineQueryResultArticle(
|
||||
id="1",
|
||||
title="丢一个 DuckDuckGo 的搜索结果",
|
||||
input_message_content=InputTextMessageContent(
|
||||
message_text=f"我建议你用 [DuckDuckGo 搜一下 {search_query}](https://duckduckgo.com/?q={search_query})",
|
||||
parse_mode=ParseMode.MARKDOWN
|
||||
),
|
||||
description=f"在 DuckDuckGo 上搜索 {search_query}"
|
||||
),
|
||||
InlineQueryResultArticle(
|
||||
id="2",
|
||||
title="丢一个 Google 的搜索结果",
|
||||
input_message_content=InputTextMessageContent(
|
||||
message_text=f"我建议你用 [Google 搜一下 {search_query}](https://www.google.com/search?q={search_query})",
|
||||
parse_mode=ParseMode.MARKDOWN
|
||||
),
|
||||
description=f"在 Google 上搜索 {search_query}"
|
||||
)
|
||||
], cache_time=0)
|
||||
else:
|
||||
await query.answer(results=[
|
||||
InlineQueryResultArticle(
|
||||
id="1",
|
||||
title="输入搜索内容",
|
||||
input_message_content=InputTextMessageContent(
|
||||
message_text="ta 好像想让你用搜索引擎搜索这个问题,但 ta 没有输入任何内容。",
|
||||
parse_mode=ParseMode.MARKDOWN
|
||||
),
|
||||
description="请在 'search' 后输入你想要搜索的内容。"
|
||||
)
|
||||
], cache_time=0)
|
||||
return
|
||||
|
||||
if query_text.startswith("pg"):
|
||||
text = query_text.replace("pg", "").strip()
|
||||
import pangu
|
||||
text = pangu.spacing_text(text)
|
||||
await query.answer(results=[
|
||||
InlineQueryResultArticle(
|
||||
id="1",
|
||||
title="发送 Pangu 格式化之后的结果",
|
||||
input_message_content=InputTextMessageContent(
|
||||
message_text=text,
|
||||
parse_mode=ParseMode.MARKDOWN
|
||||
),
|
||||
description=f"格式化后的文本:{text}"
|
||||
)
|
||||
], cache_time=0)
|
||||
return
|
||||
if query_text == "你的头怎么尖尖的":
|
||||
await query.answer(results=[
|
||||
InlineQueryResultArticle(
|
||||
id="1",
|
||||
title="阿诺模式",
|
||||
input_message_content=InputTextMessageContent(
|
||||
message_text="你的头怎么尖尖的,那我问你",
|
||||
parse_mode=ParseMode.MARKDOWN
|
||||
),
|
||||
description="我可能是阿诺,但我是阿诺不太可能"
|
||||
)
|
||||
], cache_time=0)
|
||||
return
|
||||
"""
|
||||
if query_text.startswith("你的头怎么绿绿的"):
|
||||
await query.answer(results=[
|
||||
InlineQueryResultArticle(
|
||||
id="1",
|
||||
title="头上总得有点绿",
|
||||
input_message_content=InputTextMessageContent(
|
||||
message_text="你说的对,我的头是绿的",
|
||||
parse_mode=ParseMode.MARKDOWN
|
||||
),
|
||||
description="说实话,你一般问出来这个问题的时候,我一般不建议你再继续下去了"
|
||||
)
|
||||
], cache_time=0)
|
||||
return
|
||||
"""
|
||||
if query_text.startswith('anuo'):
|
||||
main = query_text.replace("anuo", "").strip()
|
||||
await query.answer(results=[
|
||||
InlineQueryResultArticle(
|
||||
id="1",
|
||||
title="阿诺的公式",
|
||||
input_message_content=InputTextMessageContent(
|
||||
message_text=f"{"我可能是阿诺,但我是阿诺不太可能" if not main else f"{main}有可能,但{main}不太可能"}",
|
||||
parse_mode=ParseMode.MARKDOWN
|
||||
),
|
||||
description=f"{"我可能是阿诺,但我是阿诺不太可能" if not main else f"{main}有可能,但{main}不太可能"}"
|
||||
)
|
||||
], cache_time=0)
|
||||
return
|
||||
if query_text.startswith("将军:"):
|
||||
await query.answer(results=[
|
||||
InlineQueryResultArticle(
|
||||
id="1",
|
||||
title="这句话不用记",
|
||||
input_message_content=InputTextMessageContent(
|
||||
message_text=f"{query_text}\n\n旁边的手下:✍✍✍✍✍✍✍✍✍✍✍\n️围观的群众:\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o"
|
||||
f"/\\o/\\o/\\o/",
|
||||
parse_mode=ParseMode.MARKDOWN
|
||||
),
|
||||
description=f"\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o/"
|
||||
),
|
||||
], cache_time=0)
|
||||
return
|
||||
# 如果没有匹配到任何内容,则返回一个默认的结果
|
||||
await query.answer(results=[
|
||||
InlineQueryResultArticle(
|
||||
id="2",
|
||||
title=f"嘿,你好啊 {query.from_user.full_name}!",
|
||||
input_message_content=InputTextMessageContent(
|
||||
message_text="小娜😭",
|
||||
parse_mode=ParseMode.MARKDOWN
|
||||
),
|
||||
description="很抱歉,我还不能理解你说的内容。"
|
||||
)
|
||||
], cache_time=0)
|
||||
return
|
||||
|
|
@ -10,6 +10,7 @@ dependencies = [
|
|||
"mastodon-py==2.0.1",
|
||||
"matrix-nio==0.25.2",
|
||||
"mcstatus==12.0.2",
|
||||
"pangu==4.0.6.1",
|
||||
"python-abp==0.2.0",
|
||||
"pyyaml>=6.0.2",
|
||||
"requests>=2.32.4",
|
||||
|
|
11
uv.lock
generated
11
uv.lock
generated
|
@ -457,6 +457,15 @@ wheels = [
|
|||
{ url = "https://files.pythonhosted.org/packages/d8/30/9aec301e9772b098c1f5c0ca0279237c9766d94b97802e9888010c64b0ed/multidict-6.6.3-py3-none-any.whl", hash = "sha256:8db10f29c7541fc5da4defd8cd697e1ca429db743fa716325f236079b96f775a", size = 12313, upload-time = "2025-06-30T15:53:45.437Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pangu"
|
||||
version = "4.0.6.1"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/78/dd/55a1e9d35ac17f49869359b2197fc09d17ee024ba545985b49b3e75a870a/pangu-4.0.6.1.tar.gz", hash = "sha256:f799d127709fe5ecff5d476945ac63d4be758b7bda02efb95715f1dc24472a2f", size = 6288, upload-time = "2019-02-08T18:17:30.527Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/48/77/b52fac2ca4e4596f22dd6200b99ad515fb64b1ae7d3a12325b45b11e2a67/pangu-4.0.6.1-py3-none-any.whl", hash = "sha256:5023dced34b48da7ec61a2847efcbb8cf4fb748f8e589e1e7dfb008b7ad00206", size = 6401, upload-time = "2019-02-08T18:17:28.142Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "propcache"
|
||||
version = "0.3.2"
|
||||
|
@ -679,6 +688,7 @@ dependencies = [
|
|||
{ name = "mastodon-py" },
|
||||
{ name = "matrix-nio" },
|
||||
{ name = "mcstatus" },
|
||||
{ name = "pangu" },
|
||||
{ name = "python-abp" },
|
||||
{ name = "pyyaml" },
|
||||
{ name = "requests" },
|
||||
|
@ -692,6 +702,7 @@ requires-dist = [
|
|||
{ name = "mastodon-py", specifier = "==2.0.1" },
|
||||
{ name = "matrix-nio", specifier = "==0.25.2" },
|
||||
{ name = "mcstatus", specifier = "==12.0.2" },
|
||||
{ name = "pangu", specifier = "==4.0.6.1" },
|
||||
{ name = "python-abp", specifier = "==0.2.0" },
|
||||
{ name = "pyyaml", specifier = ">=6.0.2" },
|
||||
{ name = "requests", specifier = ">=2.32.4" },
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue