|
1 | 1 | import logging |
2 | 2 |
|
3 | | -from bot_storage import ( |
| 3 | +from bot.bot_storage import ( |
4 | 4 | get_all_users, |
5 | 5 | get_last_sent_id, |
6 | 6 | get_news_after_id, |
7 | 7 | save_last_sent_news_id, |
8 | 8 | ) |
| 9 | +from bot.sender import send_news |
9 | 10 | from config import setup_logger |
10 | | -from db_async import AsyncSessionLocal |
| 11 | +from db.db_async import AsyncSessionLocal |
11 | 12 | from dotenv import load_dotenv |
12 | 13 | from telegram import ( |
13 | 14 | BotCommand, |
|
23 | 24 | logger = logging.getLogger(__name__) |
24 | 25 |
|
25 | 26 |
|
26 | | -MAX_CAPTION_LENGTH = 1024 |
27 | | - |
28 | | - |
29 | | -def format_message(title, text): |
30 | | - message = f"*{title}*\n\n{text}\n" |
31 | | - if len(message) > MAX_CAPTION_LENGTH: |
32 | | - message = f"*{title}*\n\n{text[:MAX_CAPTION_LENGTH]} ...✂️\n" |
33 | | - return message |
34 | | - |
35 | | - |
36 | | -async def send_news( |
37 | | - chat_id: int, context: ContextTypes.DEFAULT_TYPE, title, image, text, url |
38 | | -): |
39 | | - message = format_message(title, text) |
40 | | - keyboard = [ |
41 | | - [InlineKeyboardButton("Читать полную версию на сайте", url=url)] |
42 | | - ] |
43 | | - reply_markup = InlineKeyboardMarkup(keyboard) |
44 | | - if image: |
45 | | - try: |
46 | | - await context.bot.send_photo( |
47 | | - chat_id=chat_id, |
48 | | - photo=image, |
49 | | - caption=message, |
50 | | - parse_mode="Markdown", |
51 | | - reply_markup=reply_markup, |
52 | | - ) |
53 | | - logger.info("Новость отправлена с картинкой") |
54 | | - return |
55 | | - except Exception as e: |
56 | | - logger.error( |
57 | | - f"Не удалось отправить фото по ссылке, ошибка: {e}", |
58 | | - ) |
59 | | - try: |
60 | | - await context.bot.send_message( |
61 | | - chat_id, message, parse_mode="Markdown", reply_markup=reply_markup |
62 | | - ) |
63 | | - logger.info(f"Новость '{title[:25]}' отправлена без картинки") |
64 | | - except Exception as e: |
65 | | - logger.error( |
66 | | - f"Не удалось отправить сообщение с новостью '{title[:25]}', ошибка: {e}" |
67 | | - ) |
68 | | - |
69 | | - |
70 | 27 | async def auto_send_news(context: ContextTypes.DEFAULT_TYPE): |
71 | 28 | async with AsyncSessionLocal() as session: |
72 | 29 | users = await get_all_users(session) |
|
0 commit comments