简体   繁体   English

上下文在使用 Web 语音 API 的 Web 应用对话流中不起作用

[英]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.

相关问题 使用 Dialogflow 和 Google Cloud Speech 在 web 应用程序中构建我自己的对话语音 AI API - Building my own conversational voice AI with Dialogflow & Google Cloud Speech API in web apps 上下文在对话流中无法正常工作 - Context is not working properly in dialogflow 如何在 NodeJS 中使用 Web Speech API - How to use the Web Speech API in NodeJS 对话流中的网页抓取 - Web scraping in dialogflow Dialogflow V2 上下文和操作使用 API JSON - Dialogflow V2 Context and Action using API JSON 如何使用Web Speech API使用ExpressJS和Node.js在mongodb中插入其数据 - how to use web speech api to insert its data in mongodb using ExpressJS and nodejs 如何在Node.js的帮助下实现简单的Google Web语音API - How to implement a simple google web speech api with help of node,js 在同一个Azure Web应用程序中部署angular 6应用程序和Web API - Deploy angular 6 app and Web API in same Azure Web app 使用Google Chrome浏览器中的Web Speech API实施来规避标准API的速率限制? - Use Web Speech API implementation in Google Chrome to circumvent rate limit of standard API? Dialogflow 使用外部 API 和 Axios - Dialogflow using external API with Axios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM