This is a Python-based AI Slack Bot using the OpenAI GPT-4 model and MySQL for storing conversation data. The bot listens for mentions in a Slack channel and generates AI responses using OpenAI's API. All conversation data is then saved to a MySQL database.
- Python 3.6 or later
- OpenAI
- slack-bolt and slack-sdk Python libraries
- mysql-connector-python Python library
- python-dotenv Python library
- SlackApi
To create a Slack API App and configure the necessary settings, follow these steps:
-
Go to the Slack API website and sign in to your Slack account.
-
Click the Create New App button.
-
Choose a name for your app and select the workspace you want to develop it in, then click the Create App button.
-
Enable the Event Subscriptions feature in the app settings:
a. Click on Event Subscriptions in the left sidebar.
b. Toggle the Enable Events switch on.
c. Enter the Request URL (you will provide this later, after deploying your bot).
d. Scroll down to Subscribe to bot events, click Add Bot User Event, and add the following bot user event:
app_mention
. -
Add the necessary OAuth Scopes:
a. Click on OAuth & Permissions in the left sidebar.
b. Scroll down to the Bot Token Scopes section and click the Add an OAuth Scope button.
c. Add the following OAuth scopes:
app_mentions:read
,chat:write
, andusers:read
. -
Install the app to your workspace:
a. Scroll up to the Install App section and click the Install App to Workspace button.
b. Authorize the app in the prompted window.
-
Retrieve your app's tokens:
a. After installing the app, you will be redirected to the OAuth & Permissions page.
b. Copy the Bot User OAuth Token (starts with
xoxb-
) and paste it into your.env
file as the value forSLACK_BOT_TOKEN
.c. Copy the App Token (starts with
xapp-
) and paste it into your.env
file as the value forSLACK_APP_TOKEN
. -
Set up a public-facing server or a tunneling service like ngrok to expose your bot's server to the internet, which is needed for Slack events to reach your bot.
-
Once your bot is running and your server is publicly accessible, go back to the Event Subscriptions settings on the Slack API website and update the Request URL with your bot's public URL followed by the endpoint path, e.g.,
https://your-public-url.com/slack/events
. -
Save your changes, and your Slack API app should now be configured and ready to use with the AI Slack Bot.
To set up the MySQL database and server, follow these steps:
-
Install MySQL Server on your machine, or use a cloud-based MySQL service. For installation instructions, refer to the official MySQL documentation.
-
Create a new MySQL database and user for the AI Slack Bot:
a. Log in to your MySQL server using the command line or a GUI tool such as MySQL Workbench or phpMyAdmin.
b. Run the following SQL command to create a new database (replace
your_database_name
with the desired name):CREATE DATABASE your_database_name;
c. Run the following SQL command to create a new user and grant them privileges to the newly created database (replace
your_user_name
,your_password
, andyour_database_name
with the appropriate values):CREATE USER 'your_user_name'@'localhost' IDENTIFIED BY 'your_password'; GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_user_name'@'localhost'; FLUSH PRIVILEGES;
-
Create the
conversations
table in your MySQL database:a. Run the following SQL command to create the
conversations
table:CREATE TABLE conversations ( id INT AUTO_INCREMENT PRIMARY KEY, user_id VARCHAR(255) NOT NULL, user_input TEXT NOT NULL, ai_response TEXT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
- Fork the repository and clone it:
git clone https://github.com/yourusername/ai-slack-bot.git
cd ai-slack-bot
- Install the required dependencies:
pip install -r requirements.txt
- Copy the template.env file to a new file named .env and update the values with your credentials:
cp template.env .env
- Replace the placeholders with your actual credentials:
SLACK_APP_TOKEN=<My-SLACK_APP_TOKEN>
SLACK_BOT_TOKEN=<My-SLACK_BOT_TOKEN>
OPENAI_API_KEY=<My-OPENAI_API_KEY>
MYSQL_HOST=<MYSQL_HOST>
MYSQL_USER=<MYSQL_USER>
MYSQL_PASSWORD=<MYSQL_PASSWORD>
MYSQL_DATABASE=<MYSQL_DATABASE>
- Run the bot:
python main.py