mirror of
https://github.com/civsocit/olgram.git
synced 2023-07-22 01:29:12 +03:00
Немного статистики
This commit is contained in:
parent
5cff8da9cd
commit
6f602f417f
@ -20,10 +20,16 @@ async def info(message: types.Message, state: FSMContext):
|
||||
await message.answer("Недостаточно прав")
|
||||
return
|
||||
|
||||
bots_count = len(await models.Bot.all())
|
||||
bots = await models.Bot.all()
|
||||
bots_count = len(bots)
|
||||
user_count = len(await models.User.all())
|
||||
templates_count = len(await models.DefaultAnswer.all())
|
||||
|
||||
income_messages = sum([bot.incoming_messages_count for bot in bots])
|
||||
outgoing_messages = sum([bot.outgoing_messages_count for bot in bots])
|
||||
|
||||
await message.answer(f"Количество ботов: {bots_count}\n"
|
||||
f"Количество пользователей: {user_count}\n"
|
||||
f"Шаблонов ответов: {templates_count}\n")
|
||||
f"Шаблонов ответов: {templates_count}\n"
|
||||
f"Входящих сообщений у всех ботов: {income_messages}\n"
|
||||
f"Исходящих сообщений у всех ботов: {outgoing_messages}\n")
|
||||
|
@ -107,6 +107,11 @@ async def send_bot_menu(bot: Bot, call: types.CallbackQuery):
|
||||
callback_data=menu_callback.new(level=2, bot_id=bot.id, operation="delete",
|
||||
chat=empty))
|
||||
)
|
||||
keyboard.insert(
|
||||
types.InlineKeyboardButton(text="Статистика",
|
||||
callback_data=menu_callback.new(level=2, bot_id=bot.id, operation="stat",
|
||||
chat=empty))
|
||||
)
|
||||
keyboard.insert(
|
||||
types.InlineKeyboardButton(text="<< Назад",
|
||||
callback_data=menu_callback.new(level=0, bot_id=empty, operation=empty, chat=empty))
|
||||
@ -174,6 +179,30 @@ async def send_bot_text_menu(bot: Bot, call: ty.Optional[types.CallbackQuery] =
|
||||
await AioBot.get_current().send_message(chat_id, text, reply_markup=keyboard, parse_mode="HTML")
|
||||
|
||||
|
||||
async def send_bot_statistic_menu(bot: Bot, call: ty.Optional[types.CallbackQuery] = None,
|
||||
chat_id: ty.Optional[int] = None):
|
||||
if call:
|
||||
await call.answer()
|
||||
keyboard = types.InlineKeyboardMarkup(row_width=2)
|
||||
keyboard.insert(
|
||||
types.InlineKeyboardButton(text="<< Назад",
|
||||
callback_data=menu_callback.new(level=1, bot_id=bot.id, operation=empty, chat=empty))
|
||||
)
|
||||
|
||||
text = dedent(f"""
|
||||
Статистика по боту @{bot.name}
|
||||
|
||||
Входящих сообщений: <b>{bot.incoming_messages_count}</b>
|
||||
Ответов: <b>{bot.outgoing_messages_count}</b>
|
||||
Шаблоны ответов: <b>{len(await bot.answers)}</b>
|
||||
Забанено пользователей: <b>{len(await bot.banned_users)}</b>
|
||||
""")
|
||||
if call:
|
||||
await edit_or_create(call, text, keyboard, parse_mode="HTML")
|
||||
else:
|
||||
await AioBot.get_current().send_message(chat_id, text, reply_markup=keyboard, parse_mode="HTML")
|
||||
|
||||
|
||||
async def send_bot_second_text_menu(bot: Bot, call: ty.Optional[types.CallbackQuery] = None,
|
||||
chat_id: ty.Optional[int] = None):
|
||||
if call:
|
||||
@ -329,6 +358,8 @@ async def callback(call: types.CallbackQuery, callback_data: dict, state: FSMCon
|
||||
return await send_chats_menu(bot, call)
|
||||
if operation == "delete":
|
||||
return await send_bot_delete_menu(bot, call)
|
||||
if operation == "stat":
|
||||
return await send_bot_statistic_menu(bot, call)
|
||||
if operation == "text":
|
||||
await state.set_state("wait_start_text")
|
||||
async with state.proxy() as proxy:
|
||||
|
6
olgram/migrations/models/9_20220218211744_update.sql
Normal file
6
olgram/migrations/models/9_20220218211744_update.sql
Normal file
@ -0,0 +1,6 @@
|
||||
-- upgrade --
|
||||
ALTER TABLE "bot" ADD "outgoing_messages_count" BIGINT NOT NULL DEFAULT 0;
|
||||
ALTER TABLE "bot" ADD "incoming_messages_count" BIGINT NOT NULL DEFAULT 0;
|
||||
-- downgrade --
|
||||
ALTER TABLE "bot" DROP COLUMN "outgoing_messages_count";
|
||||
ALTER TABLE "bot" DROP COLUMN "incoming_messages_count";
|
@ -38,6 +38,9 @@ class Bot(Model):
|
||||
on_delete=fields.relational.CASCADE,
|
||||
null=True)
|
||||
|
||||
incoming_messages_count = fields.BigIntField(default=0)
|
||||
outgoing_messages_count = fields.BigIntField(default=0)
|
||||
|
||||
def decrypted_token(self):
|
||||
cryptor = DatabaseSettings.cryptor()
|
||||
return cryptor.decrypt(self.token)
|
||||
|
@ -7,6 +7,7 @@ from contextvars import ContextVar
|
||||
from aiohttp.web_exceptions import HTTPNotFound
|
||||
from aioredis.commands import create_redis_pool
|
||||
from aioredis import Redis
|
||||
from tortoise.expressions import F
|
||||
import logging
|
||||
import typing as ty
|
||||
from olgram.settings import ServerSettings
|
||||
@ -55,6 +56,9 @@ async def message_handler(message: types.Message, *args, **kwargs):
|
||||
return SendMessage(chat_id=message.chat.id,
|
||||
text="Вы заблокированы в этом боте")
|
||||
|
||||
bot.incoming_messages_count = F("incoming_messages_count") + 1
|
||||
await bot.save(update_fields=["incoming_messages_count"])
|
||||
|
||||
# Пересылаем сообщение в супер-чат
|
||||
if is_super_group:
|
||||
thread_first_message = await _redis.get(_thread_uniqie_id(bot.pk, message.chat.id))
|
||||
@ -109,6 +113,9 @@ async def message_handler(message: types.Message, *args, **kwargs):
|
||||
await message.reply("<i>Невозможно переслать сообщение (автор заблокировал бота?)</i>",
|
||||
parse_mode="HTML")
|
||||
return
|
||||
|
||||
bot.outgoing_messages_count = F("outgoing_messages_count") + 1
|
||||
await bot.save(update_fields=["outgoing_messages_count"])
|
||||
elif super_chat_id > 0:
|
||||
# в супер-чате кто-то пишет сообщение сам себе, только для личных сообщений
|
||||
await message.forward(super_chat_id)
|
||||
|
Loading…
Reference in New Issue
Block a user