[英]Context not working in web app dialogflow using web speech API
I am using socket.io for bi-directional communication between client-server side , i have also used web speech api for speech synthesis and recognition in my web app ,though it fulfills default responses and it gives output properly , but during contextual conversation my intents doesn't match with context and results in fallback intent.我使用 socket.io 进行客户端-服务器端之间的双向通信,我还在我的网络应用程序中使用了网络语音 api 进行语音合成和识别,虽然它满足默认响应并正确提供输出,但在上下文对话中我意图与上下文不匹配并导致回退意图。
It works fine on dialogflow "try" console but doesn't work on my webapp , please can anyone help..!它在对话流“尝试”控制台上运行良好,但在我的 webapp 上不起作用,请任何人帮忙..!
here's my app.js file :-这是我的 app.js 文件:-
const path = require("path");
const express = require("express");
const colors = require("colors");
const dotenv = require("dotenv");
const socketio = require("socket.io");
const dialogflow = require("@google-cloud/dialogflow");
const uuid = require("uuid");
const app = express();
dotenv.config({ path: "./config/config.env" });
app.use(express.static(path.join(__dirname, "views")));
app.use(express.static(path.join(__dirname, "public")));
const PORT = process.env.PORT || 3000;
const server = app.listen(
PORT,
console.log(
`Server is runnig on ${process.env.NODE_ENV} mode at port ${PORT} for ${process.env.PROJECT_ID}`.yellow
.bold
)
);
const io = socketio(server);
io.on("connection", function (socket) {
console.log("a user connected");
socket.on("chat message", (message) => {
console.log(message);
const callapibot = async (projectId = process.env.PROJECT_ID) => {
try {
const sessionId = uuid.v4();
const sessionClient = new dialogflow.SessionsClient({
keyFilename: "/home/abat/Downloads/kiosk-cwbx-8e7bd8645085.json",
});
const sessionPath = sessionClient.projectAgentSessionPath(
projectId,
sessionId,
);
const request = {
session: sessionPath,
queryInput: {
text: {
text: message,
languageCode: "en-US",
},
},
};
const responses = await sessionClient.detectIntent(request);
console.log("Detected intent");
const result = responses[0].queryResult;
socket.emit("bot reply", result.fulfillmentText);
console.log(result);
if (result.intent) {
console.log(` Intent: ${result.intent.displayName}`);
} else {
console.log(` No intent matched.`)
}
} catch (error) {
console.log(error);
}
};
callapibot();
});
});
here's my script.js file :-这是我的 script.js 文件:-
const btn = document.querySelector("button");
const outputme = document.querySelector(".output-you");
const outputbot = document.querySelector(".output-bot");
const socket = io();
const SpeechRecognition =
window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
recognition.lang = "en-US";
recognition.interimResults = false;
btn.addEventListener("click", () => {
recognition.start();
});
recognition.onresult = function (event) {
const last = event.results.length - 1;
const text = event.results[last][0].transcript;
console.log(text);
outputme.textContent = text;
socket.emit("chat message", text);
};
const botReply = (text) => {
const synth = window.speechSynthesis;
const voices = synth.getVoices();
const utterance = new SpeechSynthesisUtterance();
utterance.voice = voices[4];
utterance.lang = "hi-IN";
utterance.text = text;
utterance.pitch = 1;
utterance.volume = 1;
synth.speak(utterance);
};
socket.on("bot reply", (text) => {
outputbot.textContent = text;
botReply(text);
});
Does my code need modifications for handling contexts for dialogflow ?我的代码是否需要修改来处理 dialogflow 的上下文?
Got the answer , it was because the session id was inside the async function() , i then removed it from there and placed it on the top得到了答案,这是因为会话 ID 在 async function() 内,然后我从那里将其删除并将其放在顶部
const sessionId = uuid.v4();
Don't put this under the async function , that's it.不要把它放在异步函数下,就是这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.