refactor voice_bot

This commit is contained in:
Andrey
2024-11-14 00:05:37 +03:00
parent 5c3fece394
commit 767acacc18
12 changed files with 341 additions and 329 deletions

View File

@@ -1040,61 +1040,86 @@ class BotDB:
def last_date_audio(self):
"""Получаем дату последнего войса"""
self.logger.info(
f"Запуск функции last_date_audio")
try:
self.connect()
result = self.cursor.execute(
"SELECT `date_added` FROM `audio_message_reference` ORDER BY date_added DESC LIMIT 1")
return result.fetchone()[0]
last_date = result.fetchone()[0]
self.logger.info(f"Последняя дата сообщения {last_date}")
return last_date
except sqlite3.Error as error:
print(error)
self.logger.error(f"Ошибка получения последней даты войса: {error}")
raise
finally:
self.close()
def get_last_user_audio_record(self, user_id):
"""Получает данные о количестве записей пользователя"""
self.logger.info(
f"Запуск функции get_last_user_audio_record. user_id={user_id}")
try:
self.connect()
result = self.cursor.execute("SELECT `file_id` FROM `audio_message_reference` WHERE `author_id` = ?",
r = self.cursor.execute("SELECT `file_id` FROM `audio_message_reference` WHERE `author_id` = ?",
(user_id,))
return bool(len(result.fetchall()))
result = bool(len(r.fetchall()))
self.logger.info(
f"Результат функции get_last_user_audio_record: {result}")
return result
except sqlite3.Error as error:
print(error)
self.logger.error(f"Ошибка получения последней даты войса: {error}")
raise
finally:
self.close()
def get_id_for_audio_record(self, user_id):
"""Получает ID аудио сообщения пользователя"""
self.logger.info(
f"Запуск функции get_id_for_audio_record. user_id={user_id}")
try:
self.connect()
result = self.cursor.execute(
r = self.cursor.execute(
"SELECT `file_id` FROM `audio_message_reference` WHERE `author_id` = ? "
"ORDER BY date_added DESC LIMIT 1",
(user_id,))
return result.fetchone()[0]
result = r.fetchone()[0]
self.logger.info(
f"Результат функции get_id_for_audio_record: {result}")
return result
except sqlite3.Error as error:
print(error)
self.logger.error(f"Ошибка получения последней даты войса: {error}")
raise
finally:
self.close()
def get_path_for_audio_record(self, user_id):
"""Получает данные о названии файла"""
self.logger.info(
f"Запуск функции get_path_for_audio_record. user_id={user_id}")
try:
self.connect()
result = self.cursor.execute(
r = self.cursor.execute(
"SELECT `file_name` "
"FROM `audio_message_reference` "
"WHERE `author_id` = ? "
"ORDER BY date_added "
"DESC LIMIT 1",
(user_id,))
return result.fetchone()[0]
result = r.fetchone()[0]
self.logger.info(
f"Результат функции get_path_for_audio_record: {result}")
return result
except sqlite3.Error as error:
print(error)
self.logger.error(f"Ошибка получения последней даты войса: {error}")
raise
finally:
self.close()
def check_listen_audio(self, user_id):
"""Проверяет прослушано ли аудио пользователем"""
self.logger.info(
f"Запуск функции check_listen_audio. user_id={user_id}")
try:
self.connect()
query_listen_audio = self.cursor.execute(
@@ -1111,14 +1136,19 @@ class BotDB:
new_sign = []
for i in new_sign1:
new_sign.append(i[0])
self.logger.info(
f"Функция check_listen_audio успешно отработала.")
return new_sign
except sqlite3.Error as error:
print(error)
self.logger.error(f"Ошибка получения последней даты войса: {error}")
raise
finally:
self.close()
def mark_listened_audio(self, file_name, user_id):
"""Отмечает аудио прослушанным для конкретного пользователя."""
self.logger.info(
f"Запуск функции mark_listened_audio. file_name={file_name}, user_id={user_id}")
try:
self.connect()
result = self.cursor.execute(
@@ -1126,7 +1156,8 @@ class BotDB:
(file_name, user_id, 1))
return self.conn.commit()
except sqlite3.Error as error:
print(error)
self.logger.error(f"Ошибка получения последней даты войса: {error}")
raise
finally:
self.close()

View File

@@ -1,315 +0,0 @@
import configparser
import os
import sys
from pathlib import Path
from time import sleep
from database.db import BotDB
import telebot
import random
from datetime import datetime
import time
from telebot import types
#Настройки
config_path = os.path.join(sys.path[0], 'settings.ini')
config = configparser.ConfigParser()
config.read(config_path)
#TELEGRAM
BOT_TOKEN = config.get('Telegram', 'listen_bot_token')
GROUP_FOR_POST = config.get('Telegram', 'group_for_posts')
GROUP_FOR_MESSAGE = config.get('Telegram', 'group_for_message')
MAIN_PUBLIC = config.get('Telegram', 'main_public')
GROUP_FOR_LOGS = config.get('Telegram', 'group_for_logs')
IMPORTANT_LOGS = config.get('Telegram', 'important_logs')
PREVIEW_LINK = config.getboolean('Telegram', 'PREVIEW_LINK')
#SETTINGS
LOGS = config.getboolean('Settings', 'logs')
TEST = config.getboolean('Settings', 'test')
#Инициализируем бота и базку
bot = telebot.TeleBot(BOT_TOKEN, parse_mode=None)
BotDB = BotDB('database/tg-bot-database')
def telegram_bot():
@bot.message_handler(commands=['start'])
def send_welcome(message):
try:
name_stick_hello = list(Path('Stick').rglob('Hello_*'))
number_stick_hello = random.randint(1, len(name_stick_hello))
random_stick_hello = open(name_stick_hello[number_stick_hello], 'rb')
#logging
if LOGS:
bot.forward_message(chat_id=GROUP_FOR_LOGS,
from_chat_id=message.chat.id,
message_id=message.message_id)
bot.send_sticker(message.chat.id, random_stick_hello)
sleep(0.3)
except:
if LOGS:
bot.send_message(IMPORTANT_LOGS, BotDB.get_error_message_from_db(7))
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
item1 = types.KeyboardButton("🎤Высказаться")
item2 = types.KeyboardButton("🎧Послушать")
markup.add(item1, item2)
bot.send_message(message.chat.id, "<b>Привет.</b>", parse_mode='html', reply_markup=markup,
disable_web_page_preview=not PREVIEW_LINK)
time.sleep(0.3)
bot.send_message(message.chat.id, "<i>Здесь можно послушать голосовые сообщения от совершенно незнакомых людей из Бийска</i>", parse_mode='html', reply_markup=markup,
disable_web_page_preview=not PREVIEW_LINK)
time.sleep(1)
bot.send_message(message.chat.id, "Это почти как написать письмо, положить его в бутылку и швырнуть в океан. Никогда не узнаешь, послушал его кто-то или нет и ответить тоже не получится..", parse_mode='html', reply_markup=markup,
disable_web_page_preview=not PREVIEW_LINK)
time.sleep(0.8)
bot.send_message(message.chat.id, "Записывать можно всё что угодно — никаких правил нет. Главное — твой голос, <i>хотя бы на 5-10 секунд</i>", parse_mode='html', reply_markup=markup,
disable_web_page_preview=not PREVIEW_LINK)
time.sleep(1.5)
bot.send_message(message.chat.id, "Здесь всё анонимно: тот, кому я отправлю твое сообщение, не узнает ни твое имя, ни твой аккаунт (так что можно не стесняться говорить то, что не стал(а) бы выкладывать в собственные соцсети)", parse_mode='html', reply_markup=markup,
disable_web_page_preview=not PREVIEW_LINK)
time.sleep(1.3)
bot.send_message(message.chat.id, "Если не знаешь, что сказать, можешь просто прочитать любое текстовое сообщение из недавно полученных или отправленных (или спеть, рассказать стихотворенье)", parse_mode='html', reply_markup=markup,
disable_web_page_preview=not PREVIEW_LINK)
time.sleep(0.8)
bot.send_message(message.chat.id,
"Так же можешь ознакомиться с инструкцией к боту по команде /help",
parse_mode='html', reply_markup=markup,
disable_web_page_preview=not PREVIEW_LINK)
time.sleep(0.8)
msg = bot.send_message(message.chat.id, "<b>ну всё, достаточно инструкций. записывайся! Микрофон твой - </b> 🎤", parse_mode='html', reply_markup=markup,
disable_web_page_preview=not PREVIEW_LINK)
bot.register_next_step_handler(msg, standup)
def last_message():
# функция с отображением сообщения "Последнее сообщение было записано"
date_from_db = BotDB.last_date_audio()
parse_date = datetime.strptime(date_from_db, "%Y-%m-%d %H:%M:%S")
last_voice_time_timestamp = time.mktime(parse_date.timetuple())
time_now_timestamp = time.time()
date_difference = time_now_timestamp - last_voice_time_timestamp
# считаем минуты, часы, дни
much_minutes_ago = round(date_difference / 60, 0)
much_hour_ago = round(date_difference / 3600, 0)
much_days_ago = int(round(much_hour_ago / 24, 0))
message_with_date = ''
if much_minutes_ago <= 60:
word_minute = plural_time(1, much_minutes_ago)
message_with_date = f'<b>Последнее сообщение было записано {word_minute} назад</b>'
elif much_minutes_ago > 60 and much_hour_ago <= 24:
word_hour = plural_time(2, much_hour_ago)
message_with_date = f'<b>Последнее сообщение было записано {word_hour} назад</b>'
elif much_hour_ago > 24:
word_day = plural_time(3, much_days_ago)
message_with_date = f'<b>Последнее сообщение было записано {word_day} назад</b>'
return message_with_date
def standup(message):
# Клавиатуру добавляем
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
item1 = types.KeyboardButton("🎤Высказаться")
item2 = types.KeyboardButton("🎧Послушать")
markup.add(item1, item2)
try:
if message.text == '🎤Высказаться':
markup = types.ReplyKeyboardRemove()
if LOGS:
# logging
bot.forward_message(chat_id=GROUP_FOR_LOGS,
from_chat_id=message.chat.id,
message_id=message.message_id)
if TEST:
bot.forward_message(chat_id=GROUP_FOR_LOGS, from_chat_id=message.chat.id, message_id=message.id)
bot.send_message(chat_id=message.chat.id, text='Хорошо, теперь пришли мне свое голосовое сообщение', reply_markup=markup)
message_with_date = last_message()
msg = bot.send_message(chat_id=message.chat.id, text=message_with_date, parse_mode="html")
bot.register_next_step_handler(msg, save_voice_message)
elif message.text == '🎧Послушать':
check_audio = BotDB.check_listen_audio(user_id=message.from_user.id)
list_audio = list(check_audio)
if TEST:
bot.forward_message(chat_id=GROUP_FOR_LOGS, from_chat_id=message.chat.id, message_id=message.id)
if list_audio == []:
bot.send_message(message.chat.id, 'Прости, ты прослушал все аудио😔. Возвращайся позже, возможно наша база пополнится', reply_markup=markup)
message_with_date = last_message()
msg = bot.send_message(chat_id=message.chat.id, text=message_with_date, parse_mode="html")
bot.register_next_step_handler(msg, standup)
else:
number_element = random.randint(0, len(list_audio) - 1)
audio_for_user = check_audio[number_element]
path = Path(f'voice_users/{audio_for_user}.ogg')
voice = open(path, 'rb')
#Маркируем сообщение как прослушанное
BotDB.mark_listened_audio(audio_for_user, user_id=message.from_user.id)
msg = bot.send_voice(message.chat.id, voice=voice, reply_markup=markup)
bot.register_next_step_handler(msg, standup)
if LOGS:
# logging
bot.forward_message(chat_id=GROUP_FOR_LOGS,
from_chat_id=message.chat.id,
message_id=message.message_id)
if TEST:
bot.forward_message(chat_id=GROUP_FOR_LOGS, from_chat_id=message.chat.id, message_id=message.id)
elif message.text == '/restart':
msg = bot.send_message(message.chat.id, 'Я перезапущен, и готов к работе🥳', reply_markup=markup)
bot.forward_message(chat_id=GROUP_FOR_LOGS,
from_chat_id=message.chat.id,
message_id=message.message_id)
bot.register_next_step_handler(msg, standup)
elif message.text == '/help':
help_function(message)
else:
msg = bot.send_message(chat_id=message.chat.id, text='Я тебя не понял, воспользуйся меню', reply_markup=markup)
bot.register_next_step_handler(msg, standup)
except Exception as e:
msg = bot.send_message(chat_id=message.chat.id, text='Я не могу прислать тебе голосовое, потому что они скрыты у тебя в настройках. Открой для меня голосовые и возвращайся❤️', reply_markup=markup)
bot.register_next_step_handler(msg, standup)
def plural_time(type, n):
word = []
if type == 1:
word = ['минуту', 'минуты', 'минут']
elif type == 2:
word = ['час', 'часа', 'часов']
elif type == 3:
word = ['день', 'дня', 'дней']
else:
pass
if n % 10 == 1 and n % 100 != 11:
p = 0
elif 2 <= n % 10 <= 4 and (n % 100 < 10 or n % 100 >= 20):
p = 1
else:
p = 2
new_number = int(n)
return str(new_number) + ' ' + word[p]
def save_voice_message(message):
if message.content_type == 'voice':
if TEST:
bot.forward_message(chat_id=GROUP_FOR_LOGS, from_chat_id=message.chat.id, message_id=message.id)
file_name = ''
file_id = 1
#Проверяем что запись о файле есть в базе данных
is_having_audio_from_user = BotDB.get_last_user_audio_record(user_id=message.from_user.id)
if is_having_audio_from_user is False:
#Если нет, то генерируем имя файла
file_name = f'message_from_{message.from_user.id}_number_{file_id}'
else:
#Иначе берем последнюю запись из БД, добавляем к ней 1, и создаем новую запись
file_name = BotDB.get_path_for_audio_record(user_id=message.from_user.id)
file_id = BotDB.get_id_for_audio_record(message.from_user.id) + 1
path = Path(f'voice_users/{file_name}.ogg')
if path.exists():
file_name = f'message_from_{message.from_user.id}_number_{file_id}'
else:
pass
#Собираем инфо о сообщении
author_id = message.from_user.id
time_UTC = int(time.time())
date_added = datetime.fromtimestamp(time_UTC)
#Сохраняем в базку
BotDB.add_audio_record(file_name, author_id, date_added, 0, file_id)
#Сохраняем файл на сервер
file_info = bot.get_file(message.voice.file_id)
downloaded_file = bot.download_file(file_info.file_path)
with open(f'voice_users/{file_name}.ogg', 'wb') as new_file:
new_file.write(downloaded_file)
#инициализируем кнопки
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
item1 = types.KeyboardButton("🎤Высказаться")
item2 = types.KeyboardButton("🎧Послушать")
markup.add(item1, item2)
bot.send_message(chat_id=message.chat.id, text='Окей, сохранил!👌', reply_markup=markup)
#menu_standup(message=message)
bot.register_next_step_handler(message, standup)
else:
#TODO: Если пришлют фото, он не работает
if TEST:
bot.forward_message(chat_id=GROUP_FOR_LOGS, from_chat_id=message.chat.id, message_id=message.id)
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
item1 = types.KeyboardButton("🎤Высказаться")
item2 = types.KeyboardButton("🎧Послушать")
markup.add(item1, item2)
msg = bot.send_message(chat_id=message.chat.id, text='Я тебя не понимаю🤷‍♀️ запиши голосовое', reply_markup=markup)
bot.register_next_step_handler(msg, standup)
@bot.message_handler(commands=['restart'])
def restart_function(message):
return standup(message)
@bot.message_handler(commands=['help'])
def help_function(message):
msg = bot.send_message(chat_id=message.chat.id, text='Скорее всего ответы на твои вопросы есть здесь, ознакомься: https://telegra.ph/Instrukciya-k-botu-Golosa-Bijsk-10-11-2'
'\nЕсли это не поможет, пиши в тг: @Kerrad1',disable_web_page_preview=not PREVIEW_LINK)
bot.register_next_step_handler(msg, standup)
@bot.message_handler(commands=['send'])
def notify(message):
"""Функция для рассылки сообщений пользователям бота"""
admins = [842766148]
command_sender = message.from_user.id
if command_sender in admins:
markup = types.InlineKeyboardMarkup(row_width=1)
button = types.InlineKeyboardButton(text='Я прочитал ', callback_data='from_user')
success_send_message = 0
error_send_message = 0
markup.add(button)
user_ids = BotDB.get_all_user_id()
# Парсим список id юзеров для рассылки и рассылаем сообщения
for i in user_ids:
username = BotDB.get_username(i)
try:
bot.send_message(chat_id=i,
text='Привет, на связи админ. Первая тестовая рассылка через этого бота, не суди строго, обещаю не злоупотреблять этим))\n'
'Собственно хотел сказать следующее, сегодня ночью обновил бота, добавил ему две функции /restart и /help. '
'Рекомендую ознакомиться со статьей в /help, там я подробно описал как сейчас все работает. '
'Если у тебя будут предложения по доработке бота или новому функционал, пиши не стесняйся @Kerrad1. '
'Так же прошу нажать на кнопку под сообщением, чтобы я понял что ты прочитал сообщение❤️\n\nНа этом у меня все, пока, до следующего релиза', reply_markup=markup)
bot.send_message(chat_id=message.chat.id,
text=f'Успешно отправлено - @{username}')
success_send_message += 1
except Exception as e:
bot.send_message(chat_id=message.chat.id,
text=f'Этому юзеру не отправилось - @{username}')
error_send_message += 1
bot.send_message(chat_id=message.chat.id,
text=f'<b>Количество успешных отправок</b> - {success_send_message}', parse_mode='html')
bot.send_message(chat_id=message.chat.id,
text=f'<b>Количество неуспешных отправок</b> - {error_send_message}', parse_mode='html')
standup(message)
else:
bot.send_message(command_sender, f'у вас нет прав для запуска команды')
bot.forward_message(chat_id=GROUP_FOR_LOGS, from_chat_id=message.chat.id, message_id=message.id)
standup(message)
@bot.callback_query_handler(func=lambda call: True)
def post_for_group(call):
if call.data == 'from_user' and call.message.content_type == 'text':
try:
command_sender = call.message.from_user.id
bot.answer_callback_query(call.id, text="Мне приятно, спасибо ❤")
#Попробовать варианты если не сработает call.from_user.username
bot.send_message(chat_id=842766148, text=f'Юзер с ником @{call.message.chat.username} по имени {call.message.chat.first_name} прочитал сообщение')
except:
print('что-то не так')
if __name__ == '__main__':
telegram_bot()
while True:
try:
bot.polling(none_stop=True)
bot.enable_save_next_step_handlers(delay=2)
bot.load_next_step_handlers()
except ConnectionError as e:
if LOGS:
bot.send_message(IMPORTANT_LOGS, "Ошибка соединения, потерял войс бот связь")
except Exception as r:
if LOGS:
bot.send_message(IMPORTANT_LOGS, "Произошло что-то непредвиденное, хелп. Войс бот болеет")

0
voice_bot/__init__.py Normal file
View File

View File

View File

@@ -0,0 +1,10 @@
from aiogram import types
from aiogram.utils.keyboard import ReplyKeyboardBuilder
def get_main_keyboard():
builder = ReplyKeyboardBuilder()
builder.add(types.KeyboardButton(text="🎤Высказаться"))
builder.add(types.KeyboardButton(text="🎧Послушать"))
markup = builder.as_markup(resize_keyboard=True)
return markup

18
voice_bot/main.py Normal file
View File

@@ -0,0 +1,18 @@
from aiogram import Bot, Dispatcher
from aiogram.client.default import DefaultBotProperties
from aiogram.fsm.storage.memory import MemoryStorage
from aiogram.fsm.strategy import FSMStrategy
from voice_bot.voice_handler.voice_handler import voice_router
async def start_bot(bdf):
token = bdf.settings['Telegram']['listen_bot_token']
bot = Bot(token=token, default=DefaultBotProperties(
parse_mode='HTML',
link_preview_is_disabled=bdf.settings['Telegram']['preview_link']
))
dp = Dispatcher(storage=MemoryStorage(), fsm_strategy=FSMStrategy.GLOBAL_USER)
dp.include_routers(voice_router)
await bot.delete_webhook(drop_pending_updates=True)
await dp.start_polling(bot, skip_updates=True)

View File

View File

@@ -0,0 +1,53 @@
import time
from datetime import datetime
from helper_bot.utils.base_dependency_factory import BaseDependencyFactory
bdf = BaseDependencyFactory()
BotDB = bdf.get_db()
def last_message():
# функция с отображением сообщения "Последнее сообщение было записано"
date_from_db = BotDB.last_date_audio()
parse_date = datetime.strptime(date_from_db, "%Y-%m-%d %H:%M:%S")
last_voice_time_timestamp = time.mktime(parse_date.timetuple())
time_now_timestamp = time.time()
date_difference = time_now_timestamp - last_voice_time_timestamp
# считаем минуты, часы, дни
much_minutes_ago = round(date_difference / 60, 0)
much_hour_ago = round(date_difference / 3600, 0)
much_days_ago = int(round(much_hour_ago / 24, 0))
message_with_date = ''
if much_minutes_ago <= 60:
word_minute = plural_time(1, much_minutes_ago)
message_with_date = f'<b>Последнее сообщение было записано {word_minute} назад</b>'
elif much_minutes_ago > 60 and much_hour_ago <= 24:
word_hour = plural_time(2, much_hour_ago)
message_with_date = f'<b>Последнее сообщение было записано {word_hour} назад</b>'
elif much_hour_ago > 24:
word_day = plural_time(3, much_days_ago)
message_with_date = f'<b>Последнее сообщение было записано {word_day} назад</b>'
return message_with_date
def plural_time(type, n):
word = []
if type == 1:
word = ['минуту', 'минуты', 'минут']
elif type == 2:
word = ['час', 'часа', 'часов']
elif type == 3:
word = ['день', 'дня', 'дней']
else:
pass
if n % 10 == 1 and n % 100 != 11:
p = 0
elif 2 <= n % 10 <= 4 and (n % 100 < 10 or n % 100 >= 20):
p = 1
else:
p = 2
new_number = int(n)
return str(new_number) + ' ' + word[p]

View File

@@ -0,0 +1,6 @@
def get_message(username: str, type_message: str):
constants = {
}
message = constants[type_message]
return message.replace('username', username).replace('&', '\n')

View File

View File

@@ -0,0 +1,200 @@
import random
import time
from datetime import datetime
from pathlib import Path
from aiogram import Router, types, F
from aiogram.filters import Command, StateFilter
from aiogram.fsm.context import FSMContext
from aiogram.types import FSInputFile
from helper_bot.filters.main import ChatTypeFilter
from helper_bot.utils.base_dependency_factory import BaseDependencyFactory
from logs.custom_logger import logger
from voice_bot.keyboards.keyboards import get_main_keyboard
from voice_bot.utils.helper_func import last_message
voice_router = Router()
bdf = BaseDependencyFactory()
GROUP_FOR_LOGS = bdf.settings['Telegram']['group_for_logs']
IMPORTANT_LOGS = bdf.settings['Telegram']['important_logs']
PREVIEW_LINK = bdf.settings['Telegram']['preview_link']
LOGS = bdf.settings['Settings']['logs']
TEST = bdf.settings['Settings']['test']
BotDB = bdf.get_db()
@voice_router.message(
ChatTypeFilter(chat_type=["private"]),
Command("start")
)
async def start(message: types.Message, state: FSMContext):
await state.set_state("START")
await message.forward(chat_id=GROUP_FOR_LOGS)
try:
name_stick_hello = list(Path('Stick').rglob('Hello_*'))
random_stick_hello = random.choice(name_stick_hello)
random_stick_hello = FSInputFile(path=random_stick_hello)
logger.info(f"Стикер успешно получен из БД. Наименование стикера: {name_stick_hello}")
await message.answer_sticker(random_stick_hello)
time.sleep(0.3)
except Exception as e:
if LOGS:
await message.bot.send_message(IMPORTANT_LOGS, f'Отправка приветственных стикеров лажает. Ошибка: {e}')
markup = get_main_keyboard()
await message.answer(text="<b>Привет.</b>", parse_mode='html', reply_markup=markup,
disable_web_page_preview=not PREVIEW_LINK)
time.sleep(0.3)
await message.answer(text="<i>Здесь можно послушать голосовые сообщения от совершенно незнакомых людей из "
"Бийска</i>",
parse_mode='html', reply_markup=markup,
disable_web_page_preview=not PREVIEW_LINK)
time.sleep(1)
await message.answer(text="Это почти как написать письмо, положить его в бутылку и швырнуть в океан. Никогда не "
"узнаешь, послушал его кто-то или нет и ответить тоже не получится..",
parse_mode='html', reply_markup=markup,
disable_web_page_preview=not PREVIEW_LINK)
time.sleep(0.8)
await message.answer(text="Записывать можно всё что угодно — никаких правил нет. Главное — твой голос, <i>хотя "
"бы на 5-10 секунд</i>",
parse_mode='html', reply_markup=markup,
disable_web_page_preview=not PREVIEW_LINK)
time.sleep(1.5)
await message.answer(text="Здесь всё анонимно: тот, кому я отправлю твое сообщение, не узнает ни твое имя, "
"ни твой аккаунт (так что можно не стесняться говорить то, что не стал(а) бы "
"выкладывать в собственные соцсети)",
parse_mode='html', reply_markup=markup,
disable_web_page_preview=not PREVIEW_LINK)
time.sleep(1.3)
await message.answer(text="Если не знаешь, что сказать, можешь просто прочитать любое текстовое сообщение из "
"недавно полученных или отправленных (или спеть, рассказать стихотворенье)",
parse_mode='html', reply_markup=markup,
disable_web_page_preview=not PREVIEW_LINK)
time.sleep(0.8)
await message.answer(text="Так же можешь ознакомиться с инструкцией к боту по команде /help",
parse_mode='html', reply_markup=markup,
disable_web_page_preview=not PREVIEW_LINK)
time.sleep(0.8)
await message.answer(text="<b>ну всё, достаточно инструкций. записывайся! Микрофон твой - </b> 🎤",
parse_mode='html', reply_markup=markup,
disable_web_page_preview=not PREVIEW_LINK)
@voice_router.message(
StateFilter("START"),
ChatTypeFilter(chat_type=["private"]),
F.text == '🎤Высказаться'
)
async def standup_write(message: types.Message, state: FSMContext):
await message.forward(chat_id=GROUP_FOR_LOGS)
markup = types.ReplyKeyboardRemove()
await message.answer(text='Хорошо, теперь пришли мне свое голосовое сообщение', reply_markup=markup)
message_with_date = last_message()
await message.answer(text=message_with_date, parse_mode="html")
await state.set_state('STANDUP_WRITE')
@voice_router.message(
StateFilter("STANDUP_WRITE"),
ChatTypeFilter(chat_type=["private"])
)
async def save_voice_message(message: types.Message, state: FSMContext):
markup = get_main_keyboard()
if message.content_type == 'voice':
await message.forward(chat_id=GROUP_FOR_LOGS)
file_name = ''
file_id = 1
# Проверяем что запись о файле есть в базе данных
is_having_audio_from_user = BotDB.get_last_user_audio_record(user_id=message.from_user.id)
if is_having_audio_from_user is False:
# Если нет, то генерируем имя файла
file_name = f'message_from_{message.from_user.id}_number_{file_id}'
else:
# Иначе берем последнюю запись из БД, добавляем к ней 1, и создаем новую запись
file_name = BotDB.get_path_for_audio_record(user_id=message.from_user.id)
file_id = BotDB.get_id_for_audio_record(message.from_user.id) + 1
path = Path(f'voice_users/{file_name}.ogg')
if path.exists():
file_name = f'message_from_{message.from_user.id}_number_{file_id}'
else:
pass
# Собираем инфо о сообщении
author_id = message.from_user.id
time_UTC = int(time.time())
date_added = datetime.fromtimestamp(time_UTC)
# Сохраняем в базку
BotDB.add_audio_record(file_name, author_id, date_added, 0, file_id)
# Сохраняем файл на сервер
# file_info = message.bot.get_file(file_id=message.voice.file_id)
# downloaded_file = message.bot.download_file(file_path=file_info.file_path)
# with open(f'voice_users/{file_name}.ogg', 'wb') as new_file:
# new_file.write(downloaded_file)
file_info = await message.bot.get_file(file_id=message.voice.file_id)
downloaded_file = await message.bot.download_file(file_path=file_info.file_path)
with open(f'voice_users/{file_name}.ogg', 'wb') as new_file:
new_file.write(downloaded_file.read())
await message.answer(text='Окей, сохранил!👌', reply_markup=markup)
await state.set_state('START')
else:
# TODO: Если пришлют фото, он не работает
await message.forward(chat_id=GROUP_FOR_LOGS)
await message.answer(text='Я тебя не понимаю🤷‍♀️ запиши голосовое', reply_markup=markup)
await state.set_state('STANDUP_WRITE')
@voice_router.message(
StateFilter("START"),
ChatTypeFilter(chat_type=["private"]),
F.text == '🎧Послушать'
)
async def standup_listen_audio(message: types.Message, state: FSMContext):
check_audio = BotDB.check_listen_audio(user_id=message.from_user.id)
list_audio = list(check_audio)
markup = get_main_keyboard()
await message.forward(chat_id=GROUP_FOR_LOGS)
if not list_audio:
await message.answer(text='Прости, ты прослушал все аудио😔. Возвращайся позже, возможно наша база пополнится',
reply_markup=markup)
message_with_date = last_message()
message.send_message(chat_id=message.chat.id, text=message_with_date, parse_mode="html")
else:
number_element = random.randint(0, len(list_audio) - 1)
audio_for_user = check_audio[number_element]
path = Path(f'voice_users/{audio_for_user}.ogg')
# voice = open(path, 'rb')
voice = FSInputFile(path)
# Маркируем сообщение как прослушанное
BotDB.mark_listened_audio(audio_for_user, user_id=message.from_user.id)
await message.bot.send_voice(message.chat.id, voice=voice, reply_markup=markup)
await message.forward(chat_id=GROUP_FOR_LOGS)
await state.set_state('START')
@voice_router.message(
ChatTypeFilter(chat_type=["private"]),
Command("restart")
)
async def restart_function(message: types.Message, state: FSMContext):
await message.forward(chat_id=GROUP_FOR_LOGS)
markup = get_main_keyboard()
await message.answer(text='Я перезапущен!',
reply_markup=markup)
await state.set_state('START')
@voice_router.message(
ChatTypeFilter(chat_type=["private"]),
Command("help")
)
async def help_function(message: types.Message, state: FSMContext):
await message.forward(chat_id=GROUP_FOR_LOGS)
await message.answer(
text='Скорее всего ответы на твои вопросы есть здесь, ознакомься: https://telegra.ph/Instrukciya-k-botu-Golosa-Bijsk-10-11-2'
'\nЕсли это не поможет, пиши в тг: @Kerrad1', disable_web_page_preview=not PREVIEW_LINK)
await state.set_state('START')

9
voice_bot_v2.py Normal file
View File

@@ -0,0 +1,9 @@
import asyncio
from helper_bot.utils.base_dependency_factory import BaseDependencyFactory
from voice_bot.main import start_bot
bdf = BaseDependencyFactory()
if __name__ == '__main__':
asyncio.run(start_bot(BaseDependencyFactory()))