Discord

Learn the basics of building a Discord bot with Voiceflow's Dialog Manager.

Build your assistant

In this guide, we are creating an assistant that is connected directly with Voiceflow.

Discord and VoiceflowDiscord and Voiceflow

Discord and Voiceflow

🚧

Before you start

Make sure you have:

  1. Setup a Discord bot on your DIscord server: How to
  2. Created a Voiceflow project. You need to first build a chat project on Voiceflow
  3. Template source code on Github

Setup

To obtain your versionID you have to go to your Voiceflow Project:

Find VersionIDFind VersionID

Find VersionID

Then copy the VERSION_ID from the URL in your address bar. When you are inside a Voiceflow project, your address bar should have a URL of the form: https://creator.voiceflow.com/project/{VERSION_ID}/...

apiKey
To obtain the Workspace API Key we have to go to our workspace where we have created our General Project. After this, we have to append to the URL /api-keys:

Find API KeyFind API Key

Find API Key

Video Walkthrough

In the video below, we cover the entire integration between Voiceflow and Discord.

Source Code

const Discord = require('discord.js');
const axios = require('axios');

const discordClient = new Discord.Client();

//Add start command to activate bot in Discord
const START_COMMAND = '!starter';
let isRunning = false;

// Add Voiceflow versionID and apiKey. You can find this on the project 
const versionID = "xxx";
const apiKey = "VF.xxx.xxx";


discordClient.on('message', async (message) => {
    if (message.author.bot) 
        return;

    if (message.content !== START_COMMAND && ! isRunning) 
        return;
    

    const userID = message.author.username;

    async function interact(userID, request) {
      // Dialog Manager POST request
      const response = await axios({
            method: "POST",
            url: `https://general-runtime.voiceflow.com/state/${versionID}/user/${userID}/interact`,
            headers: {
                Authorization: apiKey
            },
            data: {
                request
            }
        });

        for (const trace of response.data) {
            switch (trace.type) {
                case "text":
                case "speak":
                    {
                        message.channel.send(trace.payload.message);
                        break;
                    }
                case "end":
                    {
                        return false;
                    }
            }
        }
        return true;
    }
    isRunning = message.content === START_COMMAND ? await interact(userID, {type: "launch"}) : await interact(userID, {
        type: "text",
        payload: message.content
    });
    if (! isRunning) {
        message.channel.send("Try again by saying the starter command");
    }
});

//Login to Discord bot using your bot token
discordClient.login("{discord_bot_token}");

#Voiceflow Community Challenge

Try triggering a response from the assistant through a message reaction (e.g. with a 😊 )

Share it on Twitter and make sure to mention @voiceflow!


Did this page help you?