|
1 | | -from logging import getLevelNamesMapping |
| 1 | +from contextlib import suppress |
2 | 2 | from os import getenv |
3 | 3 | from pathlib import Path |
4 | 4 |
|
5 | 5 | import click |
| 6 | +from aiogram import Bot |
6 | 7 | from aiogram.enums import ParseMode |
7 | | -from click import Choice |
| 8 | +from click import Choice, style |
8 | 9 | from watchfiles import run_process |
9 | 10 | from watchfiles.main import FileChange |
10 | 11 |
|
11 | 12 | from aiogram_cli.develop._polling import start_polling |
12 | 13 | from aiogram_cli.develop._webhook import start_webhook |
| 14 | +from aiogram_cli.wraps import async_command |
| 15 | + |
| 16 | +LOGGING_LEVELS = [ |
| 17 | + "CRITICAL", |
| 18 | + "FATAL", |
| 19 | + "ERROR", |
| 20 | + "WARN", |
| 21 | + "WARNING", |
| 22 | + "INFO", |
| 23 | + "DEBUG", |
| 24 | + "NOTSET", |
| 25 | +] |
13 | 26 |
|
14 | 27 |
|
15 | 28 | @click.group("run") |
@@ -53,7 +66,7 @@ def develop_runner(): |
53 | 66 | @click.option( |
54 | 67 | "--log-level", |
55 | 68 | "-l", |
56 | | - type=Choice(list(getLevelNamesMapping().keys())), |
| 69 | + type=Choice(LOGGING_LEVELS, case_sensitive=False), |
57 | 70 | help="Logging level", |
58 | 71 | ) |
59 | 72 | @click.option( |
@@ -103,15 +116,16 @@ def command_polling( |
103 | 116 | "log_level": log_level, |
104 | 117 | "log_format": log_format, |
105 | 118 | } |
106 | | - if reload: |
107 | | - run_process( |
108 | | - *reload_path, |
109 | | - target=start_polling, |
110 | | - kwargs=kwargs, |
111 | | - callback=_changes_detected, |
112 | | - ) |
113 | | - else: |
114 | | - return start_polling(**kwargs) |
| 119 | + with suppress(KeyboardInterrupt): |
| 120 | + if reload: |
| 121 | + run_process( |
| 122 | + *reload_path, |
| 123 | + target=start_polling, |
| 124 | + kwargs=kwargs, |
| 125 | + callback=_changes_detected, |
| 126 | + ) |
| 127 | + else: |
| 128 | + return start_polling(**kwargs) |
115 | 129 |
|
116 | 130 |
|
117 | 131 | @develop_runner.command("webhook") |
@@ -183,7 +197,7 @@ def command_polling( |
183 | 197 | @click.option( |
184 | 198 | "--log-level", |
185 | 199 | "-l", |
186 | | - type=Choice(list(getLevelNamesMapping().keys())), |
| 200 | + type=Choice(LOGGING_LEVELS, case_sensitive=False), |
187 | 201 | help="Logging level", |
188 | 202 | ) |
189 | 203 | @click.option( |
@@ -247,17 +261,64 @@ def command_webhook( |
247 | 261 | "log_level": log_level, |
248 | 262 | "log_format": log_format, |
249 | 263 | } |
250 | | - if reload: |
251 | | - run_process( |
252 | | - *reload_path, |
253 | | - target=start_webhook, |
254 | | - kwargs=kwargs, |
255 | | - callback=_changes_detected, |
256 | | - ) |
257 | | - else: |
258 | | - return start_webhook(**kwargs) |
| 264 | + with suppress(KeyboardInterrupt): |
| 265 | + if reload: |
| 266 | + run_process( |
| 267 | + *reload_path, |
| 268 | + target=start_webhook, |
| 269 | + kwargs=kwargs, |
| 270 | + callback=_changes_detected, |
| 271 | + ) |
| 272 | + else: |
| 273 | + return start_webhook(**kwargs) |
259 | 274 |
|
260 | 275 |
|
261 | 276 | def _changes_detected(changes: set[FileChange]): |
262 | 277 | files = {path for change, path in changes} |
263 | 278 | click.echo(f"Changes detected: {files}") |
| 279 | + |
| 280 | + |
| 281 | +@develop_runner.command("info") |
| 282 | +@click.option( |
| 283 | + "--token", |
| 284 | + "-t", |
| 285 | + help="Bot token", |
| 286 | + required=True, |
| 287 | + default=getenv("TELEGRAM_TOKEN"), |
| 288 | +) |
| 289 | +@async_command |
| 290 | +async def command_info(token: str): |
| 291 | + async with Bot(token=token).context() as bot: |
| 292 | + me = await bot.get_me() |
| 293 | + webhook_info = await bot.get_webhook_info() |
| 294 | + |
| 295 | + click.echo(style("Bot info:", fg="green", bold=True)) |
| 296 | + click.echo(" " + style("ID: ", bold=True, fg="green") + str(me.id)) |
| 297 | + click.echo(" " + style("Username: ", bold=True, fg="green") + f"@{me.username} (https://t.me/{me.username})") |
| 298 | + if me.is_premium: |
| 299 | + click.echo(" " + style("Premium: ", bold=True, fg="green") + "Yes") |
| 300 | + if webhook_info.url: |
| 301 | + click.echo( |
| 302 | + style("Webhook info:", fg="green", bold=True), |
| 303 | + ) |
| 304 | + click.echo(" " + style("URL: ", bold=True, fg="green") + webhook_info.url) |
| 305 | + if webhook_info.allowed_updates: |
| 306 | + click.echo(" " + style("Allowed updates: ", bold=True, fg="green") + ", ".join(webhook_info.allowed_updates)) |
| 307 | + if webhook_info.ip_address: |
| 308 | + click.echo(" " + style("IP address: ", bold=True, fg="green") + webhook_info.ip_address) |
| 309 | + if webhook_info.has_custom_certificate: |
| 310 | + click.echo(" " + style("Has custom certificate: ", bold=True, fg="green") + "Yes") |
| 311 | + if webhook_info.pending_update_count: |
| 312 | + click.echo(" " + style("Pending updates: ", bold=True, fg="green") + str(webhook_info.pending_update_count)) |
| 313 | + if webhook_info.last_error_date: |
| 314 | + click.echo(" " + style("Last error date: ", bold=True, fg="green") + str(webhook_info.last_error_date)) |
| 315 | + if webhook_info.last_error_message: |
| 316 | + click.echo(" " + style("Last error message: ", bold=True, fg="green") + webhook_info.last_error_message) |
| 317 | + if webhook_info.max_connections: |
| 318 | + click.echo(" " + style("Max connections: ", bold=True, fg="green") + str(webhook_info.max_connections)) |
| 319 | + if webhook_info.last_synchronization_error_date: |
| 320 | + click.echo( |
| 321 | + " " |
| 322 | + + style("Last synchronization error date: ", bold=True, fg="green") |
| 323 | + + str(webhook_info.last_synchronization_error_date) |
| 324 | + ) |
0 commit comments