Skip to main content

Simplified serverless Slack bot interactions

Project description

Utility library for serverless Slack apps

PyPI

This is a (currently very simple) library that eases development of serverless Slack app and bots.

Currently, it targets Google Cloud Platform (but is easily generalised), and provides a decorator and some utility functions that make Slack signature verification automatic and help with building responses in a very lightweight fashion.

I will grow it with things I end up needing in my work, but if you have different needs and want to extend the library to support them, then PRs would be gratefully received!

MVP Example

This will accept any slash command you've configured for your bot, and just greet the user who calls it, tagging them in the reply.

from slack_serverless import slack_slash_command_gcp
from slack_messaging import slack_in_channel_text_response, slack_tags

@slack_slash_command_gcp(slack_signing_key=YOUR_SIGNING_KEY)
def command_handler(payload):
    return slack_in_channel_text_response(f"Hi there <@{slack_tags([payload['user_id']])}>")

Message Deferral

Note to avoid dependency conflicts, this library does not depend on the cloud provider APIs that are needed. For GCP, be sure to add google-cloud-pubsub>=2.18.1 to your requirements.txt (or whatever) if you're using deferral.

If you need to take more than three seconds to reply to a Slack message, the usual thing to do in a serverless environment is hand off the message to another function, and have the first one acknowledge receipt back to Slack within three seconds. This is required - failing to respond in a timely fashion will result in the user seeing an error message.

This library provides a comfort wrapper around this use case - all you need to do is set up a PubSub topic (or your cloud's equivalent, though only GCP is supported as yet) and defer the message in your main Slack handler with the slack_defer function.

You then have a second lambda, triggered by the pubsub (Eventarc or equivalent) in which the main function is decorated with @slack_deferred_slash_handler_gcp (despite the name, it supports both events and slash commands - it'll be changing soon) and handles the event.

This will take care of wrapping and unwrapping the event appropriately and generally trades of some ease of use for a little flexibility. If it doesn't meet your needs you can of course just ignore it and code up the functionality yourself.

AWS

AWS is supported (at least, Lambdas with API Gateway proxy triggers are), and it's much the same as for GCP - just use the slack_slash_command_aws_api_gateway_proxy and slack_event_webhook_aws_api_gateway_proxy decorators instead of the GCP ones, and you should be good to go.

Other Cloud Providers

Things will be a bit more manual here (but I'll happily add comfort wrappers if there's enough call and a PR that contributes them 😉).

The easiest thing to do is use the slack_slash_command decorator, which is similar to the gcp one above but needs you pass in a couple of Callables the code will use to get access to the data in a provider-specific way.

Take a look at the code in slack_serverless.py for examples to get you started.

Developing

Don't forget to set up pre-commit if you're developing things, especially if you plan to push a PR.

Legal Mumbo-Jumbo

Copyright (c)2023 Ross Bamford (and contributors)

License: MIT (see LICENSE.md for details).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slack_serverless-2024.1010.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

slack_serverless-2024.1010-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file slack_serverless-2024.1010.tar.gz.

File metadata

  • Download URL: slack_serverless-2024.1010.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.13

File hashes

Hashes for slack_serverless-2024.1010.tar.gz
Algorithm Hash digest
SHA256 293139972ff122680400d28ab1fdab9385f12612587c52ad0dbcb37c8266a7bf
MD5 96c26264130cf9ad01bd4e7072cb4310
BLAKE2b-256 b0890c69f7969de619c43569cb006be088c387a54208e7f28fd2cec9ad877ed6

See more details on using hashes here.

File details

Details for the file slack_serverless-2024.1010-py3-none-any.whl.

File metadata

File hashes

Hashes for slack_serverless-2024.1010-py3-none-any.whl
Algorithm Hash digest
SHA256 f4984ce315dd5d1115243bbb6420707603a89b151d49d94c77a435e78c140102
MD5 3c4691f4abb0325c574453aa464f3f9f
BLAKE2b-256 3c4b4c390289fd7f42b7e41f12b4e8096f5cfa638149255390f663f60f9d2979

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page