mirror of
https://github.com/civsocit/olgram.git
synced 2023-07-22 01:29:12 +03:00
threads first iteration
This commit is contained in:
parent
02df39c9fd
commit
d4582d9a9d
@ -91,6 +91,11 @@ class ServerSettings(AbstractSettings):
|
||||
def redis_timeout_ms(cls) -> ty.Optional[int]:
|
||||
return int(timedelta(days=14).total_seconds() * 1000.0)
|
||||
|
||||
@classmethod
|
||||
@lru_cache
|
||||
def thread_timeout_ms(cls) -> int:
|
||||
return int(timedelta(days=1).total_seconds() * 1000.0)
|
||||
|
||||
|
||||
logging.basicConfig(level=os.environ.get("LOGLEVEL", "WARNING"))
|
||||
|
||||
|
@ -30,6 +30,10 @@ def _message_unique_id(bot_id: int, message_id: int) -> str:
|
||||
return f"{bot_id}_{message_id}"
|
||||
|
||||
|
||||
def _thread_uniqie_id(bot_id: int, chat_id: int) -> str:
|
||||
return f"thread_{bot_id}_{chat_id}"
|
||||
|
||||
|
||||
async def message_handler(message: types.Message, *args, **kwargs):
|
||||
_logger.info("message handler")
|
||||
bot = db_bot_instance.get()
|
||||
@ -40,6 +44,7 @@ async def message_handler(message: types.Message, *args, **kwargs):
|
||||
text=bot.start_text + ServerSettings.append_text())
|
||||
|
||||
super_chat_id = await bot.super_chat_id()
|
||||
is_super_group = super_chat_id < 0
|
||||
|
||||
if message.chat.id != super_chat_id:
|
||||
# Это обычный чат
|
||||
@ -50,8 +55,20 @@ async def message_handler(message: types.Message, *args, **kwargs):
|
||||
return SendMessage(chat_id=message.chat.id,
|
||||
text="Вы заблокированы в этом боте")
|
||||
|
||||
if is_super_group:
|
||||
thread_first_message = await _redis.get(_thread_uniqie_id(bot.pk, message.chat.id))
|
||||
if thread_first_message:
|
||||
# переслать в супер-чат, отвечая на предыдущее сообщение
|
||||
new_message = await message.copy_to(super_chat_id, reply_to_message_id=thread_first_message)
|
||||
else:
|
||||
# переслать супер-чат
|
||||
new_message = await message.forward(super_chat_id)
|
||||
await _redis.set(_thread_uniqie_id(bot.pk, message.chat.id), new_message.message_id,
|
||||
pexpire=ServerSettings.thread_timeout_ms())
|
||||
else:
|
||||
# сообщение нужно переслать в супер-чат
|
||||
new_message = await message.forward(super_chat_id)
|
||||
|
||||
await _redis.set(_message_unique_id(bot.pk, new_message.message_id), message.chat.id,
|
||||
pexpire=ServerSettings.redis_timeout_ms())
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user