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 pathlib import Path
|
||||||
from typing import Dict, Any, Optional
|
from typing import Dict, Any, Optional
|
||||||
|
|
||||||
from telegram import Update
|
from telegram import Update, InlineQueryResultArticle, InputTextMessageContent
|
||||||
from telegram.constants import ParseMode
|
from telegram.constants import ParseMode
|
||||||
from telegram.ext import (
|
from telegram.ext import (
|
||||||
ApplicationBuilder,
|
ApplicationBuilder,
|
||||||
@@ -16,6 +16,7 @@ from telegram.ext import (
|
|||||||
MessageHandler,
|
MessageHandler,
|
||||||
filters,
|
filters,
|
||||||
ChatMemberHandler,
|
ChatMemberHandler,
|
||||||
|
InlineQueryHandler,
|
||||||
)
|
)
|
||||||
|
|
||||||
DATA_DIR = Path(os.environ.get("DATA_DIR", "/data"))
|
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:
|
except Exception:
|
||||||
pass
|
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 ----------------------
|
# ---------------------- Group Events ----------------------
|
||||||
|
|
||||||
@@ -325,6 +393,7 @@ def main():
|
|||||||
# Group / membership events
|
# Group / membership events
|
||||||
application.add_handler(MessageHandler(filters.StatusUpdate.NEW_CHAT_MEMBERS, new_members))
|
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(ChatMemberHandler(my_chat_member, ChatMemberHandler.MY_CHAT_MEMBER))
|
||||||
|
application.add_handler(InlineQueryHandler(inline_query))
|
||||||
# Debug raw messages (placed last with low priority)
|
# Debug raw messages (placed last with low priority)
|
||||||
application.add_handler(MessageHandler(filters.ALL, debug_all_messages), group=100)
|
application.add_handler(MessageHandler(filters.ALL, debug_all_messages), group=100)
|
||||||
|
|
||||||
@@ -336,7 +405,7 @@ def main():
|
|||||||
|
|
||||||
application.run_polling(
|
application.run_polling(
|
||||||
stop_signals=(signal.SIGINT, signal.SIGTERM),
|
stop_signals=(signal.SIGINT, signal.SIGTERM),
|
||||||
allowed_updates=["message", "chat_member", "my_chat_member"],
|
allowed_updates=["message", "chat_member", "my_chat_member", "inline_query"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,30 +1,30 @@
|
|||||||
version: '3.9'
|
version: '3.9'
|
||||||
services:
|
services:
|
||||||
counterbot:
|
counterbot:
|
||||||
# Nutzt vorgebautes Image aus Registry
|
# Nutzt vorgebautes Image aus Registry
|
||||||
image: git.beging.de/troogs/gigalativbot:latest
|
image: git.beging.de/troogs/gigalativbot:latest
|
||||||
container_name: counterbot
|
container_name: counterbot
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
# Entweder environment Schlüssel direkt setzen oder eine .env Datei mit docker compose verwenden.
|
# Entweder environment Schlüssel direkt setzen oder eine .env Datei mit docker compose verwenden.
|
||||||
environment:
|
environment:
|
||||||
# Liefert das Telegram Bot Token (nicht in Git committen). Kann auch via .env Datei bereitgestellt werden.
|
# Liefert das Telegram Bot Token (nicht in Git committen). Kann auch via .env Datei bereitgestellt werden.
|
||||||
- BOT_TOKEN=${BOT_TOKEN}
|
- BOT_TOKEN=${BOT_TOKEN}
|
||||||
# Optional: Startup Ankündigung an kommagetrennte Chat IDs (z.B. -4549916385)
|
# Optional: Startup Ankündigung an kommagetrennte Chat IDs (z.B. -4549916385)
|
||||||
- STARTUP_ANNOUNCE_CHAT_IDS=${STARTUP_ANNOUNCE_CHAT_IDS:-}
|
- STARTUP_ANNOUNCE_CHAT_IDS=${STARTUP_ANNOUNCE_CHAT_IDS:-}
|
||||||
# Optional: Begrüßung aller neuen Mitglieder (true/false)
|
# Optional: Begrüßung aller neuen Mitglieder (true/false)
|
||||||
- ANNOUNCE_ALL_JOINS=${ANNOUNCE_ALL_JOINS:-false}
|
- ANNOUNCE_ALL_JOINS=${ANNOUNCE_ALL_JOINS:-false}
|
||||||
# Standard Pfade
|
# Standard Pfade
|
||||||
- CONFIG_FILE=/app/config.yaml
|
- CONFIG_FILE=/app/config.yaml
|
||||||
- DATA_DIR=/data
|
- DATA_DIR=/data
|
||||||
volumes:
|
volumes:
|
||||||
- counterbot_data:/data
|
- counterbot_data:/data
|
||||||
# Optional: eigene angepasste config.yaml aus Host einbinden
|
# Optional: eigene angepasste config.yaml aus Host einbinden
|
||||||
# - ./config.yaml:/app/config.yaml:ro
|
# - ./config.yaml:/app/config.yaml:ro
|
||||||
# Keine Ports nötig bei Long Polling
|
# Keine Ports nötig bei Long Polling
|
||||||
# healthcheck:
|
# healthcheck:
|
||||||
# test: ["CMD", "python", "-c", "import os,sys; sys.exit(0)"]
|
# test: ["CMD", "python", "-c", "import os,sys; sys.exit(0)"]
|
||||||
# interval: 1m
|
# interval: 1m
|
||||||
# timeout: 5s
|
# timeout: 5s
|
||||||
# retries: 3
|
# retries: 3
|
||||||
volumes:
|
volumes:
|
||||||
counterbot_data:
|
counterbot_data:
|
||||||
|
|||||||
Reference in New Issue
Block a user