Let's build a Bot with Discord and Dialogflow

in #chatbot4 years ago

My first time with a chatbot was with A.L.I.C.E, an impressive piece of code that made me dream of the future, it was at that time when I decided that I would give my life to technology ...

Pre requirements
To build your bot you need the following:

Be 13 or older and have a cell phone number to verify your Google account.
Have the skills to fill out forms including copying and pasting text.
Open an account on Google.
Open an account in Github (for the account in Glitch).
Open an account in Glitch.
Install the Discord client (optional because #clientWeb) and
... open an account there, too.
Have some affinity or interest in programming to tolerate the length of this post.
Stack / Architecture
This is a high level diagram with the architecture, the stack of tools and the flow that we are going to use to connect our bot:
First Part: Discord - The medium where our bot lives
Let's start preparing a medium where the bot can interact with us, this medium is going to be a chat platform and in this tutorial I chose Discord because #gamer and because besides being great, it's free.

To do this, you need to open the discord client (web or desktop), and click on the + symbol on the left side and follow the instructions. You can consult this guide.1_4787JbhB-9--NYnr0cjJIg.png
For now you can not do much more than chat with yourself on the server that you created, but we are going to install a Discord app so that the bot has a communication channel.

Visit this page to create a new application. Give it a good name!

When you save the initial configuration you will be presented with more options, for now check the name, add a description and an icon.
We need our bot to have a user in Discord, click on the Create a Bot User button:1_ZoccUs8wOypTOubzd9jNfQ.png
After clicking on the button you will see the tag of your bot and a couple of options, click on click to reveal to see your token, this token is your access credentials to the discord API, make sure to copy it somewhere because we are going to use it soon.

The next step is to invite your bot to your Discord server, for that click on the Generate OAuth2 URL button and copy the URL that appears in that window
Paste the URL in a browser window and add the bot to your server.
Now our bot is authorized and we can see it in the list of users in the Discord client, for now it should appear Offline.
Tu bot ahora tiene acceso a un medio con capacidades de enviar y recibir mensajes.

Segunda parte: Código 💚 JavaScript — El sistema nervioso
Para conectar el entorno de nuestro bot con su cerebro necesitamos:

Un cliente programático para poder conectar a nuestro bot con Discord
Algo de código para utilizar este cliente
Un lugar para ejecutar este código
Ya he preparado todo el código que necesitamos pero para ejecutarlo vamos a utilizar Glitch, una plataforma que disfruto mucho utilizar porque te permite escribir y probar cosas super rápido. Sigue estos pasos:

Ábrete una cuenta en Glitch.com (vas a necesitar tu cuenta de GitHub)
Dale click aquí para crear un remix del proyecto base, esto va a generar un proyecto en tu cuenta y a copiar el código para hacer tu propia versión.
Selecciona el archivo .env en tu proyecto y proporciona las primeras dos variables: BOT_KEYWORD que es la palabra con la que vamos a iniciar una conversación con el bot (por ejemplo su nombre) y DISCORD_API_TOKEN que obtuviste en la parte anterior
El archivo .env debe verse parecido a este ejemplo:
Now check your Discord client and you should be able to see that your bot is online
Third part: Dialogflow - The brain of the bot
Dialogflow is an impressive tool that deserves a complete series of posts to capture a bit of its power. In this exercise, we will use its most elementary functionality, which is to understand simple intentions and solve with pre-programmed answers.

First of all you need to get an account in Dialogflow, you are going to need an account with Google.

Once you have your account, we will create an agent by clicking on the Create Agent link in the menu on the left.1_P2-S1K96Cj3D1cqrfv57Pg.png
In the options give the agent a name and make sure to select the correct language and time zone.
After clicking on CREATE you will see the options of the bot, if you advance with the scroll in this screen you will be able to consult the access keys.

Copy the Client Access Token and paste it into the variable DIALOGFLOW_CLIENT_ACCESS_KEY in the .env file of your project that we configured in Part Two.

Dialogflow uses the concept of Intent to describe the way in which it takes the text of a message and interprets it in a user's intention.

From the Dialogflow console we configure the first Intent, select INTENTS in the main menu and then hit the CREATE INTENT button on the top right.
Give your Intent a name, my recommendation is that you use a taxonomy for your attempts using points in the name to classify them, for example:

To train our bot to respond to a generic greeting we create an Intent that we can call: smalltalk.greetings.hello, and we add some training phrases (Training Phrases), these phrases are initial examples with which the bot learns to understand the intentions of his interlocutor.
IMPORTANT NOTE !!!: Be sure to give a name to the action in the Action and parameters section, this is important so that our code knows that Dialogflow has identified an intention. You can use the same name as the Intent.
Finally we are going to add some answers in the Responses section, one of these answers will be sent when the bot recognizes that we are greeting him. Then click on the SAVE button to save the Intent and the AI ​​can train with this new data, this usually takes a few seconds.



Coin Marketplace

STEEM 0.20
TRX 0.06
JST 0.025
BTC 27422.11
ETH 1741.31
USDT 1.00
SBD 2.83