1
0
mirror of https://github.com/civsocit/olgram.git synced 2025-05-24 11:03:24 +00:00
This commit is contained in:
er8dd 2024-03-22 03:44:34 +04:00
parent d0b570baa9
commit 7ce6df7fd9
3 changed files with 61 additions and 14 deletions

View File

@ -140,6 +140,11 @@ async def mailing(bot: Bot, call: types.CallbackQuery):
await bot.save(update_fields=["enable_mailing"])
async def tags(bot: Bot, call: types.CallbackQuery):
bot.enable_tags = not bot.enable_tags
await bot.save(update_fields=["enable_tags"])
async def go_mailing(bot: Bot, context: dict) -> int:
users = await bot.mailing_users
a_bot = AioBot(bot.decrypted_token())

View File

@ -197,6 +197,12 @@ async def send_bot_settings_menu(bot: Bot, call: types.CallbackQuery):
operation="thread_interrupt",
chat=empty))
)
keyboard.insert(
types.InlineKeyboardButton(text=_("Теги"),
callback_data=menu_callback.new(level=3, bot_id=bot.id,
operation="tags",
chat=empty))
)
is_promo = await bot.is_promo()
if is_promo:
keyboard.insert(
@ -217,6 +223,7 @@ async def send_bot_settings_menu(bot: Bot, call: types.CallbackQuery):
enable_always_second_message = _("включён") if bot.enable_always_second_message else _("выключен")
thread_interrupt = _("да") if bot.enable_thread_interrupt else _("нет")
mailing_turn = _("включена") if bot.enable_mailing else _("выключена")
tags_turn = _("включены") if bot.enable_tags else _("выключены")
text = dedent(_("""
<a href="https://olgram.readthedocs.io/ru/latest/options.html#threads">Потоки сообщений</a>: <b>{0}</b>
<a href="https://olgram.readthedocs.io/ru/latest/options.html#user-info">Данные пользователя</a>: <b>{1}</b>
@ -224,8 +231,9 @@ async def send_bot_settings_menu(bot: Bot, call: types.CallbackQuery):
<a href="https://olgram.readthedocs.io/ru/latest/options.html#always_second_message">Автоответчик всегда</a>: <b>{3}</b>
<a href="https://olgram.readthedocs.io/ru/latest/options.html#thread_interrupt">Прерывать поток</a>: <b>{4}</b>
<a href="https://olgram.readthedocs.io/ru/latest/options.html#mailing">Рассылка</a>: <b>{5}</b>
Теги: <b>{6}</b>
""")).format(thread_turn, info_turn, antiflood_turn, enable_always_second_message, thread_interrupt,
mailing_turn)
mailing_turn, tags_turn)
if is_promo:
olgram_turn = _("включена") if bot.enable_olgram_text else _("выключена")
@ -672,6 +680,9 @@ async def callback(call: types.CallbackQuery, callback_data: dict, state: FSMCon
if operation == "mailing":
await bot_actions.mailing(bot, call)
return await send_bot_settings_menu(bot, call)
if operation == "tags":
await bot_actions.tags(bot, call)
return await send_bot_settings_menu(bot, call)
if operation == "thread_interrupt":
await bot_actions.thread_interrupt(bot, call)
return await send_bot_settings_menu(bot, call)

View File

@ -81,7 +81,7 @@ def _on_security_policy(message: types.Message, bot):
parse_mode="HTML")
async def send_user_message(message: types.Message, super_chat_id: int, bot):
async def send_user_message(message: types.Message, super_chat_id: int, bot, tag: str = ""):
"""Переслать сообщение от пользователя, добавлять к нему user info при необходимости"""
if bot.enable_additional_info:
user_info = _("Сообщение от пользователя ")
@ -106,6 +106,15 @@ async def send_user_message(message: types.Message, super_chat_id: int, bot):
new_message_2 = await message.copy_to(super_chat_id, reply_to_message_id=new_message.message_id)
await _redis.set(_message_unique_id(bot.pk, new_message_2.message_id), message.chat.id,
pexpire=ServerSettings.redis_timeout_ms())
elif tag:
# добавлять тег в конец текста
if message.content_type == types.ContentType.TEXT and len(message.text) + len(tag) < 4093:
new_message = await message.bot.send_message(super_chat_id, message.text + "\n\n" + tag)
else:
new_message = await message.bot.send_message(super_chat_id, text=tag)
new_message_2 = await message.copy_to(super_chat_id, reply_to_message_id=new_message.message_id)
await _redis.set(_message_unique_id(bot.pk, new_message_2.message_id), message.chat.id,
pexpire=ServerSettings.redis_timeout_ms())
else:
try:
new_message = await message.forward(super_chat_id)
@ -119,6 +128,13 @@ async def send_user_message(message: types.Message, super_chat_id: int, bot):
async def send_to_superchat(is_super_group: bool, message: types.Message, super_chat_id: int, bot):
"""Пересылка сообщения от пользователя оператору (логика потоков сообщений)"""
if bot.enable_tags:
tag = await _redis.get(_tag_uid(bot.pk, message.chat.id))
else:
tag = ""
if tag:
tag = str(tag)
if is_super_group and bot.enable_threads:
if bot.enable_thread_interrupt:
thread_timeout = ServerSettings.thread_timeout_ms()
@ -128,20 +144,34 @@ async def send_to_superchat(is_super_group: bool, message: types.Message, super_
if thread_first_message:
# переслать в супер-чат, отвечая на предыдущее сообщение
try:
if tag:
if message.content_type == types.ContentType.TEXT and len(message.text) + len(tag) < 4093:
new_message = await message.bot.send_message(
super_chat_id,
message.text + "\n\n" + tag,
reply_to_message_id=int(thread_first_message))
else:
new_message = await message.copy_to(super_chat_id,
reply_to_message_id=int(thread_first_message))
new_message_2 = await message.bot.send_message(
super_chat_id, reply_to_message_id=new_message.message_id, text=tag)
await _redis.set(_message_unique_id(bot.pk, new_message_2.message_id), message.chat.id,
pexpire=thread_timeout)
else:
new_message = await message.copy_to(super_chat_id, reply_to_message_id=int(thread_first_message))
await _redis.set(_message_unique_id(bot.pk, new_message.message_id), message.chat.id,
pexpire=thread_timeout)
except exceptions.BadRequest:
new_message = await send_user_message(message, super_chat_id, bot)
new_message = await send_user_message(message, super_chat_id, bot, tag)
await _redis.set(
_thread_unique_id(bot.pk, message.chat.id), new_message.message_id, pexpire=thread_timeout)
else:
# переслать супер-чат
new_message = await send_user_message(message, super_chat_id, bot)
new_message = await send_user_message(message, super_chat_id, bot, tag)
await _redis.set(_thread_unique_id(bot.pk, message.chat.id), new_message.message_id,
pexpire=thread_timeout)
else: # личные сообщения не поддерживают потоки сообщений: просто отправляем сообщение
await send_user_message(message, super_chat_id, bot)
await send_user_message(message, super_chat_id, bot, tag)
async def _increase_count(_bot):
@ -223,6 +253,7 @@ async def handle_operator_message(message: types.Message, super_chat_id: int, bo
else:
await banned_user.delete()
return SendMessage(chat_id=message.chat.id, text=_("Пользователь разбанен"))
if bot.enable_tags:
if message.text.startswith("/tag "):
tag = message.text.replace("/tag ", "")[:20].strip()
if tag: