ai-agenttutorialapislackintegrationbotwebhooksreal-time

Integrating AI Agents with External APIs - Part 2: Slack Integration

By AgentForge Hub8/14/20253 min read
Intermediate
Integrating AI Agents with External APIs - Part 2: Slack Integration

Ad Space

Integrate AI Agents with APIs — Part 2: Slack

In this part, we’ll connect our AI agent to Slack so it can interact with users in real time.
Instead of dumping all the code at once, we’ll go step‑by‑step: introducing a concept, showing a short snippet, and explaining it.


Step 1 — Install Dependencies

Make sure you have the required libraries installed:

pip install slack-bolt openai

Step 2 — Initialize the Slack App

We’ll use the slack_bolt library to create our Slack bot and authenticate it with tokens from your Slack app settings.

from slack_bolt import App

app = App(
    token="xoxb-your-slack-bot-token",
    signing_secret="your-slack-signing-secret"
)

👉 This sets up your bot with the correct credentials.


Step 3 — Handle a Basic Message

Let’s make the bot respond when someone types “help” in Slack.

@app.message("help")
def handle_help(message, say):
    say("👋 I’m your AI agent. How can I assist you today?")

👉 Now your bot reacts to the word help.


Step 4 — Connect Slack to the AI Model

We’ll integrate a fine‑tuned OpenAI model so Slack messages get processed by your agent.

import openai

@app.message("ask")
def handle_ask(message, say):
    user_text = message['text']
    response = openai.ChatCompletion.create(
        model="ft:gpt-3.5-turbo:your-model-id",
        messages=[
            {'role': 'system', 'content': 'You are a helpful Slack assistant.'},
            {'role': 'user', 'content': user_text}
        ]
    )
    say(response['choices'][0]['message']['content'])

👉 Here, any message that starts with “ask” will be passed to the fine‑tuned model and the reply is sent back to Slack.


Step 5 — Run the App

Finally, run the app to start listening to Slack events.

if __name__ == "__main__":
    app.start(port=3000)

👉 Keep this running and your Slack app will be live.


Full Example

For those who want the complete code in one place, here it is:

from slack_bolt import App
import openai

app = App(
    token="xoxb-your-slack-bot-token",
    signing_secret="your-slack-signing-secret"
)

@app.message("help")
def handle_help(message, say):
    say("👋 I’m your AI agent. How can I assist you today?")

@app.message("ask")
def handle_ask(message, say):
    user_text = message['text']
    response = openai.ChatCompletion.create(
        model="ft:gpt-3.5-turbo:your-model-id",
        messages=[
            {'role': 'system', 'content': 'You are a helpful Slack assistant.'},
            {'role': 'user', 'content': user_text}
        ]
    )
    say(response['choices'][0]['message']['content'])

if __name__ == "__main__":
    app.start(port=3000)

Recap

  • Installed Slack + OpenAI dependencies.
  • Set up the Slack app with tokens.
  • Made the bot respond to messages.
  • Connected the bot to a fine‑tuned model.
  • Ran the app to go live.

Next, we’ll see how to integrate with other APIs like Discord and Zapier to expand your agent’s reach.

Ad Space

Recommended Tools & Resources

* This section contains affiliate links. We may earn a commission when you purchase through these links at no additional cost to you.

OpenAI API

AI Platform

Access GPT-4 and other powerful AI models for your agent development.

Pay-per-use

LangChain Plus

Framework

Advanced framework for building applications with large language models.

Free + Paid

Pinecone Vector Database

Database

High-performance vector database for AI applications and semantic search.

Free tier available

AI Agent Development Course

Education

Complete course on building production-ready AI agents from scratch.

$199

💡 Pro Tip

Start with the free tiers of these tools to experiment, then upgrade as your AI agent projects grow. Most successful developers use a combination of 2-3 core tools rather than trying everything at once.

🚀 Join the AgentForge Community

Get weekly insights, tutorials, and the latest AI agent developments delivered to your inbox.

No spam, ever. Unsubscribe at any time.

Loading conversations...