diff options
| author | user <user@node5.net> | 2024-02-27 20:25:42 +0100 |
|---|---|---|
| committer | user <user@node5.net> | 2024-02-27 20:25:42 +0100 |
| commit | efeb7e086c5b8ba45745851223d6b26962d0740a (patch) | |
| tree | 2da6617586a6baa7f4871bcda2f80032da0254f2 /src | |
| parent | 730c3e19e39c1245eb1d44536fa8b5db1ce1f563 (diff) | |
telegram bot on comment
Diffstat (limited to 'src')
| -rw-r--r-- | src/blog_node5_net.py | 2 | ||||
| -rw-r--r-- | src/telegram_handler.py | 27 |
2 files changed, 29 insertions, 0 deletions
diff --git a/src/blog_node5_net.py b/src/blog_node5_net.py index e4bde5d..4508109 100644 --- a/src/blog_node5_net.py +++ b/src/blog_node5_net.py @@ -9,6 +9,7 @@ import yaml import article_handler import db_handler +import telegram_handler logging.basicConfig(format='%(asctime)s,%(msecs)03d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s', datefmt='%Y-%m-%d:%H:%M:%S', level=logging.DEBUG) @@ -44,6 +45,7 @@ def strip_trailing_slash(path): def post_comment(): args = {'page_url': strip_trailing_slash(urllib.parse.unquote(urllib.parse.urlparse(flask.request.referrer).path)), 'public': False, 'show_contact': False} + telegram_handler.send_message(flask.request.form['comment']) for key, value in flask.request.form.items(): if key in ('username', 'visitor_url', 'contact', 'show_contact', 'public', 'comment'): if key in ('public', 'show_contact'): diff --git a/src/telegram_handler.py b/src/telegram_handler.py new file mode 100644 index 0000000..0484e9b --- /dev/null +++ b/src/telegram_handler.py @@ -0,0 +1,27 @@ +import asyncio +import os + +import telegram +import yaml + +with open(os.path.join('configs', 'telegram.yml'), 'r') as file: + config = yaml.safe_load(file.read()) + +bot = telegram.Bot(config['token']) + + +def send_message(text): + asyncio.run(async_send_message(text)) + + +async def async_send_message(text): + async with bot: + await bot.send_message(text=text, chat_id=config['chat_id']) + + +async def init_bot(): + async with bot: + print(await bot.get_me()) + + +asyncio.run(init_bot()) |
