OpenAI + Slack = The Perfect Pair for Smart Assistants!
Create your own AI Powered Slack Bot Application
In today’s fast-changing digital world, communication and working together are essential and Slack has become a revolutionary platform for teams. But what if I told you that you can improve your Slack experience by connecting it with OpenAI APIs? Imagine having a Slack bot that not only understands your questions and replies to them but also has access to extensive knowledge and advanced language abilities. We can achieve this with the help of OpenAI’s powerful models.
This article will explore the exciting world of integrating OpenAI APIs in Slack bots, unlocking a world of possibilities for enhanced productivity, intelligent automation, and seamless workflows.
Building the Slack App using Bolt and NodeJS
We will be using NodeJS
to create a Slack application that will turn into an AI-powered smart assistant that can be used as a Smart Virtual assistant, Code assistant, Language Translator, Content generator, and so on. We can create a Slack application using Java, Python, and NodeJS
. You can find more about it here and here. I will be referring to this Slack document.
- Create your Slack App.
2. Give it a name and select the workspace, under which you want to add the app. After creating the app, it will be added under the selected workspace.
3. Next, go to Features > OAuth & Permissions screen. Scroll down to Bot Token Scopes to specify the OAuth scopes.
4. Now you will need to add an app-level token.
Go to Settings > Basic Information screen, scroll down to App-Level Tokens, and add an app-level token for your app.
After this point, you will be able to install the application to the Slack Workspace.
Add the Slack application to the Slack Workspace by going to Settings → Install Apps and then proceed with Install to Workspace.
You can also add slash commands for specific defined operations. Let’s add a command to the Slack app. Slash Commands and proceed with Create New Command. You can find more about Slash Commands here.
Creating the NodeJS
App
Let’s create a NodeJS application that will be the backbone of the Slack app. After creating a node application, follow these steps.
- Create a
.env
file at the project root directory to store all environment variables. We will be using thedotenv
library for developer dependencies.
touch .env
2. We will need the following keys and tokens from OpenAI and Slack API. These will be used to configure the Slack application and OpenAI authentication.
OPENAI_API_KEY = OPEN_API_KEY : https://platform.openai.com/account/api-keys
TOKEN = TOKEN : Find in the OAuth & Permissions tab
SIGNING_SECRET = SIGNING_SECRET: Find in Basic Information Tab
APP_TOKEN = APP_LEVEL_TOKEN: Find in Basic Information Tab
3. Install @slack/bolt. It is a NodeJS library that helps to connect with desired Slack applications. Execute the following command in the project root directory.
$ npm install @slack/bolt
4. Create an app by calling the constructor, a top-level export.
const { App } = require('@slack/bolt');
const app = new App({
signingSecret: process.env.SIGNING_SECRET,
token: process.env.TOKEN,
appToken: process.env.APP_TOKEN
});
(async () => {
// Start your app
await app.start();
console.log('⚡️ Bolt app is running!');
})();
5. Now let’s add a message handler that will catch every text message the user sends to this Slack application.
app.message(async ({ message, say }) => {
try {
let cmd = message.text // The inputted parameters
console.log(cmd);
} catch (error) {
console.log("err")
console.error(error);
}
});
At this point, this application should be able to receive the text message sent by the user. You can check if we are receiving the text messages by logging those using console.log().
Interesting Part — Setting up Open AI
- Install
openai
. Execute the following command in the project root directory.
$ npm install openai
2. Let’s configure Open-AI.library with the account secret key.
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
3. Using the createChatCompletion()
API, we can create the chat completion.
let prompt = "Hi, How are you?"
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [
{"role": "system",
"content": prompt}
],
});
4. Let’s combine the Slack message and openAI chat completion.
app.message(async ({ message, say }) => {
try {
let cmd = message.text
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [
{"role": "system",
"content": cmd}
],
});
say(completion.data.choices[0].message.content);
} catch (error) {
console.log("err")
console.error(error);
}
});
And that’s it. You have created the AI-powered Slack bot. Let’s run the NodeJS application and test the Slack app we just developed.
$ node app.js
Output :
[INFO] socket-mode:SocketModeClient:0 Going to establish a new connection to Slack ...
⚡️ Bolt app is running!
[INFO] socket-mode:SocketModeClient:0 Now connected to Slack
Here are the exciting responses got while interacting with an AI-powered Slack Assistant that we have created.
Summary
We can create a Slack application to perform predefined tasks in our workspace. To add more power to the application, we used NodeJS. We created the application with NodeJS using the @slack/bolt library.
You can check for Bolt for Java & Bolt for Python, if you are willing to go for Java or Python instead of NodeJS.
After creating the Slack Application, we discovered how to integrate the OpenAI in the Node Application. We integrated the power of OpenAI in our NodeJS, and it made our Slack Application into a Smart Virtual Assistant.