Search Slack Message using API

By
 import os
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

app = FastAPI()
user_client = WebClient(token=os.getenv('SLACK_USER_TOKEN'))

class SearchParams(BaseModel):
    query: str
    username: str = None
    channel_name: str = None

@app.post("/search_messages")
async def search_messages(params: SearchParams):
    try:
        search_args = {
            'query': params.query,
            'sort': 'timestamp',
            'sort_dir': 'desc',
            'count': 300  # Increased count as per builder's request
        }

About this template

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.

Introduction to the Slack Message Search API Template

This template provides a ready-to-use API for searching messages in Slack based on a query, username, and channel name. It's designed to help you integrate Slack's search functionality into your own applications or workflows without the need to write the code from scratch. The API is built using FastAPI and requires a Slack user token with the appropriate permissions.

Clicking Start with this Template

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

Initial setup: Adding Environment Secrets

Before you can use this template, you'll need to set up an environment secret for the `SLACK_USER_TOKEN`. Here's how to obtain and set up your Slack user token: 1. Visit the Slack API website and create a new app if you haven't already. 2. Navigate to the 'OAuth & Permissions' page of your app settings. 3. Add the `search:read` and `users:read` scopes under the 'User Token Scopes' section. 4. Install the app to your workspace and authorize the requested permissions. 5. Copy the 'User OAuth Token' that is generated for you. 6. In the Lazy Builder interface, go to the Environment Secrets tab. 7. Create a new secret with the key `SLACK_USER_TOKEN` and paste the token you copied as the value.

Test: Pressing the Test Button

Once you have set up your environment secret, press the "Test" button. This will begin the deployment of the app and launch the Lazy CLI.

Using the App

After deployment, Lazy will provide you with a dedicated server link to use the API. Additionally, since this template uses FastAPI, you will also be provided with a link to the API documentation. This documentation will guide you on how to make requests to the API and understand the expected responses. Here's a sample request you might make to the API:
POST /search_messages
Content-Type: application/json

{
    "query": "hello world",
    "username": "example_user",
    "channel_name": "general"
}
And a sample response you might receive:
{
    "messages": [
        {
            "type": "message",
            "user": "U12345678",
            "text": "Hello world!",
            "ts": "1234567890.123456",
            "channel": {
                "id": "C12345678",
                "name": "general"
            },
            "username": "example_user"
        }
        // ... more messages
    ]
}

Integrating the App

If you wish to integrate this API into an external service or frontend, you will need to use the server link provided by Lazy after deployment. For example, you could make HTTP POST requests to the `/search_messages` endpoint from your frontend application to retrieve search results from Slack. Remember to handle user authentication and authorization appropriately when integrating this API into your applications, ensuring that only authorized users can access the search functionality. By following these steps, you can easily integrate Slack message search functionality into your applications using the Lazy platform.
Category
Technology
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.

Python
Slack
OpenAI

Basic Slack Bot

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

Slack

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
Home
/
Search Slack Message using API