mailing, more mailing

This commit is contained in:
walker 2022-11-07 02:03:00 +04:00
parent 696bc5368b
commit 58f551c77d
3 changed files with 43 additions and 30 deletions

View File

@ -2,8 +2,9 @@
Здесь работа с конкретным ботом Здесь работа с конкретным ботом
""" """
from asyncio import sleep from asyncio import sleep
from datetime import datetime
from aiogram import types from aiogram import types
from aiogram.utils.exceptions import TelegramAPIError, Unauthorized, BotKicked, BotBlocked from aiogram.utils import exceptions
from aiogram import Bot as AioBot from aiogram import Bot as AioBot
from olgram.models.models import Bot from olgram.models.models import Bot
from olgram.utils.mix import send_stored_message from olgram.utils.mix import send_stored_message
@ -17,14 +18,14 @@ async def delete_bot(bot: Bot, call: types.CallbackQuery):
""" """
try: try:
await unregister_token(bot.decrypted_token()) await unregister_token(bot.decrypted_token())
except Unauthorized: except exceptions.Unauthorized:
# Вероятно пользователь сбросил токен или удалил бот, это уже не наши проблемы # Вероятно пользователь сбросил токен или удалил бот, это уже не наши проблемы
pass pass
await bot.delete() await bot.delete()
await call.answer(_("Бот удалён")) await call.answer(_("Бот удалён"))
try: try:
await call.message.delete() await call.message.delete()
except TelegramAPIError: except exceptions.TelegramAPIError:
pass pass
@ -74,7 +75,7 @@ async def select_chat(bot: Bot, call: types.CallbackQuery, chat: str):
try: try:
await chat.delete() await chat.delete()
await a_bot.leave_chat(chat.chat_id) await a_bot.leave_chat(chat.chat_id)
except TelegramAPIError: except exceptions.TelegramAPIError:
pass pass
await call.answer(_("Бот вышел из чатов")) await call.answer(_("Бот вышел из чатов"))
await a_bot.session.close() await a_bot.session.close()
@ -119,16 +120,24 @@ async def go_mailing(bot: Bot, context) -> int:
users = await bot.mailing_users users = await bot.mailing_users
a_bot = AioBot(bot.decrypted_token()) a_bot = AioBot(bot.decrypted_token())
sended = 0 count = 0
print(f"start mailing {context}")
for user in users: for user in users:
bot.last_mailing_at = datetime.now()
await bot.save(update_fields=["last_mailing_at"])
try: try:
await sleep(0.2) await sleep(0.05)
await send_stored_message(context, a_bot, user.telegram_id) try:
sended += 1 await send_stored_message(context, a_bot, user.telegram_id)
except TelegramAPIError: except exceptions.RetryAfter as err:
# TODO: await sleep(err.timeout)
# delete user await send_stored_message(context, a_bot, user.telegram_id)
# check error source, check bot, break if bot deleted count += 1
continue except (exceptions.ChatNotFound, exceptions.BotBlocked, exceptions.UserDeactivated):
return sended await user.delete()
except exceptions.TelegramAPIError:
pass
return count

View File

@ -37,4 +37,4 @@ async def info(message: types.Message, state: FSMContext):
_("Входящих сообщений у всех ботов: {0}\n").format(income_messages) + _("Входящих сообщений у всех ботов: {0}\n").format(income_messages) +
_("Исходящих сообщений у всех ботов: {0}\n").format(outgoing_messages) + _("Исходящих сообщений у всех ботов: {0}\n").format(outgoing_messages) +
_("Промо-кодов выдано: {0}\n").format(promo_count) + _("Промо-кодов выдано: {0}\n").format(promo_count) +
_("Рекламную плашку выключили: {0}\n".format(olgram_text_disabled))) _("Рекламную плашку выключили: {0}\n").format(olgram_text_disabled))

View File

@ -420,15 +420,10 @@ async def mailing_text_received(message: types.Message, state: FSMContext):
_message_id = await send_stored_message(proxy, AioBot.get_current(), message.chat.id) _message_id = await send_stored_message(proxy, AioBot.get_current(), message.chat.id)
keyboard = types.InlineKeyboardMarkup(row_width=2) keyboard = types.InlineKeyboardMarkup(row_width=1)
keyboard.insert(
types.InlineKeyboardButton(text=_("<< Нет, отменить"), # TODO: don't move menu back
callback_data=menu_callback.new(level=1, bot_id=bot_id, operation=empty,
chat=empty))
)
keyboard.insert( keyboard.insert(
types.InlineKeyboardButton(text=_("Да, начать рассылку"), types.InlineKeyboardButton(text=_("Да, начать рассылку"),
callback_data=menu_callback.new(level=2, bot_id=bot_id, operation="go_go_mailing", callback_data=menu_callback.new(level=3, bot_id=bot_id, operation="go_go_mailing",
chat=empty)) chat=empty))
) )
@ -508,20 +503,15 @@ async def callback(call: types.CallbackQuery, callback_data: dict, state: FSMCon
return await send_bot_statistic_menu(bot, call) return await send_bot_statistic_menu(bot, call)
if operation == "settings": if operation == "settings":
return await send_bot_settings_menu(bot, call) return await send_bot_settings_menu(bot, call)
if operation in ("go_mailing", "go_go_mailing"): if operation == "go_mailing":
if bot.last_mailing_at and bot.last_mailing_at >= datetime.now() - timedelta(minutes=5): if bot.last_mailing_at and bot.last_mailing_at >= datetime.now() - timedelta(minutes=5):
return await call.answer(_("Рассылка была совсем недавно, подождите немного"), show_alert=True) return await call.answer(_("Рассылка была совсем недавно, подождите немного"), show_alert=True)
if operation == "go_mailing": if not await bot.mailing_users:
return await call.answer(_("Нет пользователей для рассылки"))
await state.set_state("wait_mailing_text") await state.set_state("wait_mailing_text")
async with state.proxy() as proxy: async with state.proxy() as proxy:
proxy["bot_id"] = bot.id proxy["bot_id"] = bot.id
return await send_bot_mailing_menu(bot, call) return await send_bot_mailing_menu(bot, call)
if operation == "go_go_mailing":
async with state.proxy() as proxy:
mailing_data = dict(proxy)
await state.reset_state() # TODO: double-click protection
await call.answer(_("Рассылка запущена"))
await bot_actions.go_mailing(bot, mailing_data)
if operation == "text": if operation == "text":
await state.set_state("wait_start_text") await state.set_state("wait_start_text")
async with state.proxy() as proxy: async with state.proxy() as proxy:
@ -556,6 +546,20 @@ async def callback(call: types.CallbackQuery, callback_data: dict, state: FSMCon
async with state.proxy() as proxy: async with state.proxy() as proxy:
proxy["bot_id"] = bot.id proxy["bot_id"] = bot.id
return await send_bot_second_text_menu(bot, call) return await send_bot_second_text_menu(bot, call)
if operation == "go_go_mailing":
if (await state.get_state()) == "wait_mailing_text":
async with state.proxy() as proxy:
mailing_data = dict(proxy)
await state.reset_state()
if bot.last_mailing_at and bot.last_mailing_at >= datetime.now() - timedelta(minutes=5):
return await call.answer(_("Рассылка была совсем недавно, подождите немного"), show_alert=True)
if not await bot.mailing_users:
return await call.answer(_("Нет пользователей для рассылки"))
await call.answer(_("Рассылка запущена"))
count = await bot_actions.go_mailing(bot, mailing_data)
await call.message.answer(_("Рассылка завершена, отправлено {0} сообщений").format(count))
if operation == "reset_second_text": if operation == "reset_second_text":
await bot_actions.reset_bot_second_text(bot, call) await bot_actions.reset_bot_second_text(bot, call)
return await send_bot_second_text_menu(bot, call) return await send_bot_second_text_menu(bot, call)