by Lazy Sloth
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()
Basic Slack Bot
Created: | Last Updated:
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.
Technologies
![Slack](https://a.slack-edge.com/80588/marketing/img/meta/slack_hash_256.png)