Add command to get chat id

This commit is contained in:
Gabriel Augendre 2022-09-22 21:11:07 +01:00
parent b190913e6e
commit 36f74681b2

View file

@ -1,3 +1,4 @@
import asyncio
import configparser import configparser
import os import os
import sys import sys
@ -5,6 +6,7 @@ from dataclasses import dataclass
from typing import Optional from typing import Optional
import click import click
import telegram
DEFAULT_CONFIG_DIR = click.get_app_dir("ofx_processor") DEFAULT_CONFIG_DIR = click.get_app_dir("ofx_processor")
DEFAULT_CONFIG_FILENAME = "config.ini" DEFAULT_CONFIG_FILENAME = "config.ini"
@ -54,6 +56,21 @@ def show_file_name():
click.echo(config_file) click.echo(config_file)
@config.command("telegram", help="Display the bot's chat ID.")
def print_telegram_chat_id():
config = get_config("DEFAULT")
click.pause("Please start a conversation with your bot, then press any key...")
asyncio.run(print_chat_id(config))
async def print_chat_id(config: "Config"):
bot = telegram.Bot(config.telegram_bot_token)
async with bot:
update = (await bot.get_updates())[-1]
chat_id = update.message.chat_id
click.secho(f"Your chat ID is: {chat_id}", fg="green", bold=True)
@dataclass(frozen=True) @dataclass(frozen=True)
class Config: class Config:
account: str account: str
@ -125,7 +142,10 @@ def get_config(account: str) -> Config:
section = config[account] section = config[account]
budget_id = section["budget"] budget_id = section["budget"]
token = section["token"] token = section["token"]
account = section["account"] if account == "DEFAULT":
ynab_account_id = ""
else:
ynab_account_id = section["account"]
bank_identifier = section.get("bank_identifier") bank_identifier = section.get("bank_identifier")
bank_password = section.get("bank_password") bank_password = section.get("bank_password")
mailgun_api_key = section.get("mailgun_api_key") mailgun_api_key = section.get("mailgun_api_key")
@ -140,7 +160,7 @@ def get_config(account: str) -> Config:
return handle_config_file_error(config_file, e) return handle_config_file_error(config_file, e)
return Config( return Config(
account, ynab_account_id,
budget_id, budget_id,
token, token,
bank_identifier, bank_identifier,