Basic Slack Bot

Test this app for free
65
import os
import re
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
from abilities import llm_prompt

# Install the Slack app and get xoxb- token in advance
app = App(token=os.environ["SLACK_BOT_TOKEN"])

@app.event("app_mention")
def event_test(body, say, event):
    say("Hi I'm the slack bot", thread_ts=event['ts'])

if __name__ == "__main__":
    SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()
Get full code

Created: | Last Updated:

This is a simple starting point for a Slack bot it just responds hi to a mention.

Introduction to the Basic Slack Bot Template

Welcome to the Basic Slack Bot template! This template provides a simple starting point for creating a Slack bot that responds with a greeting when mentioned in a Slack workspace. Whether you're looking to build a more complex bot or just want to learn the ropes of Slack bot development, this template is a great place to start.

Getting Started with the Template

To begin using this template, click on "Start with this Template" on the Lazy platform. This will set up the template in your Lazy Builder interface, pre-populating the code so you can start customizing your Slack bot right away.

Initial Setup

Before you can test and use your Slack bot, you'll need to set up a couple of environment secrets within the Lazy Builder. These secrets are the Slack Bot Token (`SLACK_BOT_TOKEN`) and the Slack App Token (`SLACK_APP_TOKEN`). Here's how to acquire these tokens:

  1. Go to the Slack API website and create a new app.
  2. From the 'OAuth \& Permissions' page, add the necessary bot scopes (like `chat:write`) and install the app to your workspace to get the `SLACK_BOT_TOKEN`.
  3. For the `SLACK_APP_TOKEN`, you'll need to enable Socket Mode in your Slack app settings and generate an app-level token.
  4. Once you have both tokens, go to the Environment Secrets tab in the Lazy Builder and add them as `SLACK_BOT_TOKEN` and `SLACK_APP_TOKEN` respectively.

With these tokens set, your Slack bot will be able to authenticate with Slack's servers and respond to events in your workspace.

Test: Pressing the Test Button

After setting up your environment secrets, it's time to test your Slack bot. Press the "Test" button in the Lazy Builder. This will deploy your app and start the bot in the background. You won't need to provide any additional user input at this stage.

Using the Slack Bot

Once your bot is running, you can go to your Slack workspace and mention your bot in any channel or direct message. To do this, type `@YourBotName` followed by any message. Your bot should respond with "Hi I'm the slack bot" in the thread of the mention.

Integrating the Slack Bot

After confirming that your bot responds to mentions, you can integrate it further into your Slack workspace or external tools. For example, you might want to set up more event listeners for different types of Slack events, or you might want to use the bot to interact with other APIs and services.

If you plan to integrate with other APIs, remember to add any necessary tokens or keys as environment secrets in the Lazy Builder, just like you did with the Slack tokens.

That's it! You've successfully set up and tested your Basic Slack Bot using the Lazy template. From here, you can continue to expand on the bot's abilities and customize it to fit your needs.

Technologies

Slack Slack