Я работаю над проектом в стеке MERN. Мой проект включает в себя чат-бот диалогового потока, который я развернул через веб-приложение React. Я пытаюсь автоматически отправить сообщение в мое веб-приложение, как только некоторые данные отправляются на мой MongoDB. Я пытался сделать это, используя асинхронные функции и события в диалоговом потоке; Однако это решение не отправляет сообщение мгновенно. Занимает несколько минут. Есть ли лучший способ сделать это?
Код на стороне сервера:
router.post('/issueAlert', async (req, res) => {
NewTask.find()
.then(docs => {
if (docs[0] != null) {
name = docs[0].user.first_name;
}
})
const request = {
session: sessionPath,
queryInput: {
event: {
// The query to send to the dialogflow agent
name: req.body.event,
// The language used by the client (en-US)
languageCode: languageCode,
},
},
};
// Send request and log result
const responses = await sessionClient.detectIntent(request);
console.log('Detected New Task Event');
const result = responses[0].queryResult;
if (name == 'Jacob') {
console.log(` Query: ${result.queryText}`);
console.log(` Response: ${result.fulfillmentText}`);
res.send(result)
}
})
Код на стороне клиента:
useEffect(() => {
issueAlert('maintenanceAlert')
}, [])
const issueAlert = async (event) => {
// We need to take care of the message Chatbot sent
const eventQueryVariables = {
event
}
try {
//I will send request to the textQuery ROUTE
const response = await Axios.post('/api/dialogflow/issueAlert', eventQueryVariables)
for (let content of response.data.fulfillmentMessages) {
let conversation = {
who: 'Bot',
content: content
}
dispatch(saveMessage(conversation))
}
} catch (error) {
let conversation = {
who: 'Bot',
content: {
text: {
text: " Error just occured, please check the problem"
}
}
}
dispatch(saveMessage(conversation))
}
}