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.

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.1008.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slack_serverless-2024.1008-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: slack_serverless-2024.1008.tar.gz
  • Upload date:
  • Size: 8.9 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.1008.tar.gz
Algorithm Hash digest
SHA256 11342e54ba807664e6397278054e0303e4ae26173ddfc8f796748c0d93aa36ba
MD5 b160e493dcdb5a45566e3351690bace1
BLAKE2b-256 877214671b76950ffd2c5e054ab8f403c7d4bc64b933ea65a127f740d9923b60

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for slack_serverless-2024.1008-py3-none-any.whl
Algorithm Hash digest
SHA256 2d6fd95198ea079528861c93add753394fb5e0039f465e688fe8d5ee9a2e242d
MD5 26bf74bf6ef550593e02de5bdb525fc9
BLAKE2b-256 44f9a9e17caf0be7db7177b2b1b2336f797d512dcab3069e4470c994d0ec2aa3

See more details on using hashes here.

Supported by

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