Basic Slack Bot

 
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()

About this template

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.

Category
Technology
Last published
July 20, 2024

More templates like this

AI Query Generator Slack Bot for BigQuery

This app allows users to interact with a Slack bot, ask a question about the data in a table or request the table schema, and then uses the latest ChatGPT to generate a query that is executed on BigQuery to return the results. The app includes a retry mechanism for query generation in case of an error (up to two retries) and provides the LLM with the table info to generate more accurate queries. The table schema is only printed if it is successfully retrieved. All errors from retries are now passed to the LLM. The generated query is printed before the results, and the results are displayed in a pretty table format. The bot uses the Slack API to send and receive messages and parses the user's message to determine the action to take. The bot always responds in a thread to the original message.

Python
Slack
OpenAI

Basic Slack Bot

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

Slack

Create Slack Channel using API

An app for Slack integration allowing the creation of public channels using the Slack API. This webhook app requires 'SLACK_BOT_TOKEN' to authenticate with slack api. It also requires channels:manage scope enabled to create channels. Further enchancements to prompts may allow the app to add the user to the channel.

Slack
Home
/
Basic Slack Bot