by Lazy Sloth
Slack Mention Poem Generator
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.command("/hello-socket-mode")
def hello_command(ack, body):
user_id = body["user_id"]
ack(f"Hi, <@{user_id}>!")
@app.event("app_mention")
def event_test(body, say, event):
user_message = re.sub('<.*?>', '', body['event']['text']).strip()
thread_ts = event['ts']
if len(user_message) == 0:
say("Sorry, I couldn't generate a joke as there was not enough text. Please provide more text.", thread_ts=thread_ts)
else:
say("Generating a miracle, please wait...", thread_ts=thread_ts)
poem = llm_prompt(f"Write a joke about {user_message} in 150 words or less", model="gpt-4", temperature=0.8)
Created: | Last Updated:
Introduction to the Slack Mention Poem Generator Template
Welcome to the Slack Mention Poem Generator template! This template allows you to create an app that listens for mentions on Slack and responds with a custom-generated poem based on the user's message. It's a fun and interactive way to engage with your team or community on Slack.
To get started, simply click Start with this Template on the Lazy platform. This will set up the template in your Lazy Builder interface, and you'll be ready to customize it to your liking.
Setting Environment Secrets
Before you can use this template, you'll need to set up two environment secrets:
- SLACK_BOT_TOKEN
- SLACK_APP_TOKEN
These tokens are essential for authenticating your app with Slack. Here's how to obtain and set them:
- Create a new app in your Slack workspace.
- Add the bot scope to your app.
- Install the app in your workspace.
- Enable Socket Mode for the app in the Slack API settings.
- Generate an App-Level token (SLACK_APP_TOKEN).
- Once you have the tokens, go to the Environment Secrets tab within the Lazy Builder.
- Enter the tokens as secrets with the names SLACK_BOT_TOKEN and SLACK_APP_TOKEN.
External Integrations
No external integrations are required for this template beyond setting up the Slack app and obtaining the necessary tokens as described above.
Using the Test Button
After setting up your environment secrets, you can use the Test button to deploy your app. The Lazy CLI will guide you through any additional user input that may be required. Once the app is running, you'll be able to interact with it directly in your Slack workspace.
When someone mentions your app in a Slack channel, the app will acknowledge the mention and then use the built-in 'abilities' module to generate a poem based on the message content. The poem will be posted as a reply in the same thread.
How to Use the Slack Mention Poem Generator
Once your app is deployed and running, here's how to use it:
- Mention your Slack app in a message by typing
@your_app_name
followed by a message. - The app will post a loading message indicating that a poem is being generated.
- After a short wait, the app will reply in the same thread with a custom poem based on the text you provided.
Enjoy engaging with your Slack community in a creative and entertaining way with your very own AI-powered poem generator!
If you need further assistance or have any questions, please reach out to our customer support team. We're here to help you make the most of your Lazy experience!