Visual Studio 如何使用Google Bard创建聊天机器人?

k4ymrczo  于 12个月前  发布在  Go
关注(0)|答案(1)|浏览(106)

我想用Google Bard做一个聊天机器人。我尝试使用Chatgpt,但我遇到了一些错误并放弃了。
现在我只需要基本代码只是我发了一条信息,机器人就回应了。
现在,因为有接近0教程,我用谷歌吟游诗人本身,所以我很清楚有些事情是错误的。我只是不知道是什么
下面是我当前的代码:

const {Dialogflow} = require('@google-cloud/dialogflow');

const dialogflow = new Dialogflow({
  projectId: 'my project id',
  credentials: {
    key: 'my api key',
  },
});

// Create a variable to store the conversation state.
const { Client, Collection, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent ] });

client.on('ready', () => {
console.log('Bot is ready!')
})
const ignore = '!' && '?'
const botchanelid = '1155186010485960909'

client.on('messageCreate', async (msg) => {
if (msg.content.startsWith(ignore) || msg.channelId  != botchanelid || msg.author.bot) return;
dialogflow.query(`${msg}`, conversationState, (err, response) => {
  if (err) {
    console.log(err);
    return;
  }

  // Get the response from Dialogflow.
  const responseText = response.result.text;

  // Update the conversation state.
  conversationState = response.result.conversationState;

  // Display the response to the user.
msg.reply(responseText)
})
})

现在我的错误是TypeError: Dialogflow is not a constructor。我想要一个答案,告诉我我做错了什么,并完全调整它,所以我明白。
我的版本是6.1.0
如果你需要任何额外的信息,请在评论中注明!

cclgggtu

cclgggtu1#

下面是一个增强的代码:

const { Client, GatewayIntentBits } = require('discord.js');
const dialogflow = require('@google-cloud/dialogflow');

const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMembers,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
  ],
});

// Initialize Dialogflow client
const sessionClient = new dialogflow.SessionsClient({
  projectId: 'your-project-id',
  credentials: {
    client_email: 'your-client-email',
    private_key: 'your-private-key',
  },
});

const botChannelId = 'your-bot-channel-id';

client.on('ready', () => {
  console.log('Bot is ready!');
});

client.on('messageCreate', async (msg) => {
  if (msg.content.startsWith('!') || msg.content.startsWith('?') || msg.channelId !== botChannelId || msg.author.bot) {
    return;
  }

  // Create a new session
  const sessionPath = sessionClient.projectAgentSessionPath('your-project-id', 'unique-session-id');

  // The text query request to Dialogflow
  const request = {
    session: sessionPath,
    queryInput: {
      text: {
        text: msg.content,
        languageCode: 'en-US', // Replace with the appropriate language code
      },
    },
  };

  try {
    const responses = await sessionClient.detectIntent(request);

    const responseText = responses[0].queryResult.fulfillmentText;
    msg.reply(responseText);
  } catch (error) {
    console.error('Error communicating with Dialogflow:', error);
  }
});

// Log in to Discord with your bot's token
client.login('your-bot-token');```

Well, the fix is:
In your code, you have the following import statement:
```lang-js const {Dialogflow} = require('@google-cloud/dialogflow');```
Change to
```lang-js const dialogflow = require('@google-cloud/dialogflow');```

相关问题