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"])

def hello_command(ack, body):
    user_id = body["user_id"]
    ack(f"Hi, <@{user_id}>!")

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

About this template

This app listens to mentions of our app on Slack, sends a loading message, and then responds with a poem generated by an AI in the same thread as a reply to the original mention. The poem is based on the message sent by the user. The app requires two environment variables: SLACK_BOT_TOKEN and SLACK_APP_TOKEN. These tokens are used to authenticate the app with Slack. To generate these tokens, you need to create a new app in your Slack workspace, add the bot scope, install the app in the workspace, enable Socket Mode for the app in the Slack API settings, and generate an App-Level token.

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:


These tokens are essential for authenticating your app with Slack. Here's how to obtain and set them:

  1. Create a new app in your Slack workspace.
  2. Add the bot scope to your app.
  3. Install the app in your workspace.
  4. Enable Socket Mode for the app in the Slack API settings.
  5. Generate an App-Level token (SLACK_APP_TOKEN).
  6. Once you have the tokens, go to the Environment Secrets tab within the Lazy Builder.
  7. 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:

  1. Mention your Slack app in a message by typing @your_app_name followed by a message.
  2. The app will post a loading message indicating that a poem is being generated.
  3. 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!

Last published
April 7, 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.


Basic Slack Bot

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


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 Mention Poem Generator