mirror of
https://gitflic.ru/project/alt-gnome/karkas.git
synced 2024-12-24 08:34:42 +03:00
Автоматический рефакторинг и игнорирование flake8
Выполнен автоматический рефакторинг. Для тех файлов, которые не прошли flake8 - был добавлен `noqa`, чтобы в будущем исправить эти проблемы
This commit is contained in:
parent
4edeef4003
commit
e80a01157f
1
.flake8
1
.flake8
@ -3,3 +3,4 @@ per-file-ignores =
|
|||||||
__init__.py:F401
|
__init__.py:F401
|
||||||
max-line-length = 88
|
max-line-length = 88
|
||||||
count = true
|
count = true
|
||||||
|
extend-ignore = E203,E701
|
||||||
|
8
init.py
8
init.py
@ -1,5 +1,5 @@
|
|||||||
from pathlib import Path
|
|
||||||
from json import dumps
|
from json import dumps
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
pwd = Path().cwd()
|
pwd = Path().cwd()
|
||||||
dir_core = pwd / "src" / "core"
|
dir_core = pwd / "src" / "core"
|
||||||
@ -7,9 +7,9 @@ dir_modules_standard = pwd / "src" / "modules" / "standard"
|
|||||||
dir_modules_custom = pwd / "src" / "modules" / "custom"
|
dir_modules_custom = pwd / "src" / "modules" / "custom"
|
||||||
|
|
||||||
json = {
|
json = {
|
||||||
'core': str(dir_core),
|
"core": str(dir_core),
|
||||||
'modules standard': str(dir_modules_standard),
|
"modules standard": str(dir_modules_standard),
|
||||||
'modules custom': str(dir_modules_custom),
|
"modules custom": str(dir_modules_custom),
|
||||||
}
|
}
|
||||||
with open("src/paths.json", "w", encoding="utf8") as f:
|
with open("src/paths.json", "w", encoding="utf8") as f:
|
||||||
f.write(dumps(json, indent=4))
|
f.write(dumps(json, indent=4))
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
import src.service
|
|
||||||
import src.core
|
import src.core
|
||||||
|
import src.service
|
||||||
|
@ -16,7 +16,7 @@ def setup_logger():
|
|||||||
filename=log_file,
|
filename=log_file,
|
||||||
level=logging.INFO,
|
level=logging.INFO,
|
||||||
format="%(asctime)s %(message)s",
|
format="%(asctime)s %(message)s",
|
||||||
datefmt="%H:%M:%S"
|
datefmt="%H:%M:%S",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
|
import asyncio
|
||||||
|
|
||||||
|
from aiogram import Bot, Dispatcher
|
||||||
from routers import include_routers
|
from routers import include_routers
|
||||||
|
|
||||||
from src.core.logger import log, setup_logger
|
from src.core.logger import log, setup_logger
|
||||||
from src.modules.standard.config.config import get_telegram_token
|
from src.modules.standard.config.config import get_telegram_token
|
||||||
from src.modules.standard.database.db_api import connect_database, create_tables
|
from src.modules.standard.database.db_api import connect_database, create_tables
|
||||||
import asyncio
|
|
||||||
from aiogram import Bot, Dispatcher
|
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
from aiogram import Dispatcher
|
from aiogram import Dispatcher
|
||||||
|
|
||||||
from src.modules.standard.info.routers import router as info_router
|
|
||||||
from src.modules.standard.admin.routers import router as admin_router
|
from src.modules.standard.admin.routers import router as admin_router
|
||||||
from src.modules.standard.message_processing.message_api import router as process_message
|
from src.modules.standard.info.routers import router as info_router
|
||||||
|
from src.modules.standard.message_processing.message_api import (
|
||||||
|
router as process_message,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def include_routers(dp: Dispatcher):
|
async def include_routers(dp: Dispatcher):
|
||||||
@ -13,4 +15,3 @@ async def include_routers(dp: Dispatcher):
|
|||||||
dp.include_router(info_router)
|
dp.include_router(info_router)
|
||||||
dp.include_router(admin_router)
|
dp.include_router(admin_router)
|
||||||
dp.include_router(process_message)
|
dp.include_router(process_message)
|
||||||
|
|
@ -1 +0,0 @@
|
|||||||
|
|
19
src/modules/external/yandexgpt/handlers.py
vendored
19
src/modules/external/yandexgpt/handlers.py
vendored
@ -1,10 +1,17 @@
|
|||||||
|
# flake8: noqa
|
||||||
|
import asyncio
|
||||||
|
|
||||||
from aiogram import Bot
|
from aiogram import Bot
|
||||||
from aiogram.types import Message
|
from aiogram.types import Message
|
||||||
from src.modules.external.yandexgpt.yandexgpt import *
|
|
||||||
from src.modules.standard.config.config import get_yandexgpt_token, get_yandexgpt_catalog_id, get_yandexgpt_prompt
|
|
||||||
from src.modules.standard.database.db_api import add_message
|
|
||||||
from src.core.logger import log
|
from src.core.logger import log
|
||||||
import asyncio
|
from src.modules.external.yandexgpt.yandexgpt import *
|
||||||
|
from src.modules.standard.config.config import (
|
||||||
|
get_yandexgpt_catalog_id,
|
||||||
|
get_yandexgpt_prompt,
|
||||||
|
get_yandexgpt_token,
|
||||||
|
)
|
||||||
|
from src.modules.standard.database.db_api import add_message
|
||||||
|
|
||||||
|
|
||||||
async def answer_to_message(message: Message, bot: Bot):
|
async def answer_to_message(message: Message, bot: Bot):
|
||||||
@ -14,6 +21,8 @@ async def answer_to_message(message: Message, bot: Bot):
|
|||||||
text = message.text
|
text = message.text
|
||||||
prompt = get_yandexgpt_prompt()
|
prompt = get_yandexgpt_prompt()
|
||||||
# response = await yagpt.async_yandexgpt(system_prompt=prompt, input_messages=text)
|
# response = await yagpt.async_yandexgpt(system_prompt=prompt, input_messages=text)
|
||||||
response = await yagpt.yandexgpt_request(chat_id = message.chat.id, message_id = message.message_id, type = "yandexgpt")
|
response = await yagpt.yandexgpt_request(
|
||||||
|
chat_id=message.chat.id, message_id=message.message_id, type="yandexgpt"
|
||||||
|
)
|
||||||
reply = await message.reply(response, parse_mode="Markdown")
|
reply = await message.reply(response, parse_mode="Markdown")
|
||||||
add_message(reply, message_ai_model="yandexgpt")
|
add_message(reply, message_ai_model="yandexgpt")
|
||||||
|
7
src/modules/external/yandexgpt/routers.py
vendored
7
src/modules/external/yandexgpt/routers.py
vendored
@ -1,7 +1,10 @@
|
|||||||
from aiogram import Router, F
|
# flake8: noqa
|
||||||
|
from aiogram import F, Router
|
||||||
|
|
||||||
from src.modules.external.yandexgpt.handlers import answer_to_message
|
from src.modules.external.yandexgpt.handlers import answer_to_message
|
||||||
|
|
||||||
router = Router()
|
router = Router()
|
||||||
# Если сообщение содержит в начале текст "Гномик" или "гномик" или отвечает на сообщение бота, то вызывается функция answer_to_message
|
# Если сообщение содержит в начале текст "Гномик" или "гномик" или отвечает на сообщение бота, то вызывается функция answer_to_message
|
||||||
router.message.register(answer_to_message, F.text.startswith("Гномик") | F.text.startswith("гномик"))
|
router.message.register(
|
||||||
|
answer_to_message, F.text.startswith("Гномик") | F.text.startswith("гномик")
|
||||||
|
)
|
||||||
|
143
src/modules/external/yandexgpt/yandexgpt.py
vendored
143
src/modules/external/yandexgpt/yandexgpt.py
vendored
@ -1,24 +1,27 @@
|
|||||||
import requests
|
# flake8: noqa
|
||||||
import json
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import json
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
import requests
|
||||||
|
|
||||||
from src.core.logger import log
|
from src.core.logger import log
|
||||||
|
|
||||||
from ...standard.database import *
|
|
||||||
from ...standard.config.config import *
|
from ...standard.config.config import *
|
||||||
|
from ...standard.database import *
|
||||||
|
|
||||||
|
|
||||||
class YandexGPT:
|
class YandexGPT:
|
||||||
token = None
|
token = None
|
||||||
catalog_id = None
|
catalog_id = None
|
||||||
languages = {
|
languages = {
|
||||||
"ru": "русский язык",
|
"ru": "русский язык",
|
||||||
"en": "английский язык",
|
"en": "английский язык",
|
||||||
"de": "немецкий язык",
|
"de": "немецкий язык",
|
||||||
"uk": "украинский язык",
|
"uk": "украинский язык",
|
||||||
"es": "испанский язык",
|
"es": "испанский язык",
|
||||||
"be": "белорусский язык",
|
"be": "белорусский язык",
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, token, catalog_id):
|
def __init__(self, token, catalog_id):
|
||||||
self.token = token
|
self.token = token
|
||||||
@ -29,11 +32,13 @@ class YandexGPT:
|
|||||||
async with session.post(url, headers=headers, json=prompt) as response:
|
async with session.post(url, headers=headers, json=prompt) as response:
|
||||||
return await response.json()
|
return await response.json()
|
||||||
|
|
||||||
async def async_token_check(self, messages, gpt, max_tokens, stream, temperature, del_msg_id=1):
|
async def async_token_check(
|
||||||
|
self, messages, gpt, max_tokens, stream, temperature, del_msg_id=1
|
||||||
|
):
|
||||||
url = "https://llm.api.cloud.yandex.net/foundationModels/v1/tokenizeCompletion"
|
url = "https://llm.api.cloud.yandex.net/foundationModels/v1/tokenizeCompletion"
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Authorization": f"Api-Key {self.token}"
|
"Authorization": f"Api-Key {self.token}",
|
||||||
}
|
}
|
||||||
answer_token = get_yandexgpt_token_for_answer()
|
answer_token = get_yandexgpt_token_for_answer()
|
||||||
while True:
|
while True:
|
||||||
@ -43,12 +48,14 @@ class YandexGPT:
|
|||||||
"completionOptions": {
|
"completionOptions": {
|
||||||
"stream": stream,
|
"stream": stream,
|
||||||
"temperature": temperature,
|
"temperature": temperature,
|
||||||
"maxTokens": max_tokens
|
"maxTokens": max_tokens,
|
||||||
},
|
},
|
||||||
"messages": messages
|
"messages": messages,
|
||||||
}
|
}
|
||||||
response = await self.async_request(url=url, headers=headers, prompt=request)
|
response = await self.async_request(
|
||||||
except Exception as e: # TODO: Переделать обработку ошибок
|
url=url, headers=headers, prompt=request
|
||||||
|
)
|
||||||
|
except Exception as e: # TODO: Переделать обработку ошибок
|
||||||
# print(e)
|
# print(e)
|
||||||
await log(f"Error: {e}")
|
await log(f"Error: {e}")
|
||||||
|
|
||||||
@ -62,12 +69,19 @@ class YandexGPT:
|
|||||||
Exception("IndexError: list index out of range")
|
Exception("IndexError: list index out of range")
|
||||||
return messages
|
return messages
|
||||||
|
|
||||||
async def async_yandexgpt_lite(self, system_prompt, input_messages, stream=False, temperature=0.6, max_tokens=8000):
|
async def async_yandexgpt_lite(
|
||||||
|
self,
|
||||||
|
system_prompt,
|
||||||
|
input_messages,
|
||||||
|
stream=False,
|
||||||
|
temperature=0.6,
|
||||||
|
max_tokens=8000,
|
||||||
|
):
|
||||||
url = "https://llm.api.cloud.yandex.net/foundationModels/v1/completion"
|
url = "https://llm.api.cloud.yandex.net/foundationModels/v1/completion"
|
||||||
gpt = f"gpt://{self.catalog_id}/yandexgpt-lite/latest"
|
gpt = f"gpt://{self.catalog_id}/yandexgpt-lite/latest"
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Authorization": f"Api-Key {self.token}"
|
"Authorization": f"Api-Key {self.token}",
|
||||||
}
|
}
|
||||||
|
|
||||||
messages = [{"role": "system", "text": system_prompt}]
|
messages = [{"role": "system", "text": system_prompt}]
|
||||||
@ -80,27 +94,27 @@ class YandexGPT:
|
|||||||
"completionOptions": {
|
"completionOptions": {
|
||||||
"stream": stream,
|
"stream": stream,
|
||||||
"temperature": temperature,
|
"temperature": temperature,
|
||||||
"maxTokens": max_tokens
|
"maxTokens": max_tokens,
|
||||||
},
|
},
|
||||||
"messages": messages
|
"messages": messages,
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.post(url, headers=headers, json=prompt).text
|
response = requests.post(url, headers=headers, json=prompt).text
|
||||||
return json.loads(response)["result"]["alternatives"][0]["message"]["text"]
|
return json.loads(response)["result"]["alternatives"][0]["message"]["text"]
|
||||||
|
|
||||||
async def async_yandexgpt(
|
async def async_yandexgpt(
|
||||||
self,
|
self,
|
||||||
system_prompt,
|
system_prompt,
|
||||||
input_messages,
|
input_messages,
|
||||||
stream=False,
|
stream=False,
|
||||||
temperature=0.6,
|
temperature=0.6,
|
||||||
max_tokens=get_yandexgpt_token_for_request()
|
max_tokens=get_yandexgpt_token_for_request(),
|
||||||
):
|
):
|
||||||
url = "https://llm.api.cloud.yandex.net/foundationModels/v1/completion"
|
url = "https://llm.api.cloud.yandex.net/foundationModels/v1/completion"
|
||||||
gpt = f"gpt://{self.catalog_id}/yandexgpt/latest"
|
gpt = f"gpt://{self.catalog_id}/yandexgpt/latest"
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Authorization": f"Api-Key {self.token}"
|
"Authorization": f"Api-Key {self.token}",
|
||||||
}
|
}
|
||||||
|
|
||||||
messages = []
|
messages = []
|
||||||
@ -108,21 +122,22 @@ class YandexGPT:
|
|||||||
for message in input_messages:
|
for message in input_messages:
|
||||||
messages.append(message)
|
messages.append(message)
|
||||||
|
|
||||||
messages = await self.async_token_check(messages, gpt, max_tokens, stream, temperature)
|
messages = await self.async_token_check(
|
||||||
|
messages, gpt, max_tokens, stream, temperature
|
||||||
|
)
|
||||||
|
|
||||||
request = {
|
request = {
|
||||||
"modelUri": gpt,
|
"modelUri": gpt,
|
||||||
"completionOptions": {
|
"completionOptions": {
|
||||||
"stream": stream,
|
"stream": stream,
|
||||||
"temperature": temperature,
|
"temperature": temperature,
|
||||||
"maxTokens": max_tokens
|
"maxTokens": max_tokens,
|
||||||
},
|
},
|
||||||
"messages": messages
|
"messages": messages,
|
||||||
}
|
}
|
||||||
response = await self.async_request(url=url, headers=headers, prompt=request)
|
response = await self.async_request(url=url, headers=headers, prompt=request)
|
||||||
return response["result"]["alternatives"][0]["message"]["text"]
|
return response["result"]["alternatives"][0]["message"]["text"]
|
||||||
|
|
||||||
|
|
||||||
async def async_yandexgpt_translate(self, input_language, output_language, text):
|
async def async_yandexgpt_translate(self, input_language, output_language, text):
|
||||||
input_language = self.languages[input_language]
|
input_language = self.languages[input_language]
|
||||||
output_language = self.languages[output_language]
|
output_language = self.languages[output_language]
|
||||||
@ -130,7 +145,9 @@ class YandexGPT:
|
|||||||
return await self.async_yandexgpt(
|
return await self.async_yandexgpt(
|
||||||
f"Переведи на {output_language} сохранив оригинальный смысл текста. Верни только результат:",
|
f"Переведи на {output_language} сохранив оригинальный смысл текста. Верни только результат:",
|
||||||
[{"role": "user", "text": text}],
|
[{"role": "user", "text": text}],
|
||||||
stream=False, temperature=0.6, max_tokens=8000
|
stream=False,
|
||||||
|
temperature=0.6,
|
||||||
|
max_tokens=8000,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_yandexgpt_spelling_check(self, input_language, text):
|
async def async_yandexgpt_spelling_check(self, input_language, text):
|
||||||
@ -140,15 +157,19 @@ class YandexGPT:
|
|||||||
f"Проверьте орфографию и пунктуацию текста на {input_language}. Верни исправленный текст "
|
f"Проверьте орфографию и пунктуацию текста на {input_language}. Верни исправленный текст "
|
||||||
f"без смысловых искажений:",
|
f"без смысловых искажений:",
|
||||||
[{"role": "user", "text": text}],
|
[{"role": "user", "text": text}],
|
||||||
stream=False, temperature=0.6, max_tokens=8000
|
stream=False,
|
||||||
|
temperature=0.6,
|
||||||
|
max_tokens=8000,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_yandexgpt_text_history(self, input_messages, stream=False, temperature=0.6, max_tokens=8000):
|
async def async_yandexgpt_text_history(
|
||||||
|
self, input_messages, stream=False, temperature=0.6, max_tokens=8000
|
||||||
|
):
|
||||||
url = "https://llm.api.cloud.yandex.net/foundationModels/v1/completion"
|
url = "https://llm.api.cloud.yandex.net/foundationModels/v1/completion"
|
||||||
gpt = f"gpt://{self.catalog_id}/summarization/latest"
|
gpt = f"gpt://{self.catalog_id}/summarization/latest"
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Authorization": f"Api-Key {self.token}"
|
"Authorization": f"Api-Key {self.token}",
|
||||||
}
|
}
|
||||||
|
|
||||||
messages = []
|
messages = []
|
||||||
@ -161,15 +182,17 @@ class YandexGPT:
|
|||||||
"completionOptions": {
|
"completionOptions": {
|
||||||
"stream": stream,
|
"stream": stream,
|
||||||
"temperature": temperature,
|
"temperature": temperature,
|
||||||
"maxTokens": max_tokens
|
"maxTokens": max_tokens,
|
||||||
},
|
},
|
||||||
"messages": messages
|
"messages": messages,
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.post(url, headers=headers, json=prompt).text
|
response = requests.post(url, headers=headers, json=prompt).text
|
||||||
return json.loads(response)["result"]["alternatives"][0]["message"]["text"]
|
return json.loads(response)["result"]["alternatives"][0]["message"]["text"]
|
||||||
|
|
||||||
async def async_yandex_cloud_text_to_speech(self, text, voice, emotion, speed, format, quality):
|
async def async_yandex_cloud_text_to_speech(
|
||||||
|
self, text, voice, emotion, speed, format, quality
|
||||||
|
):
|
||||||
tts = "tts.api.cloud.yandex.net/speech/v1/tts:synthesize"
|
tts = "tts.api.cloud.yandex.net/speech/v1/tts:synthesize"
|
||||||
# TODO: Сделать функцию TTS
|
# TODO: Сделать функцию TTS
|
||||||
return 0
|
return 0
|
||||||
@ -187,14 +210,18 @@ class YandexGPT:
|
|||||||
if db_api.get_message_ai_model(chat_id, message_id) != None:
|
if db_api.get_message_ai_model(chat_id, message_id) != None:
|
||||||
messages.append({"role": "assistant", "text": message})
|
messages.append({"role": "assistant", "text": message})
|
||||||
else:
|
else:
|
||||||
sender_name = db_api.get_user_name(db_api.get_message_sender_id(chat_id, message_id))
|
sender_name = db_api.get_user_name(
|
||||||
|
db_api.get_message_sender_id(chat_id, message_id)
|
||||||
|
)
|
||||||
messages.append({"role": "user", "text": sender_name + ": " + message})
|
messages.append({"role": "user", "text": sender_name + ": " + message})
|
||||||
message_id = db_api.get_answer_to_message_id(chat_id, message_id)
|
message_id = db_api.get_answer_to_message_id(chat_id, message_id)
|
||||||
if message_id is None:
|
if message_id is None:
|
||||||
break
|
break
|
||||||
return list(reversed(messages))
|
return list(reversed(messages))
|
||||||
|
|
||||||
async def collecting_messages_for_history(self, start_message_id, end_message_id, chat_id):
|
async def collecting_messages_for_history(
|
||||||
|
self, start_message_id, end_message_id, chat_id
|
||||||
|
):
|
||||||
messages = []
|
messages = []
|
||||||
# Собираем цепочку сообщений в формате: [{"role": "user", "text": "<Имя_пользователя>: Привет!"},
|
# Собираем цепочку сообщений в формате: [{"role": "user", "text": "<Имя_пользователя>: Привет!"},
|
||||||
# {"role": "assistant", "text": "Привет!"}]
|
# {"role": "assistant", "text": "Привет!"}]
|
||||||
@ -203,21 +230,33 @@ class YandexGPT:
|
|||||||
if db_api.get_message_ai_model(chat_id, start_message_id) != None:
|
if db_api.get_message_ai_model(chat_id, start_message_id) != None:
|
||||||
messages.append({"role": "assistant", "text": message})
|
messages.append({"role": "assistant", "text": message})
|
||||||
else:
|
else:
|
||||||
sender_name = db_api.get_user_name(db_api.get_message_sender_id(chat_id, start_message_id))
|
sender_name = db_api.get_user_name(
|
||||||
|
db_api.get_message_sender_id(chat_id, start_message_id)
|
||||||
|
)
|
||||||
messages.append({"role": "user", "text": sender_name + ": " + message})
|
messages.append({"role": "user", "text": sender_name + ": " + message})
|
||||||
start_message_id -= 1
|
start_message_id -= 1
|
||||||
if start_message_id <= end_message_id:
|
if start_message_id <= end_message_id:
|
||||||
break
|
break
|
||||||
return messages.reverse()
|
return messages.reverse()
|
||||||
|
|
||||||
async def yandexgpt_request(self, message_id = None, type = "yandexgpt-lite", chat_id = None,
|
async def yandexgpt_request(
|
||||||
message_id_end = None, input_language = None, output_language = None, text = None):
|
self,
|
||||||
|
message_id=None,
|
||||||
|
type="yandexgpt-lite",
|
||||||
|
chat_id=None,
|
||||||
|
message_id_end=None,
|
||||||
|
input_language=None,
|
||||||
|
output_language=None,
|
||||||
|
text=None,
|
||||||
|
):
|
||||||
if type == "yandexgpt-lite":
|
if type == "yandexgpt-lite":
|
||||||
messages = await self.collect_messages(message_id, chat_id)
|
messages = await self.collect_messages(message_id, chat_id)
|
||||||
return await self.async_yandexgpt_lite(
|
return await self.async_yandexgpt_lite(
|
||||||
system_prompt=get_yandexgpt_prompt(),
|
system_prompt=get_yandexgpt_prompt(),
|
||||||
input_messages=messages,
|
input_messages=messages,
|
||||||
stream=False, temperature=0.6, max_tokens=8000
|
stream=False,
|
||||||
|
temperature=0.6,
|
||||||
|
max_tokens=8000,
|
||||||
)
|
)
|
||||||
elif type == "yandexgpt":
|
elif type == "yandexgpt":
|
||||||
# print("yandexgpt_request")
|
# print("yandexgpt_request")
|
||||||
@ -226,24 +265,26 @@ class YandexGPT:
|
|||||||
return await self.async_yandexgpt(
|
return await self.async_yandexgpt(
|
||||||
system_prompt=get_yandexgpt_prompt(),
|
system_prompt=get_yandexgpt_prompt(),
|
||||||
input_messages=messages,
|
input_messages=messages,
|
||||||
stream=False, temperature=0.6, max_tokens=get_yandexgpt_token_for_request()
|
stream=False,
|
||||||
|
temperature=0.6,
|
||||||
|
max_tokens=get_yandexgpt_token_for_request(),
|
||||||
)
|
)
|
||||||
elif type == "yandexgpt-translate":
|
elif type == "yandexgpt-translate":
|
||||||
return await self.async_yandexgpt_translate(
|
return await self.async_yandexgpt_translate(
|
||||||
input_language,
|
input_language,
|
||||||
output_language,
|
output_language,
|
||||||
text=db_api.get_message_text(chat_id, message_id)
|
text=db_api.get_message_text(chat_id, message_id),
|
||||||
)
|
)
|
||||||
elif type == "yandexgpt-spelling-check":
|
elif type == "yandexgpt-spelling-check":
|
||||||
return await self.async_yandexgpt_spelling_check(
|
return await self.async_yandexgpt_spelling_check(
|
||||||
input_language,
|
input_language, text=db_api.get_message_text(chat_id, message_id)
|
||||||
text=db_api.get_message_text(chat_id, message_id)
|
|
||||||
)
|
)
|
||||||
elif type == "yandexgpt-text-history":
|
elif type == "yandexgpt-text-history":
|
||||||
messages = await self.collect_messages_for_history(message_id, message_id_end, chat_id)
|
messages = await self.collect_messages_for_history(
|
||||||
|
message_id, message_id_end, chat_id
|
||||||
|
)
|
||||||
return await self.async_yandexgpt_text_history(
|
return await self.async_yandexgpt_text_history(
|
||||||
messages=messages,
|
messages=messages, stream=False, temperature=0.6, max_tokens=8000
|
||||||
stream=False, temperature=0.6, max_tokens=8000
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return "Ошибка: Неизвестный тип запроса | Error: Unknown request type"
|
return "Ошибка: Неизвестный тип запроса | Error: Unknown request type"
|
||||||
|
@ -1,18 +1,26 @@
|
|||||||
|
# flake8: noqa
|
||||||
|
import time
|
||||||
|
|
||||||
from aiogram import Bot
|
from aiogram import Bot
|
||||||
from aiogram.types import Message
|
from aiogram.types import Message
|
||||||
|
|
||||||
from src.modules.standard.config.config import get_default_chat_tag
|
from src.modules.standard.config.config import get_default_chat_tag
|
||||||
import time
|
|
||||||
|
|
||||||
|
|
||||||
async def delete_message(message: Message, bot: Bot):
|
async def delete_message(message: Message, bot: Bot):
|
||||||
reply_message_id = message.reply_to_message.message_id
|
reply_message_id = message.reply_to_message.message_id
|
||||||
await bot.delete_message(message.chat.id, reply_message_id)
|
await bot.delete_message(message.chat.id, reply_message_id)
|
||||||
|
|
||||||
|
|
||||||
async def error_access(message: Message, bot: Bot):
|
async def error_access(message: Message, bot: Bot):
|
||||||
await message.reply("Вы не админ/модератор")
|
await message.reply("Вы не админ/модератор")
|
||||||
|
|
||||||
|
|
||||||
async def get_chat_id(message: Message, bot: Bot):
|
async def get_chat_id(message: Message, bot: Bot):
|
||||||
await message.reply(f"ID данного чата: `{message.chat.id}`", parse_mode="MarkdownV2")
|
await message.reply(
|
||||||
|
f"ID данного чата: `{message.chat.id}`", parse_mode="MarkdownV2"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def chat_not_in_approve_list(message: Message, bot: Bot):
|
async def chat_not_in_approve_list(message: Message, bot: Bot):
|
||||||
await message.reply(
|
await message.reply(
|
||||||
@ -22,6 +30,7 @@ async def chat_not_in_approve_list(message: Message, bot: Bot):
|
|||||||
)
|
)
|
||||||
await get_chat_id(message, bot)
|
await get_chat_id(message, bot)
|
||||||
|
|
||||||
|
|
||||||
async def mute_user(chat_id: int, user_id: int, time: int, bot: Bot):
|
async def mute_user(chat_id: int, user_id: int, time: int, bot: Bot):
|
||||||
# *, can_send_messages: bool | None = None, can_send_audios: bool | None = None, can_send_documents: bool | None = None, can_send_photos: bool | None = None, can_send_videos: bool | None = None, can_send_video_notes: bool | None = None, can_send_voice_notes: bool | None = None, can_send_polls: bool | None = None, can_send_other_messages: bool | None = None, can_add_web_page_previews: bool | None = None, can_change_info: bool | None = None, can_invite_users: bool | None = None, can_pin_messages: bool | None = None, can_manage_topics: bool | None = None, **extra_data: Any)
|
# *, can_send_messages: bool | None = None, can_send_audios: bool | None = None, can_send_documents: bool | None = None, can_send_photos: bool | None = None, can_send_videos: bool | None = None, can_send_video_notes: bool | None = None, can_send_voice_notes: bool | None = None, can_send_polls: bool | None = None, can_send_other_messages: bool | None = None, can_add_web_page_previews: bool | None = None, can_change_info: bool | None = None, can_invite_users: bool | None = None, can_pin_messages: bool | None = None, can_manage_topics: bool | None = None, **extra_data: Any)
|
||||||
|
|
||||||
@ -39,8 +48,9 @@ async def mute_user(chat_id: int, user_id: int, time: int, bot: Bot):
|
|||||||
"can_change_info": False,
|
"can_change_info": False,
|
||||||
"can_invite_users": False,
|
"can_invite_users": False,
|
||||||
"can_pin_messages": False,
|
"can_pin_messages": False,
|
||||||
"can_manage_topics": False
|
"can_manage_topics": False,
|
||||||
}
|
}
|
||||||
end_time = time + int(time.time())
|
end_time = time + int(time.time())
|
||||||
await bot.restrict_chat_member(chat_id, user_id, until_date=end_time, **mutePermissions)
|
await bot.restrict_chat_member(
|
||||||
|
chat_id, user_id, until_date=end_time, **mutePermissions
|
||||||
|
)
|
||||||
|
@ -1,15 +1,24 @@
|
|||||||
from aiogram import Router, F
|
# flake8: noqa
|
||||||
|
from aiogram import F, Router
|
||||||
|
|
||||||
from src.modules.standard.admin.handlers import delete_message, error_access, get_chat_id, chat_not_in_approve_list
|
from src.modules.standard.admin.handlers import (
|
||||||
from src.modules.standard.filters.filters import ChatModerOrAdminFilter, ChatNotInApproveFilter
|
chat_not_in_approve_list,
|
||||||
|
delete_message,
|
||||||
|
error_access,
|
||||||
|
get_chat_id,
|
||||||
|
)
|
||||||
|
from src.modules.standard.filters.filters import (
|
||||||
|
ChatModerOrAdminFilter,
|
||||||
|
ChatNotInApproveFilter,
|
||||||
|
)
|
||||||
|
|
||||||
router = Router()
|
router = Router()
|
||||||
|
|
||||||
# Если сообщение содержит какой либо текст и выполняется фильтр ChatNotInApproveFilter, то вызывается функция chat_not_in_approve_list
|
# Если сообщение содержит какой либо текст и выполняется фильтр ChatNotInApproveFilter, то вызывается функция chat_not_in_approve_list
|
||||||
router.message.register(chat_not_in_approve_list, ChatNotInApproveFilter(), F.text)
|
router.message.register(chat_not_in_approve_list, ChatNotInApproveFilter(), F.text)
|
||||||
|
|
||||||
router.message.register(get_chat_id, ChatModerOrAdminFilter(), F.text == '/chatID')
|
router.message.register(get_chat_id, ChatModerOrAdminFilter(), F.text == "/chatID")
|
||||||
|
|
||||||
router.message.register(delete_message, ChatModerOrAdminFilter(), F.text == '/rm')
|
router.message.register(delete_message, ChatModerOrAdminFilter(), F.text == "/rm")
|
||||||
router.message.register(error_access, F.text == '/rm')
|
router.message.register(error_access, F.text == "/rm")
|
||||||
router.message.register(error_access, F.text == '/chatID')
|
router.message.register(error_access, F.text == "/chatID")
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
|
# flake8: noqa
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from ....service import paths
|
from ....service import paths
|
||||||
|
|
||||||
|
|
||||||
@ -9,52 +12,64 @@ def get_config(is_test: bool = False) -> dict:
|
|||||||
path = paths.core
|
path = paths.core
|
||||||
path = f"{path}/config.yaml"
|
path = f"{path}/config.yaml"
|
||||||
|
|
||||||
with open(path, 'r') as file:
|
with open(path, "r") as file:
|
||||||
return yaml.full_load(file)
|
return yaml.full_load(file)
|
||||||
|
|
||||||
|
|
||||||
config = get_config()
|
config = get_config()
|
||||||
|
|
||||||
|
|
||||||
def get_telegram_token() -> str:
|
def get_telegram_token() -> str:
|
||||||
return config["TELEGRAM"]["TOKEN"]
|
return config["TELEGRAM"]["TOKEN"]
|
||||||
|
|
||||||
|
|
||||||
def get_telegram_check_bot() -> bool:
|
def get_telegram_check_bot() -> bool:
|
||||||
return config["TELEGRAM"]["CHECK_BOT"]
|
return config["TELEGRAM"]["CHECK_BOT"]
|
||||||
|
|
||||||
|
|
||||||
def get_aproved_chat_id() -> list:
|
def get_aproved_chat_id() -> list:
|
||||||
# Возваращем сплитованный список id чатов в формате int
|
# Возваращем сплитованный список id чатов в формате int
|
||||||
return [int(chat_id) for chat_id in config["TELEGRAM"]["APPROVED_CHAT_ID"].split(" | ")]
|
return [
|
||||||
|
int(chat_id) for chat_id in config["TELEGRAM"]["APPROVED_CHAT_ID"].split(" | ")
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def get_user_role_name(role_number) -> dict:
|
def get_user_role_name(role_number) -> dict:
|
||||||
# Возвращаем название роли пользвателя по номеру роли, если такой роли нет, возвращаем неизвестно
|
# Возвращаем название роли пользвателя по номеру роли, если такой роли нет, возвращаем неизвестно
|
||||||
return config["ROLES"].get(role_number, "Неизвестно")
|
return config["ROLES"].get(role_number, "Неизвестно")
|
||||||
|
|
||||||
|
|
||||||
def get_default_chat_tag() -> str:
|
def get_default_chat_tag() -> str:
|
||||||
return config["TELEGRAM"]["DEFAULT_CHAT_TAG"]
|
return config["TELEGRAM"]["DEFAULT_CHAT_TAG"]
|
||||||
|
|
||||||
|
|
||||||
def get_yandexgpt_token() -> str:
|
def get_yandexgpt_token() -> str:
|
||||||
return config["YANDEXGPT"]["TOKEN"]
|
return config["YANDEXGPT"]["TOKEN"]
|
||||||
|
|
||||||
|
|
||||||
def get_yandexgpt_catalog_id() -> str:
|
def get_yandexgpt_catalog_id() -> str:
|
||||||
return config["YANDEXGPT"]["CATALOGID"]
|
return config["YANDEXGPT"]["CATALOGID"]
|
||||||
|
|
||||||
|
|
||||||
def get_yandexgpt_prompt() -> str:
|
def get_yandexgpt_prompt() -> str:
|
||||||
return config["YANDEXGPT"]["PROMPT"]
|
return config["YANDEXGPT"]["PROMPT"]
|
||||||
|
|
||||||
|
|
||||||
def get_yandexgpt_start_words() -> list:
|
def get_yandexgpt_start_words() -> list:
|
||||||
return config["YANDEXGPT"]["STARTWORD"].split(" | ")
|
return config["YANDEXGPT"]["STARTWORD"].split(" | ")
|
||||||
|
|
||||||
|
|
||||||
def get_yandexgpt_in_words() -> list:
|
def get_yandexgpt_in_words() -> list:
|
||||||
return config["YANDEXGPT"]["INWORD"].split(" | ")
|
return config["YANDEXGPT"]["INWORD"].split(" | ")
|
||||||
|
|
||||||
|
|
||||||
def get_yandexgpt_token_for_request() -> int:
|
def get_yandexgpt_token_for_request() -> int:
|
||||||
return config["YANDEXGPT"]["TOKEN_FOR_REQUEST"]
|
return config["YANDEXGPT"]["TOKEN_FOR_REQUEST"]
|
||||||
|
|
||||||
|
|
||||||
def get_yandexgpt_token_for_answer() -> int:
|
def get_yandexgpt_token_for_answer() -> int:
|
||||||
return config["YANDEXGPT"]["TOKEN_FOR_ANSWER"]
|
return config["YANDEXGPT"]["TOKEN_FOR_ANSWER"]
|
||||||
|
|
||||||
|
|
||||||
def get_access_rights() -> dict:
|
def get_access_rights() -> dict:
|
||||||
return get_config()["ACCESS_RIGHTS"]
|
return get_config()["ACCESS_RIGHTS"]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
from src.modules.standard.config.config import get_config
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
from src.modules.standard.config.config import get_config
|
||||||
|
|
||||||
yaml_load = get_config(is_test=True)
|
yaml_load = get_config(is_test=True)
|
||||||
|
|
||||||
|
|
||||||
@ -18,20 +19,27 @@ class TestConfig(unittest.TestCase):
|
|||||||
def test_yaml_keys_existence(self):
|
def test_yaml_keys_existence(self):
|
||||||
self.assertTrue(all(key in yaml_load for key in ["TELEGRAM", "ROLES"]))
|
self.assertTrue(all(key in yaml_load for key in ["TELEGRAM", "ROLES"]))
|
||||||
self.assertIn("TOKEN", yaml_load["TELEGRAM"])
|
self.assertIn("TOKEN", yaml_load["TELEGRAM"])
|
||||||
self.assertTrue(all(role in yaml_load["ROLES"] for role in ["ADMIN", "MODERATOR", "USER"]))
|
self.assertTrue(
|
||||||
|
all(role in yaml_load["ROLES"] for role in ["ADMIN", "MODERATOR", "USER"])
|
||||||
|
)
|
||||||
|
|
||||||
def test_yaml_yaml_load_types(self):
|
def test_yaml_yaml_load_types(self):
|
||||||
self.assertIsInstance(yaml_load["TELEGRAM"]["TOKEN"], str)
|
self.assertIsInstance(yaml_load["TELEGRAM"]["TOKEN"], str)
|
||||||
self.assertTrue(all(isinstance(yaml_load["ROLES"][role], int) for role in ["ADMIN", "MODERATOR", "USER"]))
|
self.assertTrue(
|
||||||
|
all(
|
||||||
|
isinstance(yaml_load["ROLES"][role], int)
|
||||||
|
for role in ["ADMIN", "MODERATOR", "USER"]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
def test_yaml_values(self):
|
def test_yaml_values(self):
|
||||||
expected_token = 'xxxxxxxxxxxxxxxxxxxx'
|
expected_token = "xxxxxxxxxxxxxxxxxxxx"
|
||||||
expected_role_values = {'ADMIN': 0, 'MODERATOR': 1, 'USER': 2, 'BOT': 3}
|
expected_role_values = {"ADMIN": 0, "MODERATOR": 1, "USER": 2, "BOT": 3}
|
||||||
|
|
||||||
self.assertEqual(yaml_load["TELEGRAM"]["TOKEN"], expected_token)
|
self.assertEqual(yaml_load["TELEGRAM"]["TOKEN"], expected_token)
|
||||||
for role, value in expected_role_values.items():
|
for role, value in expected_role_values.items():
|
||||||
self.assertEqual(yaml_load["ROLES"][role], value)
|
self.assertEqual(yaml_load["ROLES"][role], value)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
from .models.chats import Chats
|
|
||||||
from .models.messages import Messages
|
|
||||||
from .models.users import Users
|
|
||||||
from .models.user_stats import UserStats
|
|
||||||
from .models.chat_stats import ChatStats
|
|
||||||
from ....service import paths
|
|
||||||
from ..exceptions.module_exceptions import MissingModuleName, NotExpectedModuleName
|
|
||||||
import peewee as pw
|
import peewee as pw
|
||||||
from aiogram.types import Message
|
from aiogram.types import Message
|
||||||
|
|
||||||
|
from ....service import paths
|
||||||
|
from ..exceptions.module_exceptions import MissingModuleName, NotExpectedModuleName
|
||||||
|
from .models.chat_stats import ChatStats
|
||||||
|
from .models.chats import Chats
|
||||||
|
from .models.messages import Messages
|
||||||
|
from .models.user_stats import UserStats
|
||||||
|
from .models.users import Users
|
||||||
|
|
||||||
|
|
||||||
def connect_database(is_test: bool = False, module: str | None = None):
|
def connect_database(is_test: bool = False, module: str | None = None):
|
||||||
if is_test:
|
if is_test:
|
||||||
@ -37,11 +38,14 @@ def create_tables(db: pw.SqliteDatabase):
|
|||||||
|
|
||||||
|
|
||||||
def add_chat(chat_id, chat_name, chat_type=10, chat_stats=0):
|
def add_chat(chat_id, chat_name, chat_type=10, chat_stats=0):
|
||||||
chat, created = Chats.get_or_create(id=chat_id, defaults={
|
chat, created = Chats.get_or_create(
|
||||||
'chat_name': chat_name,
|
id=chat_id,
|
||||||
'chat_type': chat_type,
|
defaults={
|
||||||
'chat_all_stat': chat_stats,
|
"chat_name": chat_name,
|
||||||
})
|
"chat_type": chat_type,
|
||||||
|
"chat_all_stat": chat_stats,
|
||||||
|
},
|
||||||
|
)
|
||||||
if not created:
|
if not created:
|
||||||
# Обновить существующий чат, если он уже существует
|
# Обновить существующий чат, если он уже существует
|
||||||
chat.chat_name = chat_name
|
chat.chat_name = chat_name
|
||||||
@ -50,19 +54,30 @@ def add_chat(chat_id, chat_name, chat_type=10, chat_stats=0):
|
|||||||
chat.save()
|
chat.save()
|
||||||
|
|
||||||
|
|
||||||
def add_user(user_id, user_first_name, user_last_name=None, user_tag=None, user_role=0, user_stats=0, user_rep=0):
|
def add_user(
|
||||||
|
user_id,
|
||||||
|
user_first_name,
|
||||||
|
user_last_name=None,
|
||||||
|
user_tag=None,
|
||||||
|
user_role=0,
|
||||||
|
user_stats=0,
|
||||||
|
user_rep=0,
|
||||||
|
):
|
||||||
if user_last_name is None:
|
if user_last_name is None:
|
||||||
user_name = user_first_name
|
user_name = user_first_name
|
||||||
else:
|
else:
|
||||||
user_name = user_first_name + " " + user_last_name
|
user_name = user_first_name + " " + user_last_name
|
||||||
|
|
||||||
user, created = Users.get_or_create(id=user_id, defaults={
|
user, created = Users.get_or_create(
|
||||||
'user_tag': user_tag,
|
id=user_id,
|
||||||
'user_name': user_name,
|
defaults={
|
||||||
'user_role': user_role,
|
"user_tag": user_tag,
|
||||||
'user_stats': user_stats,
|
"user_name": user_name,
|
||||||
'user_rep': user_rep
|
"user_role": user_role,
|
||||||
})
|
"user_stats": user_stats,
|
||||||
|
"user_rep": user_rep,
|
||||||
|
},
|
||||||
|
)
|
||||||
if not created:
|
if not created:
|
||||||
# Обновить существующего пользователя, если он уже существует
|
# Обновить существующего пользователя, если он уже существует
|
||||||
user.user_tag = user_tag
|
user.user_tag = user_tag
|
||||||
@ -84,26 +99,20 @@ def add_message(message: Message, message_ai_model=None):
|
|||||||
message_sender_id=message.from_user.id,
|
message_sender_id=message.from_user.id,
|
||||||
answer_to_message_id=answer_to_message_id,
|
answer_to_message_id=answer_to_message_id,
|
||||||
message_ai_model=message_ai_model,
|
message_ai_model=message_ai_model,
|
||||||
message_text=message.text
|
message_text=message.text,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def add_chat_stats(chat_id, date, messages_count):
|
def add_chat_stats(chat_id, date, messages_count):
|
||||||
ChatStats.create(
|
ChatStats.create(chat_id=chat_id, date=date, messages_count=messages_count)
|
||||||
chat_id=chat_id,
|
|
||||||
date=date,
|
|
||||||
messages_count=messages_count
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def add_user_stats(chat_id, user_id, date, messages_count):
|
def add_user_stats(chat_id, user_id, date, messages_count):
|
||||||
UserStats.create(
|
UserStats.create(
|
||||||
chat_id=chat_id,
|
chat_id=chat_id, user_id=user_id, date=date, messages_count=messages_count
|
||||||
user_id=user_id,
|
|
||||||
date=date,
|
|
||||||
messages_count=messages_count
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# Работа с таблицей чатов
|
# Работа с таблицей чатов
|
||||||
|
|
||||||
|
|
||||||
@ -125,6 +134,7 @@ def get_chat_all_stat(chat_id):
|
|||||||
chat = Chats.get_or_none(Chats.id == chat_id)
|
chat = Chats.get_or_none(Chats.id == chat_id)
|
||||||
return chat.chat_all_stat if chat else None
|
return chat.chat_all_stat if chat else None
|
||||||
|
|
||||||
|
|
||||||
# Работа с таблицей пользователей
|
# Работа с таблицей пользователей
|
||||||
|
|
||||||
|
|
||||||
@ -136,6 +146,7 @@ def get_user_tag(user_id):
|
|||||||
user = Users.get_or_none(Users.id == user_id)
|
user = Users.get_or_none(Users.id == user_id)
|
||||||
return user.user_tag if user else None
|
return user.user_tag if user else None
|
||||||
|
|
||||||
|
|
||||||
def get_user_id(user_tag):
|
def get_user_id(user_tag):
|
||||||
user = Users.get_or_none(Users.user_tag == user_tag)
|
user = Users.get_or_none(Users.user_tag == user_tag)
|
||||||
return user.id if user else None
|
return user.id if user else None
|
||||||
@ -179,32 +190,44 @@ def change_user_role(user_id, new_user_role):
|
|||||||
query = Users.update(user_role=new_user_role).where(Users.id == user_id)
|
query = Users.update(user_role=new_user_role).where(Users.id == user_id)
|
||||||
query.execute()
|
query.execute()
|
||||||
|
|
||||||
|
|
||||||
# Работа с таблицей сообщений
|
# Работа с таблицей сообщений
|
||||||
|
|
||||||
|
|
||||||
def get_message(message_chat_id, message_id):
|
def get_message(message_chat_id, message_id):
|
||||||
return Messages.get_or_none(Messages.message_chat_id == message_chat_id, Messages.message_id == message_id)
|
return Messages.get_or_none(
|
||||||
|
Messages.message_chat_id == message_chat_id, Messages.message_id == message_id
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_message_sender_id(message_chat_id, message_id):
|
def get_message_sender_id(message_chat_id, message_id):
|
||||||
message = Messages.get_or_none(Messages.message_chat_id == message_chat_id, Messages.message_id == message_id)
|
message = Messages.get_or_none(
|
||||||
|
Messages.message_chat_id == message_chat_id, Messages.message_id == message_id
|
||||||
|
)
|
||||||
return message.message_sender_id if message else None
|
return message.message_sender_id if message else None
|
||||||
|
|
||||||
|
|
||||||
def get_message_text(message_chat_id, message_id):
|
def get_message_text(message_chat_id, message_id):
|
||||||
message = Messages.get_or_none(Messages.message_chat_id == message_chat_id, Messages.message_id == message_id)
|
message = Messages.get_or_none(
|
||||||
|
Messages.message_chat_id == message_chat_id, Messages.message_id == message_id
|
||||||
|
)
|
||||||
return message.message_text if message else None
|
return message.message_text if message else None
|
||||||
|
|
||||||
|
|
||||||
def get_message_ai_model(message_chat_id, message_id):
|
def get_message_ai_model(message_chat_id, message_id):
|
||||||
message = Messages.get_or_none(Messages.message_chat_id == message_chat_id, Messages.message_id == message_id)
|
message = Messages.get_or_none(
|
||||||
|
Messages.message_chat_id == message_chat_id, Messages.message_id == message_id
|
||||||
|
)
|
||||||
return message.message_ai_model if message else None
|
return message.message_ai_model if message else None
|
||||||
|
|
||||||
|
|
||||||
def get_answer_to_message_id(message_chat_id, message_id):
|
def get_answer_to_message_id(message_chat_id, message_id):
|
||||||
message = Messages.get_or_none(Messages.message_chat_id == message_chat_id, Messages.message_id == message_id)
|
message = Messages.get_or_none(
|
||||||
|
Messages.message_chat_id == message_chat_id, Messages.message_id == message_id
|
||||||
|
)
|
||||||
return message.answer_to_message_id if message else None
|
return message.answer_to_message_id if message else None
|
||||||
|
|
||||||
|
|
||||||
# Работа с таблицей статистики чатов
|
# Работа с таблицей статистики чатов
|
||||||
|
|
||||||
|
|
||||||
@ -214,6 +237,7 @@ def get_chat_stats(chat_id):
|
|||||||
chat_stats[chat_stat.date] = chat_stat.messages_count
|
chat_stats[chat_stat.date] = chat_stat.messages_count
|
||||||
return chat_stats
|
return chat_stats
|
||||||
|
|
||||||
|
|
||||||
# Работа с таблицей статистики пользователей
|
# Работа с таблицей статистики пользователей
|
||||||
|
|
||||||
|
|
||||||
@ -223,26 +247,28 @@ def get_user_stats(user_id):
|
|||||||
user_stats[user_stat.date] = user_stat.messages_count
|
user_stats[user_stat.date] = user_stat.messages_count
|
||||||
return user_stats
|
return user_stats
|
||||||
|
|
||||||
|
|
||||||
# Функции обновления
|
# Функции обновления
|
||||||
|
|
||||||
|
|
||||||
def update_chat_all_stat(chat_id):
|
def update_chat_all_stat(chat_id):
|
||||||
query = Chats.update(chat_all_stat=Chats.chat_all_stat + 1).where(Chats.id == chat_id)
|
query = Chats.update(chat_all_stat=Chats.chat_all_stat + 1).where(
|
||||||
|
Chats.id == chat_id
|
||||||
|
)
|
||||||
query.execute()
|
query.execute()
|
||||||
|
|
||||||
|
|
||||||
def update_chat_stats(chat_id, date):
|
def update_chat_stats(chat_id, date):
|
||||||
chat_stats = ChatStats.get_or_none(ChatStats.chat_id == chat_id, ChatStats.date == date)
|
chat_stats = ChatStats.get_or_none(
|
||||||
|
ChatStats.chat_id == chat_id, ChatStats.date == date
|
||||||
|
)
|
||||||
if chat_stats:
|
if chat_stats:
|
||||||
query = ChatStats.update(messages_count=ChatStats.messages_count + 1).where(ChatStats.chat_id == chat_id,
|
query = ChatStats.update(messages_count=ChatStats.messages_count + 1).where(
|
||||||
ChatStats.date == date)
|
ChatStats.chat_id == chat_id, ChatStats.date == date
|
||||||
|
)
|
||||||
query.execute()
|
query.execute()
|
||||||
else:
|
else:
|
||||||
ChatStats.create(
|
ChatStats.create(chat_id=chat_id, date=date, messages_count=1)
|
||||||
chat_id=chat_id,
|
|
||||||
date=date,
|
|
||||||
messages_count=1
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def update_user_all_stat(user_id):
|
def update_user_all_stat(user_id):
|
||||||
@ -251,10 +277,7 @@ def update_user_all_stat(user_id):
|
|||||||
query = Users.update(user_stats=Users.user_stats + 1).where(Users.id == user_id)
|
query = Users.update(user_stats=Users.user_stats + 1).where(Users.id == user_id)
|
||||||
query.execute()
|
query.execute()
|
||||||
else:
|
else:
|
||||||
Users.create(
|
Users.create(id=user_id, user_stats=1)
|
||||||
id=user_id,
|
|
||||||
user_stats=1
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def update_user_rep(user_id):
|
def update_user_rep(user_id):
|
||||||
@ -263,24 +286,21 @@ def update_user_rep(user_id):
|
|||||||
query = Users.update(user_rep=Users.user_rep + 1).where(Users.id == user_id)
|
query = Users.update(user_rep=Users.user_rep + 1).where(Users.id == user_id)
|
||||||
query.execute()
|
query.execute()
|
||||||
else:
|
else:
|
||||||
Users.create(
|
Users.create(id=user_id, user_rep=1)
|
||||||
id=user_id,
|
|
||||||
user_rep=1
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def update_user_stats(chat_id, user_id, date):
|
def update_user_stats(chat_id, user_id, date):
|
||||||
user_stats = UserStats.get_or_none(UserStats.chat_id == chat_id, UserStats.user_id == user_id,
|
user_stats = UserStats.get_or_none(
|
||||||
UserStats.date == date)
|
UserStats.chat_id == chat_id,
|
||||||
|
UserStats.user_id == user_id,
|
||||||
|
UserStats.date == date,
|
||||||
|
)
|
||||||
if user_stats:
|
if user_stats:
|
||||||
query = UserStats.update(messages_count=UserStats.messages_count + 1).where(UserStats.chat_id == chat_id,
|
query = UserStats.update(messages_count=UserStats.messages_count + 1).where(
|
||||||
UserStats.user_id == user_id,
|
UserStats.chat_id == chat_id,
|
||||||
UserStats.date == date)
|
UserStats.user_id == user_id,
|
||||||
|
UserStats.date == date,
|
||||||
|
)
|
||||||
query.execute()
|
query.execute()
|
||||||
else:
|
else:
|
||||||
UserStats.create(
|
UserStats.create(chat_id=chat_id, user_id=user_id, date=date, messages_count=1)
|
||||||
chat_id=chat_id,
|
|
||||||
user_id=user_id,
|
|
||||||
date=date,
|
|
||||||
messages_count=1
|
|
||||||
)
|
|
||||||
|
@ -1 +0,0 @@
|
|||||||
|
|
@ -2,8 +2,8 @@ import peewee as pw
|
|||||||
|
|
||||||
|
|
||||||
class ChatStats(pw.Model):
|
class ChatStats(pw.Model):
|
||||||
class Meta:
|
class Meta: ...
|
||||||
...
|
|
||||||
chat_id = pw.IntegerField(null=False)
|
chat_id = pw.IntegerField(null=False)
|
||||||
date = pw.DateField(null=False)
|
date = pw.DateField(null=False)
|
||||||
messages_count = pw.IntegerField(null=False, default=0)
|
messages_count = pw.IntegerField(null=False, default=0)
|
||||||
|
@ -2,8 +2,8 @@ import peewee as pw
|
|||||||
|
|
||||||
|
|
||||||
class Chats(pw.Model):
|
class Chats(pw.Model):
|
||||||
class Meta:
|
class Meta: ...
|
||||||
...
|
|
||||||
chat_name = pw.CharField(null=False)
|
chat_name = pw.CharField(null=False)
|
||||||
chat_type = pw.IntegerField(null=False, default=10)
|
chat_type = pw.IntegerField(null=False, default=10)
|
||||||
chat_all_stat = pw.IntegerField(null=False)
|
chat_all_stat = pw.IntegerField(null=False)
|
||||||
|
@ -2,12 +2,11 @@ import peewee as pw
|
|||||||
|
|
||||||
|
|
||||||
class Messages(pw.Model):
|
class Messages(pw.Model):
|
||||||
class Meta:
|
class Meta: ...
|
||||||
...
|
|
||||||
message_chat_id = pw.IntegerField(null=False)
|
message_chat_id = pw.IntegerField(null=False)
|
||||||
message_id = pw.IntegerField(null=False)
|
message_id = pw.IntegerField(null=False)
|
||||||
message_sender_id = pw.IntegerField(null=False)
|
message_sender_id = pw.IntegerField(null=False)
|
||||||
answer_to_message_id = pw.IntegerField(null=True)
|
answer_to_message_id = pw.IntegerField(null=True)
|
||||||
message_ai_model = pw.TextField(null=True)
|
message_ai_model = pw.TextField(null=True)
|
||||||
message_text = pw.TextField(null=False)
|
message_text = pw.TextField(null=False)
|
||||||
|
|
||||||
|
@ -2,8 +2,8 @@ import peewee as pw
|
|||||||
|
|
||||||
|
|
||||||
class UserStats(pw.Model):
|
class UserStats(pw.Model):
|
||||||
class Meta:
|
class Meta: ...
|
||||||
...
|
|
||||||
chat_id = pw.IntegerField(null=False)
|
chat_id = pw.IntegerField(null=False)
|
||||||
user_id = pw.IntegerField(null=False)
|
user_id = pw.IntegerField(null=False)
|
||||||
date = pw.DateField(null=False)
|
date = pw.DateField(null=False)
|
||||||
|
@ -2,8 +2,8 @@ import peewee as pw
|
|||||||
|
|
||||||
|
|
||||||
class Users(pw.Model):
|
class Users(pw.Model):
|
||||||
class Meta:
|
class Meta: ...
|
||||||
...
|
|
||||||
user_tag = pw.CharField(null=True)
|
user_tag = pw.CharField(null=True)
|
||||||
user_name = pw.CharField(null=False) # до 255 символов
|
user_name = pw.CharField(null=False) # до 255 символов
|
||||||
user_role = pw.IntegerField(null=True, default=3)
|
user_role = pw.IntegerField(null=True, default=3)
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
import unittest
|
# flake8: noqa
|
||||||
import os
|
|
||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
|
||||||
from ..db_api import *
|
|
||||||
from ...exceptions.module_exceptions import MissingModuleName, NotExpectedModuleName
|
from ...exceptions.module_exceptions import MissingModuleName, NotExpectedModuleName
|
||||||
|
from ..db_api import *
|
||||||
|
|
||||||
|
|
||||||
class TestDatabaseAPI(unittest.TestCase):
|
class TestDatabaseAPI(unittest.TestCase):
|
||||||
@ -13,6 +15,7 @@ class TestDatabaseAPI(unittest.TestCase):
|
|||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.database, cls.path = connect_database(is_test=True, module="database")
|
cls.database, cls.path = connect_database(is_test=True, module="database")
|
||||||
create_tables(cls.database)
|
create_tables(cls.database)
|
||||||
|
|
||||||
def test_fail_connect(cls):
|
def test_fail_connect(cls):
|
||||||
with cls.assertRaises(MissingModuleName):
|
with cls.assertRaises(MissingModuleName):
|
||||||
cls.database, cls.path = connect_database(is_test=True)
|
cls.database, cls.path = connect_database(is_test=True)
|
||||||
@ -35,8 +38,12 @@ class TestDatabaseAPI(unittest.TestCase):
|
|||||||
self.assertEqual(chat2.chat_role, 1)
|
self.assertEqual(chat2.chat_role, 1)
|
||||||
|
|
||||||
def test_add_and_get_message(self):
|
def test_add_and_get_message(self):
|
||||||
add_message(message_id=1, message_text="Test Message 1", message_sender=1, answer_id=2)
|
add_message(
|
||||||
add_message(message_id=2, message_text="Test Message 2", message_sender=2, answer_id=1)
|
message_id=1, message_text="Test Message 1", message_sender=1, answer_id=2
|
||||||
|
)
|
||||||
|
add_message(
|
||||||
|
message_id=2, message_text="Test Message 2", message_sender=2, answer_id=1
|
||||||
|
)
|
||||||
|
|
||||||
message1 = get_message(1)
|
message1 = get_message(1)
|
||||||
self.assertIsNotNone(message1)
|
self.assertIsNotNone(message1)
|
||||||
@ -49,8 +56,22 @@ class TestDatabaseAPI(unittest.TestCase):
|
|||||||
self.assertEqual(message2.message_text, "Test Message 2")
|
self.assertEqual(message2.message_text, "Test Message 2")
|
||||||
|
|
||||||
def test_add_and_get_user(self):
|
def test_add_and_get_user(self):
|
||||||
add_user(user_id=100, user_name="TestUser1", user_tag="TestTag1", user_role=0, user_stats=10, user_rep=5)
|
add_user(
|
||||||
add_user(user_id=101, user_name="TestUser2", user_tag="TestTag2", user_role=1, user_stats=20, user_rep=10)
|
user_id=100,
|
||||||
|
user_name="TestUser1",
|
||||||
|
user_tag="TestTag1",
|
||||||
|
user_role=0,
|
||||||
|
user_stats=10,
|
||||||
|
user_rep=5,
|
||||||
|
)
|
||||||
|
add_user(
|
||||||
|
user_id=101,
|
||||||
|
user_name="TestUser2",
|
||||||
|
user_tag="TestTag2",
|
||||||
|
user_role=1,
|
||||||
|
user_stats=20,
|
||||||
|
user_rep=10,
|
||||||
|
)
|
||||||
|
|
||||||
user1 = get_user(100)
|
user1 = get_user(100)
|
||||||
self.assertIsNotNone(user1)
|
self.assertIsNotNone(user1)
|
||||||
@ -63,8 +84,22 @@ class TestDatabaseAPI(unittest.TestCase):
|
|||||||
self.assertEqual(user2.user_name, "TestUser2")
|
self.assertEqual(user2.user_name, "TestUser2")
|
||||||
|
|
||||||
def test_get_user_role(self):
|
def test_get_user_role(self):
|
||||||
add_user(user_id=102, user_name="TestUser3", user_tag="TestTag3", user_role=0, user_stats=30, user_rep=15)
|
add_user(
|
||||||
add_user(user_id=103, user_name="TestUser4", user_tag="TestTag4", user_role=1, user_stats=40, user_rep=20)
|
user_id=102,
|
||||||
|
user_name="TestUser3",
|
||||||
|
user_tag="TestTag3",
|
||||||
|
user_role=0,
|
||||||
|
user_stats=30,
|
||||||
|
user_rep=15,
|
||||||
|
)
|
||||||
|
add_user(
|
||||||
|
user_id=103,
|
||||||
|
user_name="TestUser4",
|
||||||
|
user_tag="TestTag4",
|
||||||
|
user_role=1,
|
||||||
|
user_stats=40,
|
||||||
|
user_rep=20,
|
||||||
|
)
|
||||||
|
|
||||||
user_role1 = get_user_role(102)
|
user_role1 = get_user_role(102)
|
||||||
self.assertEqual(user_role1, 0)
|
self.assertEqual(user_role1, 0)
|
||||||
@ -73,12 +108,26 @@ class TestDatabaseAPI(unittest.TestCase):
|
|||||||
self.assertEqual(user_role2, 1)
|
self.assertEqual(user_role2, 1)
|
||||||
|
|
||||||
def test_change_user_name(self):
|
def test_change_user_name(self):
|
||||||
add_user(user_id=104, user_name="OldName1", user_tag="TestTag5", user_role=0, user_stats=50, user_rep=25)
|
add_user(
|
||||||
|
user_id=104,
|
||||||
|
user_name="OldName1",
|
||||||
|
user_tag="TestTag5",
|
||||||
|
user_role=0,
|
||||||
|
user_stats=50,
|
||||||
|
user_rep=25,
|
||||||
|
)
|
||||||
change_user_name(104, "NewName1")
|
change_user_name(104, "NewName1")
|
||||||
updated_user1 = get_user(104)
|
updated_user1 = get_user(104)
|
||||||
self.assertEqual(updated_user1.user_name, "NewName1")
|
self.assertEqual(updated_user1.user_name, "NewName1")
|
||||||
|
|
||||||
add_user(user_id=105, user_name="OldName2", user_tag="TestTag6", user_role=1, user_stats=60, user_rep=30)
|
add_user(
|
||||||
|
user_id=105,
|
||||||
|
user_name="OldName2",
|
||||||
|
user_tag="TestTag6",
|
||||||
|
user_role=1,
|
||||||
|
user_stats=60,
|
||||||
|
user_rep=30,
|
||||||
|
)
|
||||||
change_user_name(105, "NewName2")
|
change_user_name(105, "NewName2")
|
||||||
updated_user2 = get_user(105)
|
updated_user2 = get_user(105)
|
||||||
self.assertEqual(updated_user2.user_name, "NewName2")
|
self.assertEqual(updated_user2.user_name, "NewName2")
|
||||||
@ -89,5 +138,5 @@ class TestDatabaseAPI(unittest.TestCase):
|
|||||||
os.system(f"rm {cls.path}")
|
os.system(f"rm {cls.path}")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
@ -10,4 +10,3 @@ class NotExpectedModuleName(BaseException):
|
|||||||
self.message = "Не ожидалось название директории модуля"
|
self.message = "Не ожидалось название директории модуля"
|
||||||
|
|
||||||
super().__init__(self.message)
|
super().__init__(self.message)
|
||||||
|
|
||||||
|
@ -1,18 +1,23 @@
|
|||||||
|
from aiogram import Bot
|
||||||
from aiogram.filters import BaseFilter
|
from aiogram.filters import BaseFilter
|
||||||
from aiogram.types import Message
|
from aiogram.types import Message
|
||||||
from aiogram import Bot
|
|
||||||
|
|
||||||
from src.modules.standard.roles.roles import Roles
|
|
||||||
from src.modules.standard.config.config import get_aproved_chat_id
|
|
||||||
from src.core.logger import log
|
from src.core.logger import log
|
||||||
|
from src.modules.standard.config.config import get_aproved_chat_id
|
||||||
|
from src.modules.standard.roles.roles import Roles
|
||||||
|
|
||||||
|
|
||||||
class ChatModerOrAdminFilter(BaseFilter):
|
class ChatModerOrAdminFilter(BaseFilter):
|
||||||
async def __call__(self, message: Message, bot: Bot) -> bool:
|
async def __call__(self, message: Message, bot: Bot) -> bool:
|
||||||
user_id = message.from_user.id
|
user_id = message.from_user.id
|
||||||
roles = Roles()
|
roles = Roles()
|
||||||
admins = await bot.get_chat_administrators(message.chat.id)
|
admins = await bot.get_chat_administrators(message.chat.id)
|
||||||
return await roles.check_admin_permission(user_id) or \
|
return (
|
||||||
await roles.check_moderator_permission(user_id) or any(user_id == admin.user.id for admin in admins)
|
await roles.check_admin_permission(user_id)
|
||||||
|
or await roles.check_moderator_permission(user_id)
|
||||||
|
or any(user_id == admin.user.id for admin in admins)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class ChatNotInApproveFilter(BaseFilter):
|
class ChatNotInApproveFilter(BaseFilter):
|
||||||
async def __call__(self, message: Message, bot: Bot) -> bool:
|
async def __call__(self, message: Message, bot: Bot) -> bool:
|
||||||
|
@ -1,13 +1,20 @@
|
|||||||
|
# flake8: noqa
|
||||||
|
|
||||||
from aiogram import Bot
|
from aiogram import Bot
|
||||||
from aiogram.types import Message
|
from aiogram.types import Message
|
||||||
from src.modules.standard.config.config import get_user_role_name
|
|
||||||
from src.modules.standard.roles.roles import Roles
|
|
||||||
from src.modules.standard.database.db_api import *
|
|
||||||
from src.core.logger import log
|
from src.core.logger import log
|
||||||
|
from src.modules.standard.config.config import get_user_role_name
|
||||||
|
from src.modules.standard.database.db_api import *
|
||||||
|
from src.modules.standard.roles.roles import Roles
|
||||||
|
|
||||||
|
|
||||||
async def get_info_answer_by_id(message: Message, bot: Bot, user_id: int):
|
async def get_info_answer_by_id(message: Message, bot: Bot, user_id: int):
|
||||||
if get_message_ai_model(message.chat.id, message.message_id) is not None:
|
if get_message_ai_model(message.chat.id, message.message_id) is not None:
|
||||||
await message.reply("Это сообщение было сгенерировано ботом используя модель: " + get_message_ai_model(message.chat.id, message.message_id))
|
await message.reply(
|
||||||
|
"Это сообщение было сгенерировано ботом используя модель: "
|
||||||
|
+ get_message_ai_model(message.chat.id, message.message_id)
|
||||||
|
)
|
||||||
elif user_id == bot.id:
|
elif user_id == bot.id:
|
||||||
await message.reply("Это сообщение было отправлено ботом")
|
await message.reply("Это сообщение было отправлено ботом")
|
||||||
elif get_user(user_id) is None:
|
elif get_user(user_id) is None:
|
||||||
@ -44,11 +51,14 @@ async def get_user_info(message: Message, bot: Bot):
|
|||||||
else:
|
else:
|
||||||
await get_info_answer_by_id(message, bot, message.from_user.id)
|
await get_info_answer_by_id(message, bot, message.from_user.id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
await message.reply("В вашем запросе что-то пошло не так,"
|
await message.reply(
|
||||||
" попробуйте запросить информацию о пользователе по его тегу или ответив на его сообщение")
|
"В вашем запросе что-то пошло не так,"
|
||||||
|
" попробуйте запросить информацию о пользователе по его тегу или ответив на его сообщение"
|
||||||
|
)
|
||||||
# print(e)
|
# print(e)
|
||||||
await log(e)
|
await log(e)
|
||||||
|
|
||||||
|
|
||||||
async def get_chat_info(message: Message, bot: Bot):
|
async def get_chat_info(message: Message, bot: Bot):
|
||||||
answer = (
|
answer = (
|
||||||
f"*Название чата:* {message.chat.title}\n"
|
f"*Название чата:* {message.chat.title}\n"
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
from aiogram import Router, F
|
# flake8: noqa
|
||||||
|
|
||||||
from src.modules.standard.info.handlers import get_user_info, get_chat_info
|
from aiogram import F, Router
|
||||||
|
|
||||||
|
from src.modules.standard.info.handlers import get_chat_info, get_user_info
|
||||||
|
|
||||||
router = Router()
|
router = Router()
|
||||||
|
|
||||||
|
@ -1,9 +1,15 @@
|
|||||||
from aiogram import Router, F, Bot, types
|
# flake8: noqa
|
||||||
|
|
||||||
|
from aiogram import Bot, F, Router, types
|
||||||
|
|
||||||
from src.modules.external.yandexgpt.handlers import answer_to_message
|
|
||||||
from src.modules.standard.database.db_api import *
|
|
||||||
from src.modules.standard.config.config import get_yandexgpt_start_words, get_yandexgpt_in_words, get_aproved_chat_id
|
|
||||||
from src.core.logger import log
|
from src.core.logger import log
|
||||||
|
from src.modules.external.yandexgpt.handlers import answer_to_message
|
||||||
|
from src.modules.standard.config.config import (
|
||||||
|
get_aproved_chat_id,
|
||||||
|
get_yandexgpt_in_words,
|
||||||
|
get_yandexgpt_start_words,
|
||||||
|
)
|
||||||
|
from src.modules.standard.database.db_api import *
|
||||||
|
|
||||||
|
|
||||||
async def chat_check(message: types.Message):
|
async def chat_check(message: types.Message):
|
||||||
@ -19,7 +25,9 @@ async def chat_check(message: types.Message):
|
|||||||
await log(f"Chat added: {message.chat.id} {message.chat.title}")
|
await log(f"Chat added: {message.chat.id} {message.chat.title}")
|
||||||
else:
|
else:
|
||||||
# print(f"Chat not in approve list: {message.chat.id} {message.chat.title}")
|
# print(f"Chat not in approve list: {message.chat.id} {message.chat.title}")
|
||||||
await log(f"Chat not in approve list: {message.chat.id} {message.chat.title}")
|
await log(
|
||||||
|
f"Chat not in approve list: {message.chat.id} {message.chat.title}"
|
||||||
|
)
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
# Проверяем обновление названия чата
|
# Проверяем обновление названия чата
|
||||||
@ -34,6 +42,7 @@ async def chat_check(message: types.Message):
|
|||||||
await log(f"Chat already exists: {message.chat.id} {message.chat.title}")
|
await log(f"Chat already exists: {message.chat.id} {message.chat.title}")
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
async def user_check(message: types.Message):
|
async def user_check(message: types.Message):
|
||||||
# Проверка наличия id пользователя в базе данных пользователей
|
# Проверка наличия id пользователя в базе данных пользователей
|
||||||
# Если пользователя нет в базе данных, то добавляем его
|
# Если пользователя нет в базе данных, то добавляем его
|
||||||
@ -42,17 +51,24 @@ async def user_check(message: types.Message):
|
|||||||
if message.from_user.last_name is None:
|
if message.from_user.last_name is None:
|
||||||
current_user_name = message.from_user.first_name
|
current_user_name = message.from_user.first_name
|
||||||
else:
|
else:
|
||||||
current_user_name = message.from_user.first_name + " " + message.from_user.last_name
|
current_user_name = (
|
||||||
|
message.from_user.first_name + " " + message.from_user.last_name
|
||||||
|
)
|
||||||
|
|
||||||
if get_user(message.from_user.id) is None:
|
if get_user(message.from_user.id) is None:
|
||||||
add_user(message.from_user.id,
|
add_user(
|
||||||
message.from_user.first_name, message.from_user.last_name,
|
message.from_user.id,
|
||||||
message.from_user.username)
|
message.from_user.first_name,
|
||||||
|
message.from_user.last_name,
|
||||||
|
message.from_user.username,
|
||||||
|
)
|
||||||
# print(f"User added: {message.from_user.id} {message.from_user.first_name} {message.from_user.last_name}")
|
# print(f"User added: {message.from_user.id} {message.from_user.first_name} {message.from_user.last_name}")
|
||||||
await log(f"User added: {message.from_user.id} {current_user_name}")
|
await log(f"User added: {message.from_user.id} {current_user_name}")
|
||||||
else:
|
else:
|
||||||
# print(f"User already exists: {message.from_user.id} {message.from_user.first_name} {message.from_user.last_name} {message.from_user.username}")
|
# print(f"User already exists: {message.from_user.id} {message.from_user.first_name} {message.from_user.last_name} {message.from_user.username}")
|
||||||
await log(f"User already exists: {message.from_user.id} {current_user_name} {message.from_user.username}")
|
await log(
|
||||||
|
f"User already exists: {message.from_user.id} {current_user_name} {message.from_user.username}"
|
||||||
|
)
|
||||||
# Проверяем обновление имени пользователя
|
# Проверяем обновление имени пользователя
|
||||||
if get_user_name(message.from_user.id) != current_user_name:
|
if get_user_name(message.from_user.id) != current_user_name:
|
||||||
change_user_name(message.from_user.id, current_user_name)
|
change_user_name(message.from_user.id, current_user_name)
|
||||||
@ -62,9 +78,12 @@ async def user_check(message: types.Message):
|
|||||||
if get_user_tag(message.from_user.id) != message.from_user.username:
|
if get_user_tag(message.from_user.id) != message.from_user.username:
|
||||||
change_user_tag(message.from_user.id, message.from_user.username)
|
change_user_tag(message.from_user.id, message.from_user.username)
|
||||||
# print(f"User updated: {message.from_user.id} {message.from_user.username}")
|
# print(f"User updated: {message.from_user.id} {message.from_user.username}")
|
||||||
await log(f"User tag updated: {message.from_user.id} {message.from_user.username}")
|
await log(
|
||||||
|
f"User tag updated: {message.from_user.id} {message.from_user.username}"
|
||||||
|
)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
async def add_stats(message: types.Message):
|
async def add_stats(message: types.Message):
|
||||||
# Добавляем пользователю и чату статистику
|
# Добавляем пользователю и чату статистику
|
||||||
update_chat_all_stat(message.chat.id)
|
update_chat_all_stat(message.chat.id)
|
||||||
@ -79,8 +98,9 @@ async def message_processing(message: types.Message, bot: Bot):
|
|||||||
add_message(message)
|
add_message(message)
|
||||||
# Если сообщение в начале содержит слово из списка или внутри сообщения содержится слово из списка или сообщение отвечает на сообщение бота
|
# Если сообщение в начале содержит слово из списка или внутри сообщения содержится слово из списка или сообщение отвечает на сообщение бота
|
||||||
|
|
||||||
if ((message.text.split(" ")[0] in get_yandexgpt_start_words())
|
if (message.text.split(" ")[0] in get_yandexgpt_start_words()) or (
|
||||||
or (any(word in message.text for word in get_yandexgpt_in_words()))):
|
any(word in message.text for word in get_yandexgpt_in_words())
|
||||||
|
):
|
||||||
# print("message_processing")
|
# print("message_processing")
|
||||||
await log("message_processing")
|
await log("message_processing")
|
||||||
await answer_to_message(message, bot)
|
await answer_to_message(message, bot)
|
||||||
@ -92,8 +112,6 @@ async def message_processing(message: types.Message, bot: Bot):
|
|||||||
await answer_to_message(message, bot)
|
await answer_to_message(message, bot)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
router = Router()
|
router = Router()
|
||||||
# Если сообщение содержит текст то вызывается функция message_processing
|
# Если сообщение содержит текст то вызывается функция message_processing
|
||||||
router.message.register(message_processing, F.text)
|
router.message.register(message_processing, F.text)
|
@ -1,12 +1,15 @@
|
|||||||
|
# flake8: noqa
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import aiohttp
|
|
||||||
import aiogram
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
import aiogram
|
||||||
|
import aiohttp
|
||||||
|
|
||||||
from ...standard.config.config import *
|
from ...standard.config.config import *
|
||||||
from ...standard.roles.roles import *
|
from ...standard.roles.roles import *
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Moderation:
|
class Moderation:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
access_rights = get_access_rights()
|
access_rights = get_access_rights()
|
||||||
@ -19,24 +22,24 @@ class Moderation:
|
|||||||
|
|
||||||
async def time_to_seconds(time):
|
async def time_to_seconds(time):
|
||||||
# Конвертация текстового указания времени по типу 3h, 5m, 10s в минуты
|
# Конвертация текстового указания времени по типу 3h, 5m, 10s в минуты
|
||||||
if time[-1] == 'd':
|
if time[-1] == "d":
|
||||||
return int(time[:-1]) * 86400
|
return int(time[:-1]) * 86400
|
||||||
elif time[-1] == 'h':
|
elif time[-1] == "h":
|
||||||
return int(time[:-1]) * 3600
|
return int(time[:-1]) * 3600
|
||||||
elif time[-1] == 'm':
|
elif time[-1] == "m":
|
||||||
return int(time[:-1]) * 60
|
return int(time[:-1]) * 60
|
||||||
elif time[-1] == 's':
|
elif time[-1] == "s":
|
||||||
return int(time[:-1])
|
return int(time[:-1])
|
||||||
|
|
||||||
async def short_time_to_time(self, time):
|
async def short_time_to_time(self, time):
|
||||||
# Конвертация времени в длинное название
|
# Конвертация времени в длинное название
|
||||||
if time[-1] == 'd':
|
if time[-1] == "d":
|
||||||
return str(f"{time[0:-1]} дней")
|
return str(f"{time[0:-1]} дней")
|
||||||
elif time[-1] == 'h':
|
elif time[-1] == "h":
|
||||||
return str(f"{time[0:-1]} часов")
|
return str(f"{time[0:-1]} часов")
|
||||||
elif time[-1] == 'm':
|
elif time[-1] == "m":
|
||||||
return str(f"{time[0:-1]} минут")
|
return str(f"{time[0:-1]} минут")
|
||||||
elif time[-1] == 's':
|
elif time[-1] == "s":
|
||||||
return str(f"{time[0:-1]} секунд")
|
return str(f"{time[0:-1]} секунд")
|
||||||
|
|
||||||
async def delete_message(self, chat_id, message_id, bot: aiogram.Bot):
|
async def delete_message(self, chat_id, message_id, bot: aiogram.Bot):
|
||||||
@ -45,6 +48,7 @@ class Moderation:
|
|||||||
async def ban_user(self, chat_id, user_id, bot: aiogram.Bot):
|
async def ban_user(self, chat_id, user_id, bot: aiogram.Bot):
|
||||||
await bot.ban_chat_member(chat_id, user_id)
|
await bot.ban_chat_member(chat_id, user_id)
|
||||||
|
|
||||||
|
|
||||||
async def mute_user(chat_id, user_id, time, bot: aiogram.Bot):
|
async def mute_user(chat_id, user_id, time, bot: aiogram.Bot):
|
||||||
mutePermissions = {
|
mutePermissions = {
|
||||||
"can_send_messages": False,
|
"can_send_messages": False,
|
||||||
@ -60,16 +64,19 @@ async def mute_user(chat_id, user_id, time, bot: aiogram.Bot):
|
|||||||
"can_change_info": False,
|
"can_change_info": False,
|
||||||
"can_invite_users": False,
|
"can_invite_users": False,
|
||||||
"can_pin_messages": False,
|
"can_pin_messages": False,
|
||||||
"can_manage_topics": False
|
"can_manage_topics": False,
|
||||||
}
|
}
|
||||||
end_time = time + int(time.time())
|
end_time = time + int(time.time())
|
||||||
await bot.restrict_chat_member(chat_id, user_id, until_date=end_time, **mutePermissions)
|
await bot.restrict_chat_member(
|
||||||
|
chat_id, user_id, until_date=end_time, **mutePermissions
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def unmute_user(chat_id, user_id, bot: aiogram.Bot):
|
async def unmute_user(chat_id, user_id, bot: aiogram.Bot):
|
||||||
await bot.restrict_chat_member(chat_id, user_id, use_independent_chat_permissions=True)
|
await bot.restrict_chat_member(
|
||||||
|
chat_id, user_id, use_independent_chat_permissions=True
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def ban_user(chat_id, user_id, bot: aiogram.Bot):
|
async def ban_user(chat_id, user_id, bot: aiogram.Bot):
|
||||||
await bot.ban_chat_member(chat_id, user_id)
|
await bot.ban_chat_member(chat_id, user_id)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
from ..database.db_api import get_user_role
|
|
||||||
from ..config.config import get_config
|
from ..config.config import get_config
|
||||||
|
from ..database.db_api import get_user_role
|
||||||
|
|
||||||
yaml_load = get_config()
|
yaml_load = get_config()
|
||||||
|
|
||||||
|
|
||||||
class Roles:
|
class Roles:
|
||||||
user = "USER"
|
user = "USER"
|
||||||
moderator = "MODERATOR"
|
moderator = "MODERATOR"
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
from ...database.db_api import create_tables, connect_database, add_user
|
|
||||||
|
from ...database.db_api import add_user, connect_database, create_tables
|
||||||
from ..roles import Roles
|
from ..roles import Roles
|
||||||
|
|
||||||
|
|
||||||
@ -14,10 +15,39 @@ class TestRoles(unittest.IsolatedAsyncioTestCase):
|
|||||||
cls.database, cls.path = connect_database(is_test=True, module="roles")
|
cls.database, cls.path = connect_database(is_test=True, module="roles")
|
||||||
create_tables(cls.database)
|
create_tables(cls.database)
|
||||||
|
|
||||||
add_user(user_id=1, user_name="TestUser1", user_tag="TestTag1", user_role=0, user_stats=30, user_rep=15)
|
add_user(
|
||||||
add_user(user_id=2, user_name="TestUser3", user_tag="TestTag3", user_role=1, user_stats=30, user_rep=15)
|
user_id=1,
|
||||||
add_user(user_id=3, user_name="TestUser4", user_tag="TestTag4", user_role=2, user_stats=30, user_rep=15)
|
user_name="TestUser1",
|
||||||
add_user(user_id=4, user_name="TestUser2", user_tag="TestTag2", user_role=3, user_stats=30, user_rep=15)
|
user_tag="TestTag1",
|
||||||
|
user_role=0,
|
||||||
|
user_stats=30,
|
||||||
|
user_rep=15,
|
||||||
|
)
|
||||||
|
add_user(
|
||||||
|
user_id=2,
|
||||||
|
user_name="TestUser3",
|
||||||
|
user_tag="TestTag3",
|
||||||
|
user_role=1,
|
||||||
|
user_stats=30,
|
||||||
|
user_rep=15,
|
||||||
|
)
|
||||||
|
add_user(
|
||||||
|
user_id=3,
|
||||||
|
user_name="TestUser4",
|
||||||
|
user_tag="TestTag4",
|
||||||
|
user_role=2,
|
||||||
|
user_stats=30,
|
||||||
|
user_rep=15,
|
||||||
|
)
|
||||||
|
add_user(
|
||||||
|
user_id=4,
|
||||||
|
user_name="TestUser2",
|
||||||
|
user_tag="TestTag2",
|
||||||
|
user_role=3,
|
||||||
|
user_stats=30,
|
||||||
|
user_rep=15,
|
||||||
|
)
|
||||||
|
|
||||||
async def test_check_admin_permission(cls):
|
async def test_check_admin_permission(cls):
|
||||||
cls.assertTrue(await cls.roles.check_admin_permission(1))
|
cls.assertTrue(await cls.roles.check_admin_permission(1))
|
||||||
cls.assertFalse(await cls.roles.check_admin_permission(2))
|
cls.assertFalse(await cls.roles.check_admin_permission(2))
|
||||||
@ -32,7 +62,9 @@ class TestRoles(unittest.IsolatedAsyncioTestCase):
|
|||||||
|
|
||||||
async def test_get_role_name(cls):
|
async def test_get_role_name(cls):
|
||||||
cls.assertEqual(await cls.roles.get_role_name(cls.roles.admin_role_id), "ADMIN")
|
cls.assertEqual(await cls.roles.get_role_name(cls.roles.admin_role_id), "ADMIN")
|
||||||
cls.assertEqual(await cls.roles.get_role_name(cls.roles.moderator_role_id), "MODERATOR")
|
cls.assertEqual(
|
||||||
|
await cls.roles.get_role_name(cls.roles.moderator_role_id), "MODERATOR"
|
||||||
|
)
|
||||||
cls.assertEqual(await cls.roles.get_role_name(cls.roles.user_role_id), "USER")
|
cls.assertEqual(await cls.roles.get_role_name(cls.roles.user_role_id), "USER")
|
||||||
cls.assertEqual(await cls.roles.get_role_name(cls.roles.bot_role_id), "BOT")
|
cls.assertEqual(await cls.roles.get_role_name(cls.roles.bot_role_id), "BOT")
|
||||||
with cls.assertRaises(ValueError):
|
with cls.assertRaises(ValueError):
|
||||||
@ -44,7 +76,5 @@ class TestRoles(unittest.IsolatedAsyncioTestCase):
|
|||||||
os.system(f"rm {cls.path}")
|
os.system(f"rm {cls.path}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
@ -1,13 +1,19 @@
|
|||||||
|
# flake8: noqa
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import random
|
||||||
|
from threading import Thread
|
||||||
|
|
||||||
from aiogram import Bot
|
from aiogram import Bot
|
||||||
from aiogram.types import Message
|
from aiogram.types import Message
|
||||||
from src.modules.standard.config.config import get_telegram_check_bot
|
|
||||||
from src.modules.standard.roles.roles import Roles
|
|
||||||
from src.modules.standard.database.db_api import *
|
|
||||||
from src.modules.standard.moderation.moderation import mute_user, unmute_user, ban_user
|
|
||||||
from aiogram.utils.keyboard import InlineKeyboardBuilder
|
|
||||||
from aiogram.types import inline_keyboard_button as types
|
from aiogram.types import inline_keyboard_button as types
|
||||||
import random, asyncio
|
from aiogram.utils.keyboard import InlineKeyboardBuilder
|
||||||
from threading import Thread
|
|
||||||
|
from src.modules.standard.config.config import get_telegram_check_bot
|
||||||
|
from src.modules.standard.database.db_api import *
|
||||||
|
from src.modules.standard.moderation.moderation import ban_user, mute_user, unmute_user
|
||||||
|
from src.modules.standard.roles.roles import Roles
|
||||||
|
|
||||||
|
|
||||||
async def create_math_task():
|
async def create_math_task():
|
||||||
first_number = random.randint(1, 100)
|
first_number = random.randint(1, 100)
|
||||||
@ -22,6 +28,7 @@ async def create_math_task():
|
|||||||
random.shuffle(fake_answers)
|
random.shuffle(fake_answers)
|
||||||
return [answer, first_number, second_number, fake_answers]
|
return [answer, first_number, second_number, fake_answers]
|
||||||
|
|
||||||
|
|
||||||
async def ban_user_timer(chat_id: int, user_id: int, time: int, bot: Bot):
|
async def ban_user_timer(chat_id: int, user_id: int, time: int, bot: Bot):
|
||||||
await asyncio.sleep(time)
|
await asyncio.sleep(time)
|
||||||
if get_user(user_id) is not None:
|
if get_user(user_id) is not None:
|
||||||
@ -30,8 +37,6 @@ async def ban_user_timer(chat_id: int, user_id: int, time: int, bot: Bot):
|
|||||||
await ban_user()
|
await ban_user()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async def check_new_user(message: Message, bot: Bot):
|
async def check_new_user(message: Message, bot: Bot):
|
||||||
print("check_new_user")
|
print("check_new_user")
|
||||||
if get_telegram_check_bot():
|
if get_telegram_check_bot():
|
||||||
@ -39,7 +44,10 @@ async def check_new_user(message: Message, bot: Bot):
|
|||||||
|
|
||||||
if get_user(message.from_user.id) is None:
|
if get_user(message.from_user.id) is None:
|
||||||
# Выдаём пользователю ограничение на отправку сообщений на 3 минуты
|
# Выдаём пользователю ограничение на отправку сообщений на 3 минуты
|
||||||
ban_task = Thread(target=ban_user_timer, args=(message.chat.id, message.from_user.id, 180, bot))
|
ban_task = Thread(
|
||||||
|
target=ban_user_timer,
|
||||||
|
args=(message.chat.id, message.from_user.id, 180, bot),
|
||||||
|
)
|
||||||
ban_task.start()
|
ban_task.start()
|
||||||
# Создаём задачу с отложенным выполнением на 3 минуты
|
# Создаём задачу с отложенным выполнением на 3 минуты
|
||||||
|
|
||||||
@ -48,27 +56,31 @@ async def check_new_user(message: Message, bot: Bot):
|
|||||||
builder = InlineKeyboardBuilder()
|
builder = InlineKeyboardBuilder()
|
||||||
for answer in math_task[3]:
|
for answer in math_task[3]:
|
||||||
if answer == math_task[0]:
|
if answer == math_task[0]:
|
||||||
builder.add(types.InlineKeyboardButton(
|
builder.add(
|
||||||
text=answer,
|
types.InlineKeyboardButton(
|
||||||
callback_data=f"check_math_task_true")
|
text=answer, callback_data=f"check_math_task_true"
|
||||||
|
)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
builder.add(types.InlineKeyboardButton(
|
builder.add(
|
||||||
text=answer,
|
types.InlineKeyboardButton(
|
||||||
callback_data=f"check_math_task_false")
|
text=answer, callback_data=f"check_math_task_false"
|
||||||
|
)
|
||||||
)
|
)
|
||||||
await message.reply(
|
await message.reply(
|
||||||
f"Приветствую, {message.from_user.first_name}!\n"
|
f"Приветствую, {message.from_user.first_name}!\n"
|
||||||
f"Для продолжения работы с ботом, пожалуйста, решите математический пример в течении 3х минут:\n"
|
f"Для продолжения работы с ботом, пожалуйста, решите математический пример в течении 3х минут:\n"
|
||||||
f"*{text}*",
|
f"*{text}*",
|
||||||
reply_markup=builder.as_markup()
|
reply_markup=builder.as_markup(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def math_task_true(message: Message, bot: Bot):
|
async def math_task_true(message: Message, bot: Bot):
|
||||||
await message.reply(f"Верно! Добро пожаловать в чат {message.from_user.first_name}")
|
await message.reply(f"Верно! Добро пожаловать в чат {message.from_user.first_name}")
|
||||||
await unmute_user(message.chat.id, message.from_user.id, bot)
|
await unmute_user(message.chat.id, message.from_user.id, bot)
|
||||||
add_user(message.from_user.id,
|
add_user(
|
||||||
message.from_user.first_name + ' ' + message.from_user.last_name,
|
message.from_user.id,
|
||||||
message.from_user.username)
|
message.from_user.first_name + " " + message.from_user.last_name,
|
||||||
|
message.from_user.username,
|
||||||
|
)
|
||||||
pass
|
pass
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from aiogram import Router, F
|
from aiogram import F, Router
|
||||||
|
|
||||||
from src.modules.standard.welcome.handlers import check_new_user
|
from src.modules.standard.welcome.handlers import check_new_user
|
||||||
|
|
||||||
@ -7,4 +7,6 @@ router = Router()
|
|||||||
# Если в чат пришел новый пользователь
|
# Если в чат пришел новый пользователь
|
||||||
router.message.register(check_new_user, F.new_chat_members.exists())
|
router.message.register(check_new_user, F.new_chat_members.exists())
|
||||||
# Ловин колбеки от кнопок с callback_data=f"check_math_task_true"
|
# Ловин колбеки от кнопок с callback_data=f"check_math_task_true"
|
||||||
router.callback_query.register(check_new_user, F.callback_data == "check_math_task_true")
|
router.callback_query.register(
|
||||||
|
check_new_user, F.callback_data == "check_math_task_true"
|
||||||
|
)
|
||||||
|
@ -9,13 +9,14 @@ class Path:
|
|||||||
modules_standard: str
|
modules_standard: str
|
||||||
modules_custom: str
|
modules_custom: str
|
||||||
|
|
||||||
|
|
||||||
def _get_paths(path_to_json: str):
|
def _get_paths(path_to_json: str):
|
||||||
with open(path_to_json, encoding="utf8") as f:
|
with open(path_to_json, encoding="utf8") as f:
|
||||||
paths = loads(f.read())
|
paths = loads(f.read())
|
||||||
return Path(
|
return Path(
|
||||||
core=paths["core"],
|
core=paths["core"],
|
||||||
modules_standard=paths["modules standard"],
|
modules_standard=paths["modules standard"],
|
||||||
modules_custom=paths["modules custom"]
|
modules_custom=paths["modules custom"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user