Files
alice-backend/server/quick-actions.mjs
T

40 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const units = 'ноль один два три четыре пять шесть семь восемь девять десять одиннадцать двенадцать тринадцать четырнадцать пятнадцать шестнадцать семнадцать восемнадцать девятнадцать'.split(' ');
const tens = 'двадцать тридцать сорок пятьдесят шестьдесят семьдесят восемьдесят девяносто'.split(' ');
function volumeLevel(text) {
if (/^\d{1,3}$/.test(text)) return Number(text);
if (text === 'сто') return 100;
const words = text.split(' ');
if (words.length === 1 && units.includes(words[0])) return units.indexOf(words[0]);
const decade = tens.indexOf(words[0]);
if (decade === -1) return -1;
if (words.length === 1) return (decade + 2) * 10;
const unit = units.indexOf(words[1]);
return words.length === 2 && unit >= 1 && unit <= 9 ? (decade + 2) * 10 + unit : -1;
}
export function quickAction(transcript) {
const text = transcript.toLowerCase().replace(/[.,!?;:…\s]+/gu, ' ').trim()
.replace(/^алиса\s+/, '').replace(/^пожалуйста\s+|\s+пожалуйста$/g, '');
if (/^(?:пауза|на паузу|(?:поставь|поставить)(?: музыку| воспроизведение)? на паузу|(?:приостанови|выключи|останови) (?:музыку|воспроизведение))$/.test(text)) {
return {
name: 'spotify_control',
args: { action: 'pause' },
done: 'Музыка на паузе.',
failure: 'Не удалось поставить музыку на паузу.',
success: result => result?.accepted === true && ['paused', 'stopped'].includes(result.status),
};
}
const match = text.match(/^(?:(?:поставь|установи|сделай) )?громкость(?: на| до)? (.+?)(?: ?%| процент(?:а|ов)?)?$/);
if (!match) return null;
const level = volumeLevel(match[1]);
if (level < 0 || level > 100) return null;
return {
name: 'station_volume',
args: { level },
done: `Громкость ${level}%.`,
failure: 'Не удалось изменить громкость колонки.',
success: result => result?.ok === true && result.level === level,
};
}