Subscribe to learn about new product features, the latest in technology, solutions, and updates.
Artiificial Intelligence
Hosting
Docker
Dockerfiles
Slackbot
Tutorial
By the end of this guide, we will have a fully functioning Slack bot that can answer our questions about FL0 and its features using AI 🤖✨.
The advent of OpenAI’s API has empowered countless developers to create sophisticated chatbots without breaking a sweat 🧑💻.
We’ve noticed that there’s a considerable amount of curiosity within the developer community regarding the workings and features of FL0. This gave us the idea to build a simple chatbot using the GPT API.
In this article, we would be building a slack chatbot named !!FL0Bot!! which could answer questions regarding !!FL0!!. 💬
We would be using !!NodeJs!! for our backend and !!Postgres!! as database. Then we would be deploying our application effortlessly with the help of !!FL0!! 🚀.
As we prepare to embark on this journey, let’s kick things off with a little humor. Here’s an xkcd comic strip to lighten the mood 👇
Let’s start with building our chatbot 💬.
To speed up things, in this tutorial we would be using the “fl0zone/blog-express-pg-sequelize” template.
You may refer to this blog for more details regarding the tutorial 👇
https://fl0.com/blog/building-and-deploying-a-nodejs-postgres-api-in-less-than-30-minutes
In this template we have our basic NodeJs application and postgres database dockerized.
Here’s our !!docker-compose.yaml!! file for the same 🐳
Before we get started, here’s a look at our final project folder structure for reference 📂
And here’s a high level overview of what we are gonna build 👀
Now, let’s delve into the code 🧑💻
After we have created our new project using the above template, we would first need to install a few packages.
Now, we would need to get our OpenAI API key 🔑.
For this, we would need to create our account at platform.openai.com.
After this, we would select the “API” option, and click on “View API Keys” in account options.
Now, we would need to go ahead and create a new API key as shown below 👇
We would create a !!.env.example!! file to list the environment variables just for reference 👇
Then, we would need to go ahead and add these variables to our already present config file 📝
!!src/config/index.js!!
Now let’s get started with setting up our database. As we are using !!sequelize ORM!!, we would need to create models for our !!postgres!! database 🐘.
Here we would need to create a !!Chat!! in which we would be storing all the communication between the !!Fl0Bot!! and !!User!!.
Everytime a new request is made, we !!SELECT!! the recent chats from this database and send it for reference to the Fl0Bot. 💬
!!src/models/chat.js!!
Now let’s move on to writing the code for our ChatBot! 🤖
First we would create our !!handleAppMention!! function.
Here we’re parsing the text message, excluding any mentions, then looking for an existing user chat session or creating one if it doesn’t exist.
We’re fetching the last five chat messages to maintain the context of the conversation. 💬✨
Here we’re leveraging OpenAI’s API to get a completion response to the user’s input. 🤖
We are also adding a !!system!! in the conversation which is in the !!config.bot_system!!. This provides GPT the context about !!Fl0!!.
!!Example GPT System Prompt!!
!!src/index.js!!
🚗 Coming to our routes, we’ve set up an endpoint (!!/slack/action-endpoint!!) for Slack's !!action-events!!, in response to !!app_mention!! events.
And we are returning the response from !!handleAppMention!! function.
This response would be sent back by our Slack Bot.
!!src/index.js!!
Now that we have a functional API and database, its time to deploy them to a server! 🚀
In this tutorial, we’re utilizing !!FL0!!, a platform expertly designed for straightforward deployment of dockerized NodeJS applications, fully integrated with a database.
We would just need to push our repo to !!GitHub!!.
Now we would be deploying our project just by “Connecting our !!GitHub!! account" and selecting our project.
Then we would be adding our environment variables listed in !!.env.example!! file.
You may find a detailed process of deployment in this blog 👉 https://fl0.com/blog/building-and-deploying-a-nodejs-postgres-api-in-less-than-30-minutes
Now that our project is set up, let’s create our Slack App.
We would visit https://api.slack.com/apps and click on Create New App.
We would name our bot “FL0Bot” 😁
In the Event Subscriptions section, we would enable events, set the request URL, and subscribe to bot events: app_mention
We would also need to get our webhook and pass it as an environment variable to our FL0 hosting.
So, there we have it — a completely operational chatbot tailored to answer questions about !!FL0!! and its features, built using NodeJs, Postgres, and OpenAI's GPT, and seamlessly deployed with !!FL0!!!
Here’s the link to our repository for reference ➡️ Visit FL0Bot Repo
The power of OpenAI’s APIs and quick deployments with FL0, make it effortless to build our own AI bots 🚀🎉.
Head on to fl0.com to start building your own bots 🧑💻.
Tool and strategies modern teams need to help their companies grow.