I made a fully well functioning chatbot using dialogflow now I wanted to integrate it to my discord bot. I followed a youtube video which integrated only the intents but I couldn't figure out how to do with contexts and follow up intents.
const Discord = require('discord.js');
const client = new Discord.Client();
require('dotenv').config();
const token = process.env.DISCORD_TOKEN;
process.env.GOOGLE_APPLICATION_CREDENTIALS = `${process.env.PWD}/${process.env.REPL_SLUG}/config.json`
const dialogflow = require('@google-cloud/dialogflow');
const uuid = require('uuid');
client.once('ready', () => console.log('ready!'));
async function replyMsg(textMsg) {
projectId = process.env.PROJECT_ID;
// A unique identifier for the given session
const sessionId = uuid.v4();
// Create a new session
const sessionClient = new dialogflow.SessionsClient();
const sessionPath = await sessionClient.projectAgentSessionPath(
projectId,
sessionId
);
// The text query request.
const request = {
session: sessionPath,
queryInput: {
text: {
// The query to send to the dialogflow agent
text: textMsg,
// The language used by the client (en-US)
languageCode: 'en-US',
},
},
};
// Send request and log result
const responses = await sessionClient.detectIntent(request);
console.log("Detected intent");
const result = responses[0].queryResult;
console.log(`Query: ${result.queryText}`);
console.log(` Response: ${result.fulfillmentText}`);
if (result.intent) {
console.log(` Intent: ${result.intent.displayName}`);
} else {
console.log(` No intent matched.`);
}
return await result.fulfillmentText;
}
client.on('message', (message) => {
console.log(message.author.bot);
if (!message.author.bot) {
if (message.mentions.has('784734506812833792'))
replyMsg(message.content).then((res) => {
console.log(res);
message.channel.send("<@" + message.author.id + ">" + ' ' + res);
});
}
});
client.login(token);
question from:
https://stackoverflow.com/questions/65868075/how-to-integrate-dialogflow-to-my-discord-bot-along-with-contexts-and-followup-i 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…