1
0
mirror of https://github.com/civsocit/olgram.git synced 2025-12-15 08:16:17 +00:00

Независимый проект Instance

This commit is contained in:
mihalin
2021-07-03 12:56:59 +03:00
parent 60bb00bcc9
commit c5e0192d24
9 changed files with 123 additions and 22 deletions

View File

@@ -5,6 +5,8 @@ import aioredis
from aiogram import Dispatcher, types, exceptions
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from settings import InstanceSettings
class BotInstance:
def __init__(self, token: str, super_chat_id: int, start_text: str,
@@ -26,12 +28,22 @@ class BotInstance:
self._dp.stop_polling()
async def start_polling(self):
self._redis = await aioredis.create_redis_pool('redis://localhost:6370')
self._redis = await aioredis.create_redis_pool(InstanceSettings.redis_path())
bot = aiogram.Bot(self._token)
self._dp = Dispatcher(bot, storage=MemoryStorage())
self._dp.register_message_handler(self._receive_text, content_types=[types.ContentType.TEXT])
# Здесь перечислены все типы сообщений, которые бот должен пересылать
self._dp.register_message_handler(self._receive_message, content_types=[types.ContentType.TEXT,
types.ContentType.CONTACT,
types.ContentType.ANIMATION,
types.ContentType.AUDIO,
types.ContentType.DOCUMENT,
types.ContentType.PHOTO,
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])
@@ -55,46 +67,50 @@ class BotInstance:
if message.left_chat_member.id == message.bot.id:
await self._left_callback(self._identify, message)
async def _receive_text(self, message: types.Message):
async def _receive_message(self, message: types.Message):
"""
Some text received
Получено обычное сообщение, вероятно, для пересыла в другой чат
:param message:
:return:
"""
if message.text and message.text.startswith("/start"):
# На команду start нужно ответить, не пересылая сообщение никуда
await message.answer(self._start_text)
return
if message.chat.id != self._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)
else:
# Это чат, в который бот должен пересылать сообщения
# Это супер-чат
if message.reply_to_message:
# Ответ из супер-чата переслать тому пользователю,
chat_id = await self._redis.get(self._message_unique_id(message.reply_to_message.message_id))
if not chat_id:
chat_id = message.reply_to_message.forward_from_chat
if not chat_id:
await message.reply("Невозможно ответить, автор сообщения не найден")
await message.reply("Невозможно переслать сообщение: автор не найден")
return
chat_id = int(chat_id)
try:
await message.copy_to(chat_id)
except exceptions.MessageError:
await message.reply("Невозможно отправить сообщение пользователю: возможно, он заблокировал бота")
await message.reply("Невозможно переслать сообщение: возможно, автор заблокировал бота")
return
else:
await message.forward(self._super_chat_id)
if __name__ == '__main__':
# Single instance mode
import os
"""
Режим single-instance. В этом режиме не работает olgram. На сервере запускается только один feedback (instance)
бот для пересылки сообщений. Все настройки этого бота задаются в переменных окружения на сервере. Бот работает
в режиме polling
"""
bot = BotInstance(
os.getenv("TOKEN"),
int(os.getenv("CHAT_ID")),
os.getenv("START_TEXT")
InstanceSettings.token(),
InstanceSettings.super_chat_id(),
InstanceSettings.start_text()
)
asyncio.get_event_loop().run_until_complete(bot.start_polling())