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 is designed to help you create a Slack bot that responds with a friendly greeting whenever it is mentioned in a Slack channel. This is a great starting point for those looking to integrate a bot into their Slack workspace without worrying about complex setup processes.

Getting Started with the Template

To begin using this template, simply click on "Start with this Template" in the Lazy Builder interface. This will pre-populate the code in the Lazy Builder, so you won't need to copy, paste, or delete any code manually.

Initial Setup

Before you can test 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 obtain these values:

  • Go to the Slack API website and create a new app.
  • From the 'OAuth & Permissions' page, copy the 'Bot User OAuth Token' which usually starts with `xoxb-`.
  • For the 'App-Level Token', go to the 'Basic Information' page of your app and generate a token with connections:write permission. This token will start with `xapp-`.
  • Once you have these tokens, go to the Environment Secrets tab in the Lazy Builder and add them as `SLACK_BOT_TOKEN` and `SLACK_APP_TOKEN` respectively.

Test: Pressing the Test Button

With your environment secrets set up, you're ready to test your Slack bot. Press the "Test" button in the Lazy Builder. This will deploy your application and launch the Lazy CLI. You will not need to provide any additional user input at this stage.

Using the App

Once your app is deployed, you can interact with your Slack bot directly within your Slack workspace. Mention your bot in any channel by typing `@YourBotName` and it will respond with "Hi I'm the slack bot".

Integrating the App

If you wish to further integrate your Slack bot or expand its capabilities, you can modify the code within the Lazy Builder. For example, you can add more event handlers to respond to different types of events in Slack. Remember to update your Slack app's event subscriptions to match the events your bot will handle.

That's it! You now have a basic Slack bot that can greet users in your workspace. Enjoy customizing and expanding your bot's abilities to suit your needs.

Category
Technology
Last published
June 15, 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

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

Search Slack Message using API

A Slack app with search functionality for messages based on query, username, and channel name. Requires SLACK_USER_TOKEN with search:read users scope enabled.

Slack
Home
/
Basic Slack Bot