mirror of
https://github.com/civsocit/olgram.git
synced 2025-12-15 08:06:17 +00:00
Миграции
This commit is contained in:
@@ -1,36 +1,43 @@
|
||||
import asyncio
|
||||
import typing as ty
|
||||
import aiogram
|
||||
import aioredis
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass
|
||||
from aiogram import Dispatcher, types, exceptions
|
||||
from aiogram.contrib.fsm_storage.memory import MemoryStorage
|
||||
|
||||
from settings import InstanceSettings
|
||||
try:
|
||||
from settings import InstanceSettings
|
||||
except ModuleNotFoundError:
|
||||
from .settings import InstanceSettings
|
||||
|
||||
|
||||
class BotInstance:
|
||||
def __init__(self, token: str, super_chat_id: int, start_text: str,
|
||||
invite_callback: ty.Optional[ty.Callable] = None,
|
||||
left_callback: ty.Optional[ty.Callable] = None,
|
||||
identify: ty.Optional[int] = None):
|
||||
self._token = token
|
||||
self._bot_id = self._token.split(":")[0]
|
||||
self._super_chat_id = super_chat_id
|
||||
self._start_text = start_text
|
||||
@dataclass()
|
||||
class BotProperties:
|
||||
token: str
|
||||
start_text: str
|
||||
bot_id: int
|
||||
super_chat_id: int
|
||||
|
||||
|
||||
class BotInstance(ABC):
|
||||
def __init__(self):
|
||||
self._redis: aioredis.Redis = None
|
||||
self._dp: aiogram.Dispatcher = None
|
||||
self._identify = identify
|
||||
|
||||
self._invite_callback = invite_callback
|
||||
self._left_callback = left_callback
|
||||
@abstractmethod
|
||||
async def _properties(self) -> BotProperties:
|
||||
raise NotImplemented()
|
||||
|
||||
def stop_polling(self):
|
||||
self._dp.stop_polling()
|
||||
|
||||
async def start_polling(self):
|
||||
async def _setup(self):
|
||||
self._redis = await aioredis.create_redis_pool(InstanceSettings.redis_path())
|
||||
|
||||
bot = aiogram.Bot(self._token)
|
||||
props = await self._properties()
|
||||
|
||||
bot = aiogram.Bot(props.token)
|
||||
self._dp = Dispatcher(bot, storage=MemoryStorage())
|
||||
|
||||
# Здесь перечислены все типы сообщений, которые бот должен пересылать
|
||||
@@ -43,29 +50,14 @@ class BotInstance:
|
||||
types.ContentType.STICKER,
|
||||
types.ContentType.VIDEO,
|
||||
types.ContentType.VOICE])
|
||||
# Callback-и на добавление бота в чат и удаление бота из чата
|
||||
self._dp.register_message_handler(self._receive_invite, content_types=[types.ContentType.NEW_CHAT_MEMBERS])
|
||||
self._dp.register_message_handler(self._receive_left, content_types=[types.ContentType.LEFT_CHAT_MEMBER])
|
||||
|
||||
async def start_polling(self):
|
||||
await self._setup()
|
||||
await self._dp.start_polling()
|
||||
|
||||
def _message_unique_id(self, message_id) -> str:
|
||||
return self._bot_id + "-" + str(message_id)
|
||||
|
||||
async def _receive_invite(self, message: types.Message):
|
||||
if not self._invite_callback:
|
||||
return
|
||||
|
||||
for member in message.new_chat_members:
|
||||
if member.id == message.bot.id:
|
||||
await self._invite_callback(self._identify, message)
|
||||
|
||||
async def _receive_left(self, message: types.Message):
|
||||
if not self._left_callback:
|
||||
return
|
||||
|
||||
if message.left_chat_member.id == message.bot.id:
|
||||
await self._left_callback(self._identify, message)
|
||||
@classmethod
|
||||
def _message_unique_id(cls, bot_id: int, message_id: int) -> str:
|
||||
return f"{bot_id}_{message_id}"
|
||||
|
||||
async def _receive_message(self, message: types.Message):
|
||||
"""
|
||||
@@ -73,20 +65,23 @@ class BotInstance:
|
||||
:param message:
|
||||
:return:
|
||||
"""
|
||||
props = await self._properties()
|
||||
if message.text and message.text.startswith("/start"):
|
||||
# На команду start нужно ответить, не пересылая сообщение никуда
|
||||
await message.answer(self._start_text)
|
||||
await message.answer(props.start_text)
|
||||
return
|
||||
|
||||
if message.chat.id != self._super_chat_id:
|
||||
if message.chat.id != props.super_chat_id:
|
||||
# Это обычный чат: сообщение нужно переслать в супер-чат
|
||||
new_message = await message.forward(self._super_chat_id)
|
||||
await self._redis.set(self._message_unique_id(new_message.message_id), message.chat.id)
|
||||
new_message = await message.forward(props.super_chat_id)
|
||||
await self._redis.set(self._message_unique_id(props.bot_id, new_message.message_id),
|
||||
message.chat.id)
|
||||
else:
|
||||
# Это супер-чат
|
||||
if message.reply_to_message:
|
||||
# Ответ из супер-чата переслать тому пользователю,
|
||||
chat_id = await self._redis.get(self._message_unique_id(message.reply_to_message.message_id))
|
||||
chat_id = await self._redis.get(
|
||||
self._message_unique_id(props.bot_id, message.reply_to_message.message_id))
|
||||
if not chat_id:
|
||||
chat_id = message.reply_to_message.forward_from_chat
|
||||
if not chat_id:
|
||||
@@ -99,7 +94,17 @@ class BotInstance:
|
||||
await message.reply("Невозможно переслать сообщение: возможно, автор заблокировал бота")
|
||||
return
|
||||
else:
|
||||
await message.forward(self._super_chat_id)
|
||||
await message.forward(props.super_chat_id)
|
||||
|
||||
|
||||
class FreezeBotInstance(BotInstance):
|
||||
def __init__(self, token: str, start_text: str, super_chat_id: int):
|
||||
super().__init__()
|
||||
|
||||
self._props = BotProperties(token, start_text, int(token.split(":")[0]), super_chat_id)
|
||||
|
||||
async def _properties(self) -> BotProperties:
|
||||
return self._props
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
@@ -108,9 +113,9 @@ if __name__ == '__main__':
|
||||
бот для пересылки сообщений. Все настройки этого бота задаются в переменных окружения на сервере. Бот работает
|
||||
в режиме polling
|
||||
"""
|
||||
bot = BotInstance(
|
||||
bot = FreezeBotInstance(
|
||||
InstanceSettings.token(),
|
||||
InstanceSettings.super_chat_id(),
|
||||
InstanceSettings.start_text()
|
||||
InstanceSettings.start_text(),
|
||||
InstanceSettings.super_chat_id()
|
||||
)
|
||||
asyncio.get_event_loop().run_until_complete(bot.start_polling())
|
||||
|
||||
Reference in New Issue
Block a user