81 lines
2.4 KiB
JavaScript
81 lines
2.4 KiB
JavaScript
const models = require("../models");
|
|
const db = models.db;
|
|
const redis = models.redis;
|
|
const {sequelize, Sequelize} = db;
|
|
const Intent = db.intent;
|
|
|
|
exports.getAllIntents = async (req, res) => {
|
|
try {
|
|
const intents = await Intent.findAll();
|
|
|
|
return res.status(200).send(intents);
|
|
} catch (err) {
|
|
console.error("getIntents error:", err);
|
|
|
|
return res.status(500).send({message: err.message || "Internal server error."});
|
|
}
|
|
};
|
|
|
|
exports.getIntentCommand = async (req, res) => {
|
|
try {
|
|
const {text, device_id} = req.body;
|
|
|
|
if (!text || !device_id) {
|
|
return res.status(400).json({
|
|
error: 'Missing required fields: text, device_id',
|
|
});
|
|
}
|
|
|
|
console.log(`\n[FALLBACK API] Processing intent.`);
|
|
|
|
const intents = await Intent.findAll();
|
|
const intent = intents.find(intent => intent.description === text);
|
|
|
|
if (intent) {
|
|
const commandId = await redis.client.xAdd(
|
|
'commands',
|
|
'*',
|
|
{
|
|
device_id: device_id,
|
|
command: intent.command,
|
|
processed_at: new Date().toISOString(),
|
|
source: 'fallback_api',
|
|
}
|
|
);
|
|
|
|
console.log(`Intent matched and published to stream: ${commandId}`);
|
|
|
|
return res.json({
|
|
success: true,
|
|
intent: intent.description,
|
|
command: intent.command
|
|
});
|
|
} else {
|
|
const errorId = await redis.client.xAdd(
|
|
'commands',
|
|
'*',
|
|
{
|
|
device_id: device_id,
|
|
intent: 'error',
|
|
command: 'intent_not_found',
|
|
processed_at: new Date().toISOString(),
|
|
source: 'fallback_api'
|
|
}
|
|
);
|
|
|
|
console.log(`No intent match, error published: ${errorId}`);
|
|
|
|
return res.status(400).json({
|
|
success: false,
|
|
error: 'intent_not_found',
|
|
message: `Could not understand: "${text}"`,
|
|
stream_id: errorId,
|
|
source: 'fallback_api',
|
|
});
|
|
}
|
|
} catch (err) {
|
|
console.error("getIntentCommand error:", err);
|
|
|
|
return res.status(500).send({message: err.message || "Internal server error."});
|
|
}
|
|
}; |