Add spotify search (only search), default menu
This commit is contained in:
@@ -1,9 +1,20 @@
|
||||
from aiogram import Router
|
||||
from . import initialize
|
||||
from . import (
|
||||
initialize,
|
||||
inline_default,
|
||||
inline_empty,
|
||||
on_chosen,
|
||||
)
|
||||
|
||||
from bot.middlewares import SaveChosenMiddleware
|
||||
|
||||
router = Router()
|
||||
|
||||
router.chosen_inline_result.outer_middleware(SaveChosenMiddleware())
|
||||
|
||||
router.include_routers(
|
||||
initialize.router,
|
||||
inline_default.router,
|
||||
inline_empty.router,
|
||||
on_chosen.router,
|
||||
)
|
||||
|
||||
1
bot/handlers/inline_default/__init__.py
Normal file
1
bot/handlers/inline_default/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .on_inline_default import router
|
||||
38
bot/handlers/inline_default/on_inline_default.py
Normal file
38
bot/handlers/inline_default/on_inline_default.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from aiogram import Router, Bot, F
|
||||
from aiogram.types import (
|
||||
InlineQuery, InlineQueryResultDocument, InlineQueryResultCachedAudio,
|
||||
InlineKeyboardMarkup, InlineKeyboardButton,
|
||||
)
|
||||
|
||||
from bot.modules.spotify import spotify
|
||||
from bot.modules.database import db
|
||||
|
||||
router = Router()
|
||||
|
||||
|
||||
@router.inline_query(F.query != '')
|
||||
async def default_inline_query(inline_query: InlineQuery, bot: Bot):
|
||||
await inline_query.answer(
|
||||
[
|
||||
InlineQueryResultDocument(
|
||||
id='spot::' + audio.id,
|
||||
title=audio.name,
|
||||
description=audio.all_artists,
|
||||
thumb_url=audio.thumbnail,
|
||||
document_url=audio.preview_url,
|
||||
mime_type='application/zip',
|
||||
reply_markup=InlineKeyboardMarkup(
|
||||
inline_keyboard=[
|
||||
[InlineKeyboardButton(text='Downloading...', callback_data='.')]
|
||||
]
|
||||
)
|
||||
) if audio.id not in list(db.spotify.keys()) else
|
||||
InlineQueryResultCachedAudio(
|
||||
id='spotc::' + audio.id,
|
||||
audio_file_id=db.spotify[audio.id],
|
||||
)
|
||||
for audio in spotify.songs.search(inline_query.query)
|
||||
],
|
||||
cache_time=0,
|
||||
is_personal=True
|
||||
)
|
||||
1
bot/handlers/inline_empty/__init__.py
Normal file
1
bot/handlers/inline_empty/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .on_inline_empty import router
|
||||
23
bot/handlers/inline_empty/on_inline_empty.py
Normal file
23
bot/handlers/inline_empty/on_inline_empty.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from aiogram import Router, Bot, F
|
||||
from aiogram.types import (
|
||||
InlineQuery, InputTextMessageContent, InlineQueryResultArticle
|
||||
)
|
||||
from bot.keyboards.inline.full_menu import get_full_menu_kb
|
||||
|
||||
router = Router()
|
||||
|
||||
|
||||
@router.inline_query(F.query == '')
|
||||
async def empty_inline_query(inline_query: InlineQuery, bot: Bot):
|
||||
await inline_query.answer(
|
||||
[
|
||||
InlineQueryResultArticle(
|
||||
id='show_menu',
|
||||
title='⚙️ Open menu',
|
||||
input_message_content=InputTextMessageContent(
|
||||
message_text='⚙️ Menu'
|
||||
),
|
||||
reply_markup=get_full_menu_kb()
|
||||
)
|
||||
], cache_time=0
|
||||
)
|
||||
1
bot/handlers/on_chosen/__init__.py
Normal file
1
bot/handlers/on_chosen/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .spotify import router
|
||||
35
bot/handlers/on_chosen/spotify.py
Normal file
35
bot/handlers/on_chosen/spotify.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from aiogram import Router, Bot, F
|
||||
from aiogram.types import (
|
||||
FSInputFile, URLInputFile, InputMediaAudio,
|
||||
ChosenInlineResult,
|
||||
)
|
||||
|
||||
from bot.modules.spotify import spotify
|
||||
from rich import print
|
||||
from bot.utils.config import config
|
||||
from bot.modules.database import db
|
||||
|
||||
router = Router()
|
||||
|
||||
|
||||
@router.chosen_inline_result(F.result_id.startswith('spot::'))
|
||||
async def on_new_chosen(chosen_result: ChosenInlineResult, bot: Bot):
|
||||
print('TEST: DOWNLOADING NEW')
|
||||
song = spotify.songs.from_id(chosen_result.result_id.removeprefix('spot::'))
|
||||
|
||||
audio = await bot.send_audio(
|
||||
chat_id=config.telegram.files_chat,
|
||||
audio=FSInputFile('tests/test.mp3'),
|
||||
thumbnail=URLInputFile(song.thumbnail),
|
||||
performer=song.all_artists,
|
||||
title=song.name,
|
||||
)
|
||||
|
||||
db.spotify[song.id] = audio.audio.file_id
|
||||
await db.occasionally_write()
|
||||
|
||||
await bot.edit_message_media(
|
||||
inline_message_id=chosen_result.inline_message_id,
|
||||
media=InputMediaAudio(media=audio.audio.file_id),
|
||||
reply_markup=None
|
||||
)
|
||||
Reference in New Issue
Block a user