fix: inline_query indentation & prepare for increment buttons
This commit is contained in:
73
bot.py
73
bot.py
@@ -7,7 +7,7 @@ import sys
|
||||
from pathlib import Path
|
||||
from typing import Dict, Any, Optional
|
||||
|
||||
from telegram import Update
|
||||
from telegram import Update, InlineQueryResultArticle, InputTextMessageContent
|
||||
from telegram.constants import ParseMode
|
||||
from telegram.ext import (
|
||||
ApplicationBuilder,
|
||||
@@ -16,6 +16,7 @@ from telegram.ext import (
|
||||
MessageHandler,
|
||||
filters,
|
||||
ChatMemberHandler,
|
||||
InlineQueryHandler,
|
||||
)
|
||||
|
||||
DATA_DIR = Path(os.environ.get("DATA_DIR", "/data"))
|
||||
@@ -203,6 +204,73 @@ async def error_handler(update: object, context: ContextTypes.DEFAULT_TYPE):
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# ---------------------- Inline Query ----------------------
|
||||
|
||||
async def inline_query(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
"""Answer inline queries listing counters; selecting one increments it.
|
||||
|
||||
Hinweis: Auswahl eines Counters inkrementiert ihn und sendet den neuen Wert.
|
||||
Usage: @DeinBot [filter]
|
||||
"""
|
||||
if update.inline_query is None:
|
||||
return
|
||||
query_text = (update.inline_query.query or "").strip().lower()
|
||||
counters: Dict[str, int] = context.bot_data.get('counters', {})
|
||||
results = []
|
||||
if not counters:
|
||||
results.append(
|
||||
InlineQueryResultArticle(
|
||||
id="_no_counters_",
|
||||
title="Keine Counter vorhanden",
|
||||
description="/add <name> legt einen neuen Counter an",
|
||||
input_message_content=InputTextMessageContent(
|
||||
"Noch keine Counter vorhanden. Nutze /add <name>."
|
||||
),
|
||||
)
|
||||
)
|
||||
else:
|
||||
# Erste Ergebniszeile ist ein Hinweis
|
||||
results.append(
|
||||
InlineQueryResultArticle(
|
||||
id="_hint_",
|
||||
title="Hinweis: Auswahl inkrementiert",
|
||||
description="Beim Antippen eines Counters wird er um 1 erhöht",
|
||||
input_message_content=InputTextMessageContent(
|
||||
"(Inline Hinweis) — Auswahl eines Counters erhöht ihn um 1."
|
||||
),
|
||||
)
|
||||
)
|
||||
all_items = sorted(counters.items())
|
||||
if query_text:
|
||||
all_items = [kv for kv in all_items if query_text in kv[0]]
|
||||
for name, value in all_items[:50]:
|
||||
new_val = value + 1
|
||||
results.append(
|
||||
InlineQueryResultArticle(
|
||||
id=f"counter_{name}",
|
||||
title=f"{name} -> {new_val}",
|
||||
description=f"Aktuell: {value}; nach Auswahl: {new_val}",
|
||||
input_message_content=InputTextMessageContent(
|
||||
f"/increment {name}"
|
||||
),
|
||||
)
|
||||
)
|
||||
if len(results) == 1: # nur Hinweis, keine Treffer
|
||||
results.append(
|
||||
InlineQueryResultArticle(
|
||||
id="_no_match_",
|
||||
title="Keine Treffer",
|
||||
description="Filter ändern oder neuen Counter anlegen",
|
||||
input_message_content=InputTextMessageContent(
|
||||
"Keine passenden Counter gefunden."
|
||||
),
|
||||
)
|
||||
)
|
||||
try:
|
||||
await update.inline_query.answer(results, cache_time=0, is_personal=True)
|
||||
except Exception as e:
|
||||
logger.warning("Fehler beim Beantworten der Inline Query: %s", e)
|
||||
|
||||
|
||||
# ---------------------- Group Events ----------------------
|
||||
|
||||
@@ -325,6 +393,7 @@ def main():
|
||||
# Group / membership events
|
||||
application.add_handler(MessageHandler(filters.StatusUpdate.NEW_CHAT_MEMBERS, new_members))
|
||||
application.add_handler(ChatMemberHandler(my_chat_member, ChatMemberHandler.MY_CHAT_MEMBER))
|
||||
application.add_handler(InlineQueryHandler(inline_query))
|
||||
# Debug raw messages (placed last with low priority)
|
||||
application.add_handler(MessageHandler(filters.ALL, debug_all_messages), group=100)
|
||||
|
||||
@@ -336,7 +405,7 @@ def main():
|
||||
|
||||
application.run_polling(
|
||||
stop_signals=(signal.SIGINT, signal.SIGTERM),
|
||||
allowed_updates=["message", "chat_member", "my_chat_member"],
|
||||
allowed_updates=["message", "chat_member", "my_chat_member", "inline_query"],
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user