-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
444 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
from abc import ABC | ||
|
||
from sulguk.render import State | ||
|
||
from .base import Entity | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
from dataclasses import dataclass | ||
|
||
from sulguk.render import State | ||
|
||
from .base import Entity | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
from dataclasses import dataclass | ||
|
||
from sulguk.render import State | ||
|
||
from .base import Entity | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
from dataclasses import dataclass | ||
|
||
from sulguk.render import State | ||
|
||
from .base import Entity | ||
|
||
|
||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from logging import getLogger | ||
from typing import Union | ||
|
||
from aiogram import Bot | ||
from aiogram.exceptions import TelegramBadRequest | ||
|
||
from .exceptions import ChatNotFound | ||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
async def get_chat(bot: Bot, chat_id: Union[str, int]): | ||
try: | ||
return await bot.get_chat(chat_id) | ||
except TelegramBadRequest as e: | ||
if "chat not found" in e.message: | ||
logger.error("Chat %s not found", chat_id) | ||
raise ChatNotFound | ||
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import asyncio | ||
import logging | ||
import os | ||
|
||
from aiogram import Bot | ||
|
||
from .editor import edit | ||
from .exceptions import ManagerError | ||
from .params import parse_args | ||
from .sender import send | ||
|
||
|
||
async def main(): | ||
logging.basicConfig( | ||
level=logging.INFO, | ||
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", | ||
) | ||
logging.getLogger("aiogram").setLevel(logging.WARNING) | ||
bot = Bot(token=os.getenv("BOT_TOKEN")) | ||
args = parse_args() | ||
try: | ||
if args.command == "edit": | ||
await edit(bot, args) | ||
else: | ||
await send(bot, args) | ||
except ManagerError: | ||
logging.error("There were errors during execution. See above") | ||
finally: | ||
await bot.session.close() | ||
|
||
|
||
def cli(): | ||
try: | ||
asyncio.run(main()) | ||
except (KeyboardInterrupt, SystemExit): | ||
pass | ||
|
||
|
||
if __name__ == '__main__': | ||
cli() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import logging | ||
|
||
from aiogram import Bot | ||
from aiogram.exceptions import TelegramBadRequest | ||
|
||
from .chat_info import get_chat | ||
from .file import load_file | ||
from .params import EditArgs | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
async def edit(bot: Bot, args: EditArgs): | ||
chat = await get_chat(bot, args.destination.group_id) | ||
if not args.destination.post_id: | ||
raise ValueError("No post provided to edit") | ||
if args.destination.comment_id: | ||
chat_id = chat.linked_chat_id | ||
message_id = args.destination.comment_id | ||
else: | ||
chat_id = chat.id | ||
message_id = args.destination.post_id | ||
|
||
data = load_file(args.file) | ||
try: | ||
await bot.edit_message_text( | ||
chat_id=chat_id, | ||
message_id=message_id, | ||
text=data.text, | ||
entities=data.entities, | ||
) | ||
except TelegramBadRequest as e: | ||
if "message is not modified" in e.message: | ||
logger.debug("Nothing changed") | ||
return | ||
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class ManagerError(Exception): | ||
pass | ||
|
||
|
||
class LinkedMessageNotFound(ManagerError): | ||
pass | ||
|
||
|
||
class ChatNotFound(ManagerError): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import logging | ||
|
||
from sulguk import transform_html, RenderResult | ||
from .exceptions import ManagerError | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def load_file(filename) -> RenderResult: | ||
try: | ||
with open(filename) as f: | ||
return transform_html(f.read()) | ||
except FileNotFoundError: | ||
logger.error("File `%s` not found", filename) | ||
raise ManagerError |
Oops, something went wrong.