sam-slash-slack


Namesam-slash-slack JSON
Version 0.1.2 PyPI version JSON
download
home_pagehttps://github.com/henryivesjones/sam-slash-slack
SummaryA SAM python framework for slack slash bots.
upload_time2023-08-24 20:33:27
maintainer
docs_urlNone
author
requires_python>=3.6
licenseGPL-3.0-or-later
keywords slack lambda bot slash command aws sam
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # sam-slash-slack

![Tests + Linting](https://github.com/henryivesjones/sam-slash-slack/actions/workflows/checks.yml/badge.svg?branch=main&event=push)
![pypi](https://img.shields.io/pypi/v/sam-slash-slack)
![License](https://img.shields.io/pypi/l/sam-slash-slack)
![Downloads](https://img.shields.io/pypi/dm/sam-slash-slack)

A python framework for building slack slash bots using AWS SAM. Get input parsing and validation, command routing, async responses, auto-generated help dialog, response visibility, and response message formatting all for free.

`sam-slash-slack` utilizes the AWS Serverless Application Model (SAM) to generate serverless resources to run your `sam-slash-slack` bot. The `sam-slash-slack` bot consists of two lambda functions and one SQS queue. The `api_handler` lambda function receives the POST request from slack enqueues the request in the SQS queue for async processing, and immediately responds. Slack requires a response within 3 seconds. This architecture enables your slash commands to take longer than that to respond. The `async_handler` receives messages from the `api_handler` and executes the given command.

Most of the code for `sam-slash-slack` is shared from [`slash-slack`](https://github.com/henryivesjones/slash-slack).

View [Slack slash command documentation](https://api.slack.com/interactivity/slash-commands) here.

```python
# EX: /slash-slack math 10 * 10
# Response: 100.0
import os

from sam_slash_slack import Flag, Float, SAMSlashSlack, String

slash = SAMSlashSlack(signing_secret=os.environ['SLACK_SIGNING_SECRET'])
api_handler = slash.get_api_handler()
async_handler = slash.get_async_handler()


@slash.command("math")
def math_fn(
    x: float = Float(),
    symbol: str = Enum(values={"*", "+", "-", "/"}),
    y: float = Float(),
):
    if symbol == "*":
        return x * y
    if symbol == "+":
        return x + y
    if symbol == "-":
        return x - y
    if symbol == "/":
        return x / y
```

# Why use `sam-slash-slack`?

Building a slack slash bot can seem very straightforward at first, however there are some complexities that make it difficult. `sam-slash-slack` handles all of the complexities for you letting you focus on the bot response handlers.

You don't have to worry about deployment, as you can let SAM and lambda functions do the heavy lifting.

## Webhook signature verification

`slash-slack` will verify that incoming requests were made by slack by validating the request signature. To disable signature verification use the `dev=True` option when creating the `SAMSlashSlack` object.

## Command Response Timeout/Async responses

Slack requires that the slash bot webhook be responded to within 3 seconds.

Often times the action being taken by the bot will depend on external services which might not respond within 3 seconds.

`sam-slash-slack` sends an immediate `200` response to the webhook request, and runs the command function asynchronously. When the command function finishes, the response is sent back to slack using the `response_url` from the request.

You can optionally add content to the immediate response to let your user know that something is being
done in the background. A global/default response can be set with the `acknowledge_response` parameter on
the `SAMSlashSlack` class, or at the command level with the `acknowledge_response` parameter on the `command` decorator.
The value passed to `acknowledge_response` will be passed to `blocks._make_block_message` and can be a `str`, `block`, `list[str]`, or `list[block]`
where a `block` is a [block kit block](https://api.slack.com/block-kit/building#getting_started).
See the [example](https://github.com/henryivesjones/slash-slack/blob/main/example.py) for example usage.

## Input Arg/Flag parsing

`sam-slash-slack` takes care of parsing command input into pre-defined args and flags which let you focus on writing the command function, and not wrangling the content into the format that you need.

## Auto-generated help

`sam-slash-slack` provides help dialog auto-generated from your commands, args, and flags. Additional details can be embedded directly into the command decorator and arg/flag initializers.

To request global help:

```
/slash-slack help
```

To request command specific help:

```
/slash-slack command --help
```

## Response visibility

Slack slash command responses can be made visible only to the requestor, or to the entire channel. `sam-slash-slack` adds the ability for any command to be made visible with the `--visible` flag.

## Response formatting

Slack expects responses to be in the Slack Block Kit format. `sam-slash-slack` will automatically convert your string responses into slack `mrkdown` blocks.

# Deployment

`sam-slash-slack` utilizes SAM to generate and deploy the underlying AWS infrastructure. To initialize a new `sam-slash-slack` app. Run `init-sam-slash-slack` in the project directory. This will bootstrap the SAM template, and `app.py` file.
You must expose the underlying `api_handler`, and `async_handler` methods from the `SAMSlashSlack` bot.

```bash
sam build
sam deploy

```

# Command Inputs

The inputs and parsing for each command is determined by the parameters to the function. `SAMSlashSlack` parses the function parameters and generates an input schema.

When a request is made to a given command, `SAMSlashSlack` attempts to parse the input text into the command input schema.

## Flags

Flags are boolean options that can be added to commands anywhere within the request. During the input parsing, flags are parsed and removed, and then args are parsed.

There is no difference in doing `/slash-slack command arg --flag` and `/slash-slack command --flag arg`.

### Global Flags

There are 2 global flags: `--visible` and `--help`.

The `--visible` flag will make the response visible in the channel that the request was made. By default, responses are only visible to the user which made the request.

The `--help` flag will indicate that the `SlashSlack` app should return the relevant help message. Whether that is app level `/slash-slack --help`, or command level `/slash-slack command --help`.

## Args

All non-flag arguments to the command function make up the input schema for the command function. This means that the # of words in the command request must match up with the # of non-flag arguments. (With two exceptions: String, UnknownLengthList).

### String

When the only non-flag parameter for the function is a `String()` then the entire argument body (with flags removed) will be passed into that parameter.

```python
# EX: /slash-slack echo hello --upper world
# Response: HELLO WORLD
@slash.command("echo")
def echo(s: str, upper: bool = Flag()):
    return s
```

### Unknown Length List

To collect an arbitrary # of args from the user use the `UnknownLengthList` arg type. This arg type will be passed a list of all of the values passed to it parsed into the given type.

Because this consumes args till the end of the arg list, this must be the last non-flag param for the command function.

```python
# EX: /slash-slack avg 10, 20, 30
# Response: 20.0
@slash.command("avg")
def avg(numbers = UnknownLengthList(arg_type=Float())):
    return sum(numbers) / len(numbers)
```

### SlashSlackRequest

If you want to have access to the complete request as sent from the slack servers. Add a param with the type annotation of `SlashSlackRequest` to the command function.

```python
# EX: /slash-slack echo hello world
# Response: hello world This request was made by John Doe
@slash.command("echo")
def echo(content: str, slash_slack_request: SlashSlackRequest):
    return f"{content} This request was made by {slash_slack_request.user_name}"

```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/henryivesjones/sam-slash-slack",
    "name": "sam-slash-slack",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "slack,lambda,bot,slash command,aws,sam",
    "author": "",
    "author_email": "Henry Jones <henryivesjones@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/62/59/86e7cf03a49ad51012c12b1b7ecaba0cf20e921be718ef1b374297d63287/sam-slash-slack-0.1.2.tar.gz",
    "platform": null,
    "description": "# sam-slash-slack\n\n![Tests + Linting](https://github.com/henryivesjones/sam-slash-slack/actions/workflows/checks.yml/badge.svg?branch=main&event=push)\n![pypi](https://img.shields.io/pypi/v/sam-slash-slack)\n![License](https://img.shields.io/pypi/l/sam-slash-slack)\n![Downloads](https://img.shields.io/pypi/dm/sam-slash-slack)\n\nA python framework for building slack slash bots using AWS SAM. Get input parsing and validation, command routing, async responses, auto-generated help dialog, response visibility, and response message formatting all for free.\n\n`sam-slash-slack` utilizes the AWS Serverless Application Model (SAM) to generate serverless resources to run your `sam-slash-slack` bot. The `sam-slash-slack` bot consists of two lambda functions and one SQS queue. The `api_handler` lambda function receives the POST request from slack enqueues the request in the SQS queue for async processing, and immediately responds. Slack requires a response within 3 seconds. This architecture enables your slash commands to take longer than that to respond. The `async_handler` receives messages from the `api_handler` and executes the given command.\n\nMost of the code for `sam-slash-slack` is shared from [`slash-slack`](https://github.com/henryivesjones/slash-slack).\n\nView [Slack slash command documentation](https://api.slack.com/interactivity/slash-commands) here.\n\n```python\n# EX: /slash-slack math 10 * 10\n# Response: 100.0\nimport os\n\nfrom sam_slash_slack import Flag, Float, SAMSlashSlack, String\n\nslash = SAMSlashSlack(signing_secret=os.environ['SLACK_SIGNING_SECRET'])\napi_handler = slash.get_api_handler()\nasync_handler = slash.get_async_handler()\n\n\n@slash.command(\"math\")\ndef math_fn(\n    x: float = Float(),\n    symbol: str = Enum(values={\"*\", \"+\", \"-\", \"/\"}),\n    y: float = Float(),\n):\n    if symbol == \"*\":\n        return x * y\n    if symbol == \"+\":\n        return x + y\n    if symbol == \"-\":\n        return x - y\n    if symbol == \"/\":\n        return x / y\n```\n\n# Why use `sam-slash-slack`?\n\nBuilding a slack slash bot can seem very straightforward at first, however there are some complexities that make it difficult. `sam-slash-slack` handles all of the complexities for you letting you focus on the bot response handlers.\n\nYou don't have to worry about deployment, as you can let SAM and lambda functions do the heavy lifting.\n\n## Webhook signature verification\n\n`slash-slack` will verify that incoming requests were made by slack by validating the request signature. To disable signature verification use the `dev=True` option when creating the `SAMSlashSlack` object.\n\n## Command Response Timeout/Async responses\n\nSlack requires that the slash bot webhook be responded to within 3 seconds.\n\nOften times the action being taken by the bot will depend on external services which might not respond within 3 seconds.\n\n`sam-slash-slack` sends an immediate `200` response to the webhook request, and runs the command function asynchronously. When the command function finishes, the response is sent back to slack using the `response_url` from the request.\n\nYou can optionally add content to the immediate response to let your user know that something is being\ndone in the background. A global/default response can be set with the `acknowledge_response` parameter on\nthe `SAMSlashSlack` class, or at the command level with the `acknowledge_response` parameter on the `command` decorator.\nThe value passed to `acknowledge_response` will be passed to `blocks._make_block_message` and can be a `str`, `block`, `list[str]`, or `list[block]`\nwhere a `block` is a [block kit block](https://api.slack.com/block-kit/building#getting_started).\nSee the [example](https://github.com/henryivesjones/slash-slack/blob/main/example.py) for example usage.\n\n## Input Arg/Flag parsing\n\n`sam-slash-slack` takes care of parsing command input into pre-defined args and flags which let you focus on writing the command function, and not wrangling the content into the format that you need.\n\n## Auto-generated help\n\n`sam-slash-slack` provides help dialog auto-generated from your commands, args, and flags. Additional details can be embedded directly into the command decorator and arg/flag initializers.\n\nTo request global help:\n\n```\n/slash-slack help\n```\n\nTo request command specific help:\n\n```\n/slash-slack command --help\n```\n\n## Response visibility\n\nSlack slash command responses can be made visible only to the requestor, or to the entire channel. `sam-slash-slack` adds the ability for any command to be made visible with the `--visible` flag.\n\n## Response formatting\n\nSlack expects responses to be in the Slack Block Kit format. `sam-slash-slack` will automatically convert your string responses into slack `mrkdown` blocks.\n\n# Deployment\n\n`sam-slash-slack` utilizes SAM to generate and deploy the underlying AWS infrastructure. To initialize a new `sam-slash-slack` app. Run `init-sam-slash-slack` in the project directory. This will bootstrap the SAM template, and `app.py` file.\nYou must expose the underlying `api_handler`, and `async_handler` methods from the `SAMSlashSlack` bot.\n\n```bash\nsam build\nsam deploy\n\n```\n\n# Command Inputs\n\nThe inputs and parsing for each command is determined by the parameters to the function. `SAMSlashSlack` parses the function parameters and generates an input schema.\n\nWhen a request is made to a given command, `SAMSlashSlack` attempts to parse the input text into the command input schema.\n\n## Flags\n\nFlags are boolean options that can be added to commands anywhere within the request. During the input parsing, flags are parsed and removed, and then args are parsed.\n\nThere is no difference in doing `/slash-slack command arg --flag` and `/slash-slack command --flag arg`.\n\n### Global Flags\n\nThere are 2 global flags: `--visible` and `--help`.\n\nThe `--visible` flag will make the response visible in the channel that the request was made. By default, responses are only visible to the user which made the request.\n\nThe `--help` flag will indicate that the `SlashSlack` app should return the relevant help message. Whether that is app level `/slash-slack --help`, or command level `/slash-slack command --help`.\n\n## Args\n\nAll non-flag arguments to the command function make up the input schema for the command function. This means that the # of words in the command request must match up with the # of non-flag arguments. (With two exceptions: String, UnknownLengthList).\n\n### String\n\nWhen the only non-flag parameter for the function is a `String()` then the entire argument body (with flags removed) will be passed into that parameter.\n\n```python\n# EX: /slash-slack echo hello --upper world\n# Response: HELLO WORLD\n@slash.command(\"echo\")\ndef echo(s: str, upper: bool = Flag()):\n    return s\n```\n\n### Unknown Length List\n\nTo collect an arbitrary # of args from the user use the `UnknownLengthList` arg type. This arg type will be passed a list of all of the values passed to it parsed into the given type.\n\nBecause this consumes args till the end of the arg list, this must be the last non-flag param for the command function.\n\n```python\n# EX: /slash-slack avg 10, 20, 30\n# Response: 20.0\n@slash.command(\"avg\")\ndef avg(numbers = UnknownLengthList(arg_type=Float())):\n    return sum(numbers) / len(numbers)\n```\n\n### SlashSlackRequest\n\nIf you want to have access to the complete request as sent from the slack servers. Add a param with the type annotation of `SlashSlackRequest` to the command function.\n\n```python\n# EX: /slash-slack echo hello world\n# Response: hello world This request was made by John Doe\n@slash.command(\"echo\")\ndef echo(content: str, slash_slack_request: SlashSlackRequest):\n    return f\"{content} This request was made by {slash_slack_request.user_name}\"\n\n```\n",
    "bugtrack_url": null,
    "license": "GPL-3.0-or-later",
    "summary": "A SAM python framework for slack slash bots.",
    "version": "0.1.2",
    "project_urls": {
        "Homepage": "https://github.com/henryivesjones/sam-slash-slack"
    },
    "split_keywords": [
        "slack",
        "lambda",
        "bot",
        "slash command",
        "aws",
        "sam"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a7198c054eaa006e450cf01ada8d46620ba7363e31692a636bf2a6bb32d4930b",
                "md5": "285808083a55d06802fd313ec56367a9",
                "sha256": "56c828d9b12d6927ddfbd51ac23e91019a67745c6a0f55b35764219bfb764e2e"
            },
            "downloads": -1,
            "filename": "sam_slash_slack-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "285808083a55d06802fd313ec56367a9",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 28673,
            "upload_time": "2023-08-24T20:33:25",
            "upload_time_iso_8601": "2023-08-24T20:33:25.644313Z",
            "url": "https://files.pythonhosted.org/packages/a7/19/8c054eaa006e450cf01ada8d46620ba7363e31692a636bf2a6bb32d4930b/sam_slash_slack-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "625986e7cf03a49ad51012c12b1b7ecaba0cf20e921be718ef1b374297d63287",
                "md5": "0ea9a9c637250bce780a1bffd52a781c",
                "sha256": "aaf147b14392f42d5d71b5b493f8db3e9b2e1142fbc6fc54d1cedf248d33ccd8"
            },
            "downloads": -1,
            "filename": "sam-slash-slack-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "0ea9a9c637250bce780a1bffd52a781c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 31228,
            "upload_time": "2023-08-24T20:33:27",
            "upload_time_iso_8601": "2023-08-24T20:33:27.135467Z",
            "url": "https://files.pythonhosted.org/packages/62/59/86e7cf03a49ad51012c12b1b7ecaba0cf20e921be718ef1b374297d63287/sam-slash-slack-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-24 20:33:27",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "henryivesjones",
    "github_project": "sam-slash-slack",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "sam-slash-slack"
}
        
Elapsed time: 0.54831s