mm-telegram 0.1.2__py3-none-any.whl → 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mm_telegram/__init__.py +4 -1
- mm_telegram/bot.py +81 -0
- mm_telegram-0.2.0.dist-info/METADATA +6 -0
- mm_telegram-0.2.0.dist-info/RECORD +7 -0
- mm_telegram-0.1.2.dist-info/METADATA +0 -5
- mm_telegram-0.1.2.dist-info/RECORD +0 -6
- /mm_telegram/{simple_message.py → message.py} +0 -0
- {mm_telegram-0.1.2.dist-info → mm_telegram-0.2.0.dist-info}/WHEEL +0 -0
mm_telegram/__init__.py
CHANGED
mm_telegram/bot.py
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
import logging
|
2
|
+
from typing import Any
|
3
|
+
|
4
|
+
from telegram import Update
|
5
|
+
from telegram.ext import (
|
6
|
+
Application,
|
7
|
+
ApplicationBuilder,
|
8
|
+
ApplicationHandlerStop,
|
9
|
+
BaseHandler,
|
10
|
+
CallbackContext,
|
11
|
+
CommandHandler,
|
12
|
+
ContextTypes,
|
13
|
+
ExtBot,
|
14
|
+
MessageHandler,
|
15
|
+
filters,
|
16
|
+
)
|
17
|
+
|
18
|
+
logger = logging.getLogger(__name__)
|
19
|
+
|
20
|
+
|
21
|
+
type TelegramHandler = BaseHandler[Any, CallbackContext[ExtBot[None], dict[Any, Any], dict[Any, Any], dict[Any, Any]], Any]
|
22
|
+
|
23
|
+
|
24
|
+
async def ping(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
25
|
+
if update.effective_chat is not None:
|
26
|
+
await context.bot.send_message(chat_id=update.effective_chat.id, text="pong")
|
27
|
+
|
28
|
+
|
29
|
+
async def unknown(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
30
|
+
if update.effective_chat is not None:
|
31
|
+
await context.bot.send_message(chat_id=update.effective_chat.id, text="Sorry, I didn't understand that command.")
|
32
|
+
|
33
|
+
|
34
|
+
async def is_admin(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
35
|
+
admins: list[int] = context.bot_data.get("admins", [])
|
36
|
+
|
37
|
+
if update.effective_user is None or update.message is None:
|
38
|
+
raise ApplicationHandlerStop
|
39
|
+
|
40
|
+
if update.effective_user.id not in admins:
|
41
|
+
logger.warning("is not admin", extra={"telegram_user_id": update.effective_user.id})
|
42
|
+
await update.message.reply_text("Who are you?")
|
43
|
+
raise ApplicationHandlerStop
|
44
|
+
|
45
|
+
|
46
|
+
class TelegramBot:
|
47
|
+
app: Application[Any, Any, Any, Any, Any, Any] | None
|
48
|
+
|
49
|
+
def __init__(self, handlers: list[TelegramHandler], bot_data: dict[str, object]) -> None:
|
50
|
+
self.handlers = handlers
|
51
|
+
self.bot_data = bot_data
|
52
|
+
self.app = None
|
53
|
+
|
54
|
+
async def start(self, token: str, admins: list[int]) -> None:
|
55
|
+
if not admins:
|
56
|
+
raise ValueError("No admins provided")
|
57
|
+
logger.debug("Starting telegram bot...")
|
58
|
+
app = ApplicationBuilder().token(token).build()
|
59
|
+
for key, value in self.bot_data.items():
|
60
|
+
app.bot_data[key] = value
|
61
|
+
app.bot_data["admins"] = admins
|
62
|
+
|
63
|
+
for handler in self.handlers:
|
64
|
+
app.add_handler(handler)
|
65
|
+
|
66
|
+
app.add_handler(CommandHandler("ping", ping))
|
67
|
+
app.add_handler(MessageHandler(filters.COMMAND, unknown))
|
68
|
+
|
69
|
+
await app.initialize()
|
70
|
+
await app.start()
|
71
|
+
if app.updater is not None:
|
72
|
+
await app.updater.start_polling()
|
73
|
+
logger.debug("Telegram bot started.")
|
74
|
+
|
75
|
+
self.app = app
|
76
|
+
|
77
|
+
async def shutdown(self) -> None:
|
78
|
+
if self.app is not None:
|
79
|
+
await self.app.shutdown()
|
80
|
+
self.app = None
|
81
|
+
logger.debug("Telegram bot stopped.")
|
@@ -0,0 +1,7 @@
|
|
1
|
+
mm_telegram/__init__.py,sha256=MfDt-Y42ew8LaQW34MElvgrpkPcs_RWCW_coQSyFYOE,142
|
2
|
+
mm_telegram/bot.py,sha256=ZQeKONCiFkmuinCpgvbfYc1clvd2lB26YR9NAuL8eMs,2614
|
3
|
+
mm_telegram/message.py,sha256=nbBWG58PjKU74NYqxwMANBLax0HJqgJ0B6r3FOAeVxg,2285
|
4
|
+
mm_telegram/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
5
|
+
mm_telegram-0.2.0.dist-info/METADATA,sha256=o0WxxaffFumRugRsp9zfbrXpNy8-fUn0QipGaCupXjw,150
|
6
|
+
mm_telegram-0.2.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
7
|
+
mm_telegram-0.2.0.dist-info/RECORD,,
|
@@ -1,6 +0,0 @@
|
|
1
|
-
mm_telegram/__init__.py,sha256=Nl5I50id12u28Q0H_uHziyd2t_bjySeZAH1etBOVW3A,57
|
2
|
-
mm_telegram/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
|
-
mm_telegram/simple_message.py,sha256=nbBWG58PjKU74NYqxwMANBLax0HJqgJ0B6r3FOAeVxg,2285
|
4
|
-
mm_telegram-0.1.2.dist-info/METADATA,sha256=SX9nfEVX-V_8fdzTahfrY_RuDRMpNR0yq_BXugWXDd0,109
|
5
|
-
mm_telegram-0.1.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
6
|
-
mm_telegram-0.1.2.dist-info/RECORD,,
|
File without changes
|
File without changes
|