# How to Build an AI Agent in Slack [DIY Guide]

# Objective

By the end of this DIY guide, you’ll have:

1. A Slack Bot
    
2. A backend that can
    
    * Listen to user messages or alerts in a channel and take agentic action based on prompts or steps that you might have in mind.
        
    * Query your Grafana instance, analyse logs/dashboards and send info about anomaly in reply to an alert/message in your Slack channel
        

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1753595500164/544dba64-3148-45ac-abdd-5b40ed903954.png align="center")

## Pre-requisites

1. Python [uv](https://docs.astral.sh/uv/getting-started/installation/) (package manager[)](https://docs.astral.sh/uv/getting-started/installation/)
    
2. Ngrok to expose the slackbot server. [Setup instructions](https://ngrok.com/docs/getting-started/)
    
3. Grafana (Optional)
    

## Step 0: Clone the repo:

Repository Link - [https://github.com/DrDroidLab/slack-ai-bot-builder](https://github.com/DrDroidLab/slack-ai-bot-builder)

## [**Step 1: Building the Slack Bot with an integrated backend**](https://github.com/DrDroidLab/slack-ai-bot-builder)

The backend repo setup here, behaves in multiple ways:

* Acts as an MCP Client for any AI calls you might want to make
    
* Acts as a server to accept webhooks from Slack and manage configurations
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1753595599394/56688817-8307-4257-a31e-fd26e97174f6.png align="center")

### **Setting up the Ngrok Tunnel**

Expose port 5000 of your localhost, using the command:

```plaintext
ngrok http 5000
```

You will receive a HTTPS URL of the form [https://abc123.ngrok.io](https://abc123.ngrok.io)

Which is pointing to port 5000 of your system.

**Note:** ***We have not setup a server running on port 5000 yet***, but that is fine since ngrok is independent of that, and exposes the port regardless.

### **Creating the Slack Application**

1. Go to Slack API Apps – [https://api.slack.com/apps](https://api.slack.com/apps)
    
2. Click on Create App, and select the option ‘From a manifest’
    
3. Copy the [manifest json](https://github.com/DrDroidLab/slack-ai-bot-builder/blob/main/slack_manifest.json) from the repository and **<mark>replace the placeholder ‘&lt;hostname&gt;’ with the HTTPS URL you got from ngrok. Include the “https://” part as well.</mark>**
    
4. Install the app in your workspace.
    
5. Copy the credentials for the slack application into credentials.yaml
    
    1. app\_id, app\_name and signing secret can be found in the basic information tab.
        
    2. Bot-auth-token can be found in the Oauth & Permissions tab.
        
    3. openai\_key from openai - if you plan to use AI based workflows.
        
6. Create a channel called #drdroid-slack-bot-tester in your slack workspace & add the bot to the channel.
    

### **Setting up the bot server**

Run the following commands to set up your virtual environment and activate it.

```plaintext
uv env venv
source .venv/bin/activate
```

Install dependencies using:

```plaintext
uv sync
```

Now we can finally run the bot server using:

```plaintext
uv run python app.py
```

The server is now running on port 5000, and exposed to the outside world via your ngrok tunnel.

### **Testing the bot**

Add the bot to the drdroid-slack-bot-tester workspace that you had previously created.

And just type in a ‘hi’. The bot should send you a sample response.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1753596133455/a92590b2-6b31-479b-a209-015fa52872b3.png align="center")

## [**Step 2: Inte**](https://github.com/DrDroidLab/slack-ai-bot-builder)[**grating AI**](https://ngrok.com/docs/getting-started/)

There is already an example workflow for AI (name: "chatbot") in the [workflows.yaml](https://github.com/DrDroidLab/slack-ai-bot-builder/blob/main/workflows.yaml). It is just boilerplate code, you can modify it as required. For now, you can tag your bot in the drdroid-slack-bot-tester, and chat with it.

* Add your OpenAI/LLM key
    

For example:

```plaintext
Message in Slack: chatbot How to debug Kubernetes CrashLoopBackOff error? @bot
Message in Slack: chatbot I'm getting this alert. What does it mean? @bot
```

## **Step 3: Making the bot an Agent by giving AI access to different tools (Grafana for demo)**

MCP Servers help abstract out any API and making them accessible to AI.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1753595526326/211ca3ee-f4b8-45f2-a1f0-3520850160a6.png align="center")

### **Setting up Grafana MCP Server**

Clone the following repository:

Repository URL - [https://github.com/DrDroidLab/grafana-mcp-server](https://github.com/DrDroidLab/grafana-mcp-server)

Navigate into the root directory of the repository.

<mark>Populate the </mark> `src/grafana_mcp_server/config.yaml` <mark> with your grafana credentials.</mark>

Install and setup the dependencies using:

```plaintext
uv venv .venv
source .venv/bin/activate
uv sync
```

Run the MCP server:

```plaintext
uv run -m src.grafana_mcp_server.mcp_server
```

Your MCP server is now running on port 8000.

### **Creating an AI Grafana workflow in slack-bot-builder**

There is already an example workflow for grafana ai in the workflows.yaml. 

We will be running the script scripts/grafana\_ai\_tool.py in this workflow, it is just boilerplate code, you can modify it as required.  
Now you can tag your bot in the drdroid-slack-bot-tester, and ask it to do various things from grafana.  
For example:

```plaintext
Message in Slack: Fetch me logs from the currencyservice in grafana ai. 
Message in Slack: Fetch and analyse the Go Microservices dashboard from grafana ai
```

## Next Steps:

Now that you’ve been able to setup a bot, here are a few things you can do:

* Productionise it from your current ngrok setup to a static endpoint
    
* Integrate with Grafana or open source [MCP servers](https://glama.ai/mcp/servers) of your favourite tool that you want to leverage for automation
    
* Add custom prompts and scripts
    

Stuck anywhere? Ask on our [Discord](https://discord.gg/AQ3tusPtZn)
