Home

Awesome

Metabot for Slack

metabot server fastapi_metabot package

modules/vacations modules/help modules/feedback

Metabot is a Slack bot written using FastAPI which allows you to dynamically add new functionality (commands, interactive components & modals) via modules.

Module example

Step 1: pip install fastapi-metabot

Step 2: create example.py

from fastapi import FastAPI
from fastapi_metabot.module import Module
from fastapi_metabot.utils import (
    command_metadata, 
    action_metadata, 
    async_slack_request
)

app = FastAPI()

module = Module(
    name='example',
    description='Example module',
    module_url='http://example:8000',  # URL of this module
    metabot_url='http://metabot:8000'  # URL of the Metabot server
)


# /meta example test "param" "optional"
# command arguments are based on your function parameters
@module.command('test')  
async def test(param: int, optional: str = 'default') -> None:
    # argument values are casted from str according to the annotation
    # use @module.converter(Type) to provide custom cast functions
    print(type(param))  # <class 'int'>

    # you can access any Slack API method via the client
    await async_slack_request(
        method='chat_postMessage',
        payload={
            'text': 'Hello, World!',
            'channel': '#general'
        }
    )

    # Slack command/action payloads are stored inside contextvars
    payload = command_metadata.get()
    print(payload.user_name)  # current user's name


# note that action and view ids must be unique across ALL modules
@module.action('action_id')
async def action() -> None:
    # this is called when someone triggers an interactive component
    payload = action_metadata.get()
    print(payload.actions)


@module.view('callback_id')
async def view() -> None:
    # this is called when someone submits a modal
    payload = action_metadata.get()
    print(payload.view)


# this creates a few routes for Metabot and starts the heartbeat
module.install(app)

Step 3: uvicorn example:app --reload

How it works

architecture

Metabot and its modules communicate via HTTP APIs:

Modules

For more details, use the /meta help me command.