mm-telegram 0.1.2__py3-none-any.whl → 0.3.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 CHANGED
@@ -1 +1,4 @@
1
- from .simple_message import send_message as send_message
1
+ from .bot import TelegramBot, TelegramHandler
2
+ from .message import send_message
3
+
4
+ __all__ = ["TelegramBot", "TelegramHandler", "send_message"]
mm_telegram/bot.py ADDED
@@ -0,0 +1,95 @@
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
+ """Responds with 'pong' to /ping command."""
26
+ if update.effective_chat is not None:
27
+ await context.bot.send_message(chat_id=update.effective_chat.id, text="pong")
28
+
29
+
30
+ async def unknown(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
31
+ """Handles unknown commands with a default response."""
32
+ if update.effective_chat is not None:
33
+ await context.bot.send_message(chat_id=update.effective_chat.id, text="Sorry, I didn't understand that command.")
34
+
35
+
36
+ async def is_admin(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
37
+ """Checks if user is admin and blocks access if not.
38
+
39
+ Raises ApplicationHandlerStop if user is not in the admins list.
40
+ """
41
+ admins: list[int] = context.bot_data.get("admins", [])
42
+
43
+ if update.effective_user is None or update.message is None:
44
+ raise ApplicationHandlerStop
45
+
46
+ if update.effective_user.id not in admins:
47
+ logger.warning("is not admin", extra={"telegram_user_id": update.effective_user.id})
48
+ await update.message.reply_text("Who are you?")
49
+ raise ApplicationHandlerStop
50
+
51
+
52
+ class TelegramBot:
53
+ """Telegram bot wrapper that manages application lifecycle and handlers."""
54
+
55
+ app: Application[Any, Any, Any, Any, Any, Any] | None
56
+
57
+ def __init__(self, handlers: list[TelegramHandler], bot_data: dict[str, object]) -> None:
58
+ """Initialize bot with custom handlers and initial bot data."""
59
+ self.handlers = handlers
60
+ self.bot_data = bot_data
61
+ self.app = None
62
+
63
+ async def start(self, token: str, admins: list[int]) -> None:
64
+ """Start the bot with given token and admin list.
65
+
66
+ Raises ValueError if no admins are provided.
67
+ """
68
+ if not admins:
69
+ raise ValueError("No admins provided")
70
+ logger.debug("Starting telegram bot...")
71
+ app = ApplicationBuilder().token(token).build()
72
+ for key, value in self.bot_data.items():
73
+ app.bot_data[key] = value
74
+ app.bot_data["admins"] = admins
75
+
76
+ for handler in self.handlers:
77
+ app.add_handler(handler)
78
+
79
+ app.add_handler(CommandHandler("ping", ping))
80
+ app.add_handler(MessageHandler(filters.COMMAND, unknown))
81
+
82
+ await app.initialize()
83
+ await app.start()
84
+ if app.updater is not None:
85
+ await app.updater.start_polling()
86
+ logger.debug("Telegram bot started.")
87
+
88
+ self.app = app
89
+
90
+ async def shutdown(self) -> None:
91
+ """Stop the bot and clean up resources."""
92
+ if self.app is not None:
93
+ await self.app.shutdown()
94
+ self.app = None
95
+ logger.debug("Telegram bot stopped.")
@@ -1,6 +1,7 @@
1
1
  import asyncio
2
2
 
3
- from mm_std import Result, http_request
3
+ from mm_http import http_request
4
+ from mm_result import Result
4
5
 
5
6
 
6
7
  async def send_message(
@@ -0,0 +1,7 @@
1
+ Metadata-Version: 2.4
2
+ Name: mm-telegram
3
+ Version: 0.3.0
4
+ Requires-Python: >=3.13
5
+ Requires-Dist: mm-http>=0.0.1
6
+ Requires-Dist: mm-result>=0.0.2
7
+ Requires-Dist: python-telegram-bot~=22.1
@@ -0,0 +1,7 @@
1
+ mm_telegram/__init__.py,sha256=MfDt-Y42ew8LaQW34MElvgrpkPcs_RWCW_coQSyFYOE,142
2
+ mm_telegram/bot.py,sha256=VNs2yPulCQosIe_Db9HKPQ7IAGLaVbZaoDAT0i7d3oE,3186
3
+ mm_telegram/message.py,sha256=bEJl_ctSEdTjFQbY14g3dlwZKcTef7RfaM-8Jqn4INA,2307
4
+ mm_telegram/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ mm_telegram-0.3.0.dist-info/METADATA,sha256=6vzPzU_D0XFfhmyuayP0wvaEXeWPWXSkneBJkr1lx64,182
6
+ mm_telegram-0.3.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
7
+ mm_telegram-0.3.0.dist-info/RECORD,,
@@ -1,5 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: mm-telegram
3
- Version: 0.1.2
4
- Requires-Python: >=3.12
5
- Requires-Dist: mm-std~=0.4.11
@@ -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,,