Slack Mention Poem Generator

Test this app for free
57
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)
Get full code

Created: | Last Updated:

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:

  • SLACK_BOT_TOKEN
  • SLACK_APP_TOKEN

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!

Technologies

Slack integrations and API Slack integrations and API

Similar templates