feat: Spotify integration (OAuth + voice tools)
Some checks failed
Deploy / deploy (push) Failing after 1m40s
Some checks failed
Deploy / deploy (push) Failing after 1m40s
This commit is contained in:
34
lib/spotify-client.ts
Normal file
34
lib/spotify-client.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
let cachedToken: { token: string; expiresAt: number } | null = null
|
||||
|
||||
export async function getAccessToken(): Promise<string> {
|
||||
if (cachedToken && Date.now() < cachedToken.expiresAt - 30_000) {
|
||||
return cachedToken.token
|
||||
}
|
||||
|
||||
const refreshToken = process.env.SPOTIFY_REFRESH_TOKEN
|
||||
if (!refreshToken) throw new Error('SPOTIFY_REFRESH_TOKEN not set')
|
||||
|
||||
const clientId = process.env.SPOTIFY_CLIENT_ID!
|
||||
const clientSecret = process.env.SPOTIFY_CLIENT_SECRET!
|
||||
|
||||
const res = await fetch('https://accounts.spotify.com/api/token', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`,
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
grant_type: 'refresh_token',
|
||||
refresh_token: refreshToken,
|
||||
}),
|
||||
})
|
||||
|
||||
const data = await res.json()
|
||||
if (!data.access_token) throw new Error(`Spotify token refresh failed: ${JSON.stringify(data)}`)
|
||||
|
||||
cachedToken = {
|
||||
token: data.access_token,
|
||||
expiresAt: Date.now() + (data.expires_in || 3600) * 1000,
|
||||
}
|
||||
return cachedToken.token
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import { tools as calendarTools } from './calendar'
|
||||
import { tools as timerTools } from './timers'
|
||||
import { tool as notes } from './notes'
|
||||
import { tools as smartHomeTools } from './smart-home'
|
||||
import { tools as spotifyTools } from './spotify'
|
||||
|
||||
const ALL_TOOLS: VoiceTool[] = [
|
||||
weather,
|
||||
@@ -21,6 +22,7 @@ const ALL_TOOLS: VoiceTool[] = [
|
||||
...timerTools,
|
||||
notes,
|
||||
...smartHomeTools,
|
||||
...spotifyTools,
|
||||
]
|
||||
|
||||
export const TOOL_SCHEMAS = ALL_TOOLS.map((t) => t.schema)
|
||||
|
||||
59
lib/tools/spotify.ts
Normal file
59
lib/tools/spotify.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import type { VoiceTool } from './_types'
|
||||
import { tabletGet, tabletJson } from './_http'
|
||||
|
||||
const getNowPlaying: VoiceTool = {
|
||||
schema: {
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'get_now_playing',
|
||||
description:
|
||||
'Узнать что сейчас играет в Spotify: название трека, исполнитель, статус воспроизведения. ' +
|
||||
'Используй для вопросов «что сейчас играет», «какая музыка», «кто поёт».',
|
||||
parameters: { type: 'object', properties: {} },
|
||||
},
|
||||
},
|
||||
async execute() {
|
||||
return tabletGet('/api/voice/tools/spotify')
|
||||
},
|
||||
}
|
||||
|
||||
const controlSpotify: VoiceTool = {
|
||||
schema: {
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'control_spotify',
|
||||
description:
|
||||
'Управление Spotify: включить музыку (можно с поиском), поставить на паузу, следующий/предыдущий трек, громкость. ' +
|
||||
'Используй для команд «включи музыку», «поставь паузу», «следующий трек», «громкость 50%», ' +
|
||||
'«включи The Weeknd», «поставь джаз».',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
action: {
|
||||
type: 'string',
|
||||
enum: ['play', 'pause', 'next', 'previous', 'volume'],
|
||||
description: 'Действие: play, pause, next, previous, volume',
|
||||
},
|
||||
query: {
|
||||
type: 'string',
|
||||
description: 'Поисковый запрос для action=play: исполнитель, трек или жанр',
|
||||
},
|
||||
volume: {
|
||||
type: 'number',
|
||||
description: 'Громкость 0-100 для action=volume',
|
||||
},
|
||||
},
|
||||
required: ['action'],
|
||||
},
|
||||
},
|
||||
},
|
||||
async execute(args) {
|
||||
return tabletJson('POST', '/api/voice/tools/spotify', {
|
||||
action: args.action,
|
||||
query: args.query,
|
||||
volume: args.volume,
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
export const tools: VoiceTool[] = [getNowPlaying, controlSpotify]
|
||||
Reference in New Issue
Block a user