Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

node.js - How to integrate Dialogflow to my discord bot along with contexts and followup intents in Node js

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

instead of this

const sessionId = uuid.v4();

Use user wise Session for contexts and follow up intents like...

const sessionId = Userid;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...