new function with audio message. Read audio from db

This commit is contained in:
KerradKerridi
2022-08-28 23:37:27 +03:00
parent 6da8d8211c
commit b162d5c50b
2 changed files with 38 additions and 30 deletions

55
main.py
View File

@@ -1,5 +1,4 @@
import configparser
import itertools
import os
import sys
from pathlib import Path
@@ -82,12 +81,13 @@ def telegram_bot():
@bot.message_handler(commands=['end_command'])
def after_post(message):
markup = types.ReplyKeyboardMarkup(row_width=2)
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
item1 = types.KeyboardButton("Предложить свой пост")
item2 = types.KeyboardButton("Связаться с админами")
item3 = types.KeyboardButton("Удалить пост")
item4 = types.KeyboardButton("Сказать пока!")
markup.add(item1, item2, item3, item4)
item4 = types.KeyboardButton("Войти в режим стендапа")
item5 = types.KeyboardButton("Сказать пока!")
markup.add(item1, item2, item3, item4, item5)
bot.send_message(message.chat.id,
"Выбери нужную кнопку внизу экрана".format(
message.from_user, bot.get_me()),
@@ -128,10 +128,11 @@ def telegram_bot():
bot.register_next_step_handler(msg, resend_message_in_group_for_message)
elif message.text == "Войти в режим стендапа":
markup = types.ReplyKeyboardMarkup(row_width=1)
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
item1 = types.KeyboardButton("Высказаться")
item2 = types.KeyboardButton("Послушать")
markup.add(item1, item2)
item3 = types.KeyboardButton("Вернуться в меню")
markup.add(item1, item2, item3)
first_name = message.from_user.first_name
message_for_standup = BotDB.get_message_from_db('message_for_standup', first_name)
msg = bot.send_message(message.chat.id, message_for_standup, parse_mode='html', reply_markup=markup, disable_web_page_preview=not PREVIEW_LINK)
@@ -193,7 +194,7 @@ def telegram_bot():
bot.forward_message(chat_id=GROUP_FOR_LOGS,
from_chat_id=message.chat.id,
message_id=message.message_id)
menu_standup(message=message)
#menu_standup(message=message)
def standup(message):
try:
@@ -202,29 +203,25 @@ def telegram_bot():
msg = bot.send_message(chat_id=message.chat.id, text='Пришли мне свое голосовое сообщение', reply_markup=markup)
bot.register_next_step_handler(msg, save_voice_message)
elif message.text == 'Послушать':
#msg = bot.send_voice(chat_id=message.chat.id, open('voice_users/new_file.ogg', 'rb'))
msg = bot.send_message(chat_id=message.chat.id, text='Погоди, так еще не умею')
bot.register_next_step_handler(msg, menu_standup)
file = BotDB.get_random_audio(user_id=message.from_user.id)[0]
path = Path(f'voice_users/{file}.ogg')
voice = open(path, 'rb')
#Клавиатуру добавляем
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
item1 = types.KeyboardButton("Высказаться")
item2 = types.KeyboardButton("Послушать")
item3 = types.KeyboardButton("Вернуться в меню")
markup.add(item1, item2, item3)
msg = bot.send_voice(message.chat.id, voice=voice, reply_markup=markup)
bot.register_next_step_handler(msg, standup)
elif message.text == 'Вернуться в меню':
after_post(message=message)
except:
if LOGS:
bot.send_message(chat_id=IMPORTANT_LOGS, text=BotDB.get_error_message_from_db(4))
def menu_standup(message):
markup = types.ReplyKeyboardMarkup(row_width=2)
item1 = types.KeyboardButton("Высказаться еще")
item2 = types.KeyboardButton("Послушать")
item3 = types.KeyboardButton("Вернуться в меню")
markup.add(item1, item2, item3)
msg = bot.send_message(chat_id=message.chat.id,
text='Держи клавиатуру и выбери нужную кнопку внизу экрана!',
parse_mode='html', reply_markup=markup, disable_web_page_preview=not PREVIEW_LINK)
bot.register_next_step_handler(msg, standup)
def save_voice_message(message):
if message.content_type == 'voice':
#ret_msg = bot.send_voice(message.chat.id, open('voice_users/new_file.ogg', 'rb')) message_id, author_id, added_date
file_name = ''
file_id = 1
#Проверяем что запись о файле есть в базе данных
@@ -252,10 +249,18 @@ def telegram_bot():
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)
bot.send_message(chat_id=message.chat.id, text='Окей, сохранил!')
menu_standup(message=message)
#инициализируем кнопки
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
item1 = types.KeyboardButton("Вернуться в меню")
markup.add(item1)
bot.send_message(chat_id=message.chat.id, text='Окей, сохранил!', reply_markup=markup)
#menu_standup(message=message)
bot.register_next_step_handler(message, standup)
else:
msg = bot.send_message(chat_id=message.chat.id, text='Я тебя не понимаю, запиши голосовое')
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
item1 = types.KeyboardButton("Вернуться в меню")
markup.add(item1)
msg = bot.send_message(chat_id=message.chat.id, text='Я тебя не понимаю, запиши голосовое', reply_markup=markup)
bot.register_next_step_handler(msg, save_voice_message)
def resend_message_in_group_for_post(message):