1
0
mirror of https://github.com/civsocit/olgram.git synced 2025-12-16 00:46: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,6 @@ COPY . /app
WORKDIR /app
RUN pip install --upgrade pip && \
pip install -r requirements.txt && \
pip install -r requirements.txt
CMD ["python", "instance"]
CMD ["python", "bot.py"]

View File

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())

View File

@@ -0,0 +1,23 @@
version: '3'
services:
redis:
restart: unless-stopped
image: 'bitnami/redis:latest'
environment:
- ALLOW_EMPTY_PASSWORD=yes
volumes:
- redis-db:/bitnami/redis/data
env_file:
- .env
instance:
build: .
restart: unless-stopped
depends_on:
- redis
env_file:
- .env
environment:
- INSTANCE_REDIS_PATH=redis://redis
volumes:
redis-db:

View File

@@ -0,0 +1,3 @@
aiogram
python-dotenv
aioredis

46
instance/settings.py Normal file
View File

@@ -0,0 +1,46 @@
from dotenv import load_dotenv
import os
load_dotenv()
class InstanceSettings:
@classmethod
def _get_env(cls, parameter: str) -> str:
parameter = os.getenv(parameter, None)
if not parameter:
raise ValueError(f"{parameter} not defined in ENV")
return parameter
@classmethod
def token(cls) -> str:
"""
Token instance бота
:return:
"""
return cls._get_env("INSTANCE_TOKEN")
@classmethod
def super_chat_id(cls) -> int:
"""
ID чата, в который бот пересылает сообщения
Это может быть личный чат (ID > 0) или общий чат (ID < 0)
:return:
"""
return int(cls._get_env("INSTANCE_SUPER_CHAT_ID"))
@classmethod
def start_text(cls) -> str:
"""
Этот текст будет отправляться пользователю по команде /start
:return:
"""
return cls._get_env("INSTANCE_START_TEXT")
@classmethod
def redis_path(cls) -> str:
"""
Путь до БД redis
:return:
"""
return cls._get_env("INSTANCE_REDIS_PATH")