Skip to content

Commit

Permalink
docs: basic documentation for async mode
Browse files Browse the repository at this point in the history
  • Loading branch information
DonDebonair committed Aug 13, 2022
1 parent 6323f5c commit 65605c8
Show file tree
Hide file tree
Showing 13 changed files with 300 additions and 81 deletions.
54 changes: 28 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@
[![image](https://img.shields.io/pypi/v/slack-machine.svg)](https://pypi.python.org/pypi/slack-machine)
[![image](https://img.shields.io/pypi/l/slack-machine.svg)](https://pypi.python.org/pypi/slack-machine)
[![image](https://img.shields.io/pypi/pyversions/slack-machine.svg)](https://pypi.python.org/pypi/slack-machine)
[![CI Status](https://github.com/DandyDev/slack-machine/actions/workflows/ci.yml/badge.svg)](https://github.com/DandyDev/slack-machine/actions/workflows/ci.yml)
[![image](https://codecov.io/gh/DandyDev/slack-machine/branch/master/graph/badge.svg)](https://codecov.io/gh/DandyDev/slack-machine)
[![CI Status](https://github.com/DonDebonair/slack-machine/actions/workflows/ci.yml/badge.svg)](https://github.com/DandyDev/slack-machine/actions/workflows/ci.yml)
[![image](https://codecov.io/gh/DandyDev/slack-machine/branch/main/graph/badge.svg)](https://codecov.io/gh/DandyDev/slack-machine)

Slack Machine is a sexy, simple, yet powerful and extendable Slack bot.
More than just a bot, Slack Machine is a framework that helps you
develop your Slack team into a ChatOps powerhouse.

![image](extra/logo.png)

## *Warning*
## *Note*

As of v0.24.0 Python 3.6 support has been dropped. Python 3.6 is EOL on 2021-12-23, which is 3
weeks from now at the time of writing (2021-12-1). I just switched to a new Macbook Pro M1 on which
I cannot build and test for Python 3.6 easily anymore, so I'm dropping support.
As of v0.26.0 Slack Machine supports AsyncIO using the
[Slack Events API](https://api.slack.com/apis/connections/events-api) and
[Socket Mode](https://api.slack.com/apis/connections/socket). This is still experimental and should be thoroughly
tested. The goal is to eventually stop supporting the old version that uses the Slack RTM API, as the Events API is
recommended by Slack for must use cases and asyncio has the potential to be much more performant.

This does not mean that Slack Machine won't work with Python 3.6 anymore. For now, there are no
specific features being used from Python 3.7 or higher.
I encourage everyone to start testing the async mode and report any issues in this repository.

## Features

Expand All @@ -30,24 +31,26 @@ specific features being used from Python 3.7 or higher.
- Support for rich interactions using the [Slack Web API](https://api.slack.com/web)
- High-level API for maximum convenience when building plugins
- Low-level API for maximum flexibility
- **(Experimental) Support for asyncio**

### Plugin API features:

- Listen and respond to any regular expression
- Capture parts of messages to use as variables in your functions
- Respond to messages in channels, groups and direct message conversations
- Respond with Emoji
- Respond with reactions
- Respond in threads
- Respond with ephemeral messages
- Send DMs to any user
- Support for [message attachments](https://api.slack.com/docs/message-attachments)
- Support for [blocks](https://api.slack.com/reference/block-kit/blocks)
- Listen and respond to any [Slack event](https://api.slack.com/events) supported by the RTM API
- Listen and respond to any [Slack event](https://api.slack.com/events) supported by the RTM API (or the Events API
with Socket Mode in the case of using async mode)
- Store and retrieve any kind of data in persistent storage (currently Redis and in-memory storage are supported)
- Schedule actions and messages
- Schedule actions and messages (note: currently not supported in async mode)
- Emit and listen for events
- Help texts for Plugins
- Built in web server for webhooks
- Built in web server for webhooks (note: currently not supported in async mode)

### Coming Soon

Expand All @@ -67,26 +70,25 @@ It is **strongly recommended** that you install `slack-machine` inside a

## Usage

1. Create a directory for your Slack Machine bot:
`mkdir my-slack-bot && cd my-slack-bot`
2. Add a `local_settings.py` file to your bot directory:
`touch local_settings.py`
3. Create a Bot User for your Slack team:
https://my.slack.com/services/new/bot (take note of your API
token)
4. Add the Slack API token to your `local_settings.py` like this:
1. Create a directory for your Slack Machine bot:
`mkdir my-slack-bot && cd my-slack-bot`
2. Add a `local_settings.py` file to your bot directory:
`touch local_settings.py`
3. Create a Bot User for your Slack team:
https://my.slack.com/services/new/bot (take note of your API
token)
4. Add the Slack API token to your `local_settings.py` like this:

``` python
SLACK_API_TOKEN = 'xox-my-slack-token'
```

5. Start the bot with `slack-machine`
6. ...
7. Profit!
5. Start the bot with `slack-machine`
6. ...
7. Profit!

## Documentation

You can find the documentation for Slack Machine here: https://dandydev.github.io/slack-machine/
You can find the documentation for Slack Machine here: https://dondebonair.github.io/slack-machine/

Go read it to learn how to properly configure Slack Machine, write
plugins, and more!
Go read it to learn how to properly configure Slack Machine, write plugins, and more!
46 changes: 46 additions & 0 deletions docs/api/api-async.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# API Documentation (Async)

This is the API documentation of **async versions** of all the classes and functions relevant for Plugin development.
The rest of the code deals with the internal workings of Slack Machine and is very much an implementation detail and
subject to change. Therefore it is not documented.

## Plugin classes

The following 2 classes form the basis for Plugin development.

### ::: machine.asyncio.plugins.base.MachineBasePlugin

------------------------------------------------------------------------

### ::: machine.asyncio.plugins.base.Message


## Decorators

These are the decorators you can use to have Slack Machine respond to
specific things (events, messages, etc.)

### ::: machine.asyncio.plugins.decorators

## Models

These classes represent base objects from the Slack API

### ::: machine.asyncio.models.user.User

### ::: machine.asyncio.models.channel.Channel

## Storage

Storage is exposed to plugins through the `self.storage` field. The
following class implements the interface plugins can use to interact
with the storage backend.

### ::: machine.asyncio.storage.PluginStorage

------------------------------------------------------------------------

New *Storage Backends* can be implemented by extending the following
class:

### ::: machine.asyncio.storage.backends.base.MachineBaseStorage
File renamed without changes.
55 changes: 55 additions & 0 deletions docs/async.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Async mode

As of v0.26.0 Slack Machine supports AsyncIO using the
[Slack Events API](https://api.slack.com/apis/connections/events-api) and
[Socket Mode](https://api.slack.com/apis/connections/socket). This is still experimental and should be thoroughly
tested. The goal is to eventually stop supporting the old version that uses the Slack RTM API, as the Events API is
recommended by Slack for must use cases and asyncio has the potential to be much more performant.

All the asyncio code is in the `machine.asyncio` package.

## How to start using it

With the advent of asyncio in Slack Machine, we're also moving towards using **Slack Apps** instead of legacy bot
tokens. This means that you'll need to create a new app in Slack and get an app token and a bot token for it:

1. Create a new app in Slack: <https://api.slack.com/apps>
2. Choose to create an app from an _App manifest_
3. Copy/paste the following manifest:

``` title="manifest.yaml"
--8<-- "docs/extra/manifest.yaml"
```
4. Add the Slack API token to your `local_settings.py` like this:
``` title="local_settings.py"
SLACK_APP_TOKEN = "xapp-my-app-token"
SLACK_BOT_TOKEN = "xoxb-my-bot-token"
```
5. Start the bot with `slack-machine-async`
6. ...
7. Profit!
## Choosing storage
Async mode uses different storage backend then the old sync mode. The following 2 backends are available:
- `machine.asyncio.storage.backends.memory.MemoryStorage`
- `machine.asyncio.storage.backends.memory.RedisStorage`
## Plugin API and usage
To support asyncio, the plugin API is slightly different. All user-defined plugin methods (the methods that are
marked with the plugin [decorators][decorators]) should now be defined as `async def`. And all
[builtin plugin][machine.asyncio.plugins.base.MachineBasePlugin] methods are async as well.
This also means that the builtin plugins have async versions:
- `machine.asyncio.plugins.builtin.general.HelloPlugin`,
- `machine.asyncio.plugins.builtin.general.PingPongPlugin`,
- `machine.asyncio.plugins.builtin.help.HelpPlugin`,
- `machine.asyncio.plugins.builtin.fun.images.ImageSearchPlugin`,
- `machine.asyncio.plugins.builtin.fun.memes.MemePlugin`,
- `machine.asyncio.plugins.builtin.admin.RBACPlugin`
76 changes: 76 additions & 0 deletions docs/extra/manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
display_information:
name: Slack Machine
features:
bot_user:
display_name: Slack Machine
always_online: false
oauth_config:
scopes:
bot:
- app_mentions:read
- channels:history
- channels:join
- channels:read
- chat:write
- chat:write.public
- emoji:read
- groups:history
- groups:read
- groups:write
- im:history
- im:read
- im:write
- mpim:history
- mpim:read
- mpim:write
- pins:read
- pins:write
- reactions:read
- reactions:write
- users:read
- users:read.email
- channels:manage
- chat:write.customize
- dnd:read
- files:read
- files:write
- links:read
- links:write
- metadata.message:read
- usergroups:read
- usergroups:write
- users.profile:read
- users:write
settings:
event_subscriptions:
bot_events:
- app_mention
- channel_archive
- channel_created
- channel_deleted
- channel_id_changed
- channel_left
- channel_rename
- channel_unarchive
- group_archive
- group_deleted
- group_left
- group_rename
- group_unarchive
- member_joined_channel
- member_left_channel
- message.channels
- message.groups
- message.im
- message.mpim
- reaction_added
- reaction_removed
- team_join
- user_change
- user_profile_changed
- user_status_changed
interactivity:
is_enabled: true
org_deploy_enabled: false
socket_mode_enabled: true
token_rotation_enabled: false
16 changes: 10 additions & 6 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

![image](img/logo.png)

[![Join the chat at https://gitter.im/slack-machine/lobby](https://badges.gitter.im/slack-machine/lobby.svg)](https://gitter.im/slack-machine/lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![image](https://img.shields.io/pypi/v/slack-machine.svg)](https://pypi.python.org/pypi/slack-machine)
[![image](https://img.shields.io/pypi/l/slack-machine.svg)](https://pypi.python.org/pypi/slack-machine)
[![image](https://img.shields.io/pypi/pyversions/slack-machine.svg)](https://pypi.python.org/pypi/slack-machine)
[![CI Status](https://github.com/DandyDev/slack-machine/actions/workflows/ci.yml/badge.svg)](https://github.com/DandyDev/slack-machine/actions/workflows/ci.yml)
[![image](https://codecov.io/gh/DandyDev/slack-machine/branch/master/graph/badge.svg)](https://codecov.io/gh/DandyDev/slack-machine)
[![CI Status](https://github.com/DonDebonair/slack-machine/actions/workflows/ci.yml/badge.svg)](https://github.com/DandyDev/slack-machine/actions/workflows/ci.yml)
[![image](https://codecov.io/gh/DandyDev/slack-machine/branch/main/graph/badge.svg)](https://codecov.io/gh/DandyDev/slack-machine)

Slack Machine is a sexy, simple, yet powerful and extendable Slack bot.
More than just a bot, Slack Machine is a framework that helps you
Expand All @@ -20,24 +21,27 @@ develop your Slack workspace into a ChatOps powerhouse.
- Support for rich interactions using the [Slack Web API](https://api.slack.com/web)
- High-level API for maximum convenience when building plugins
- Low-level API for maximum flexibility
- **(Experimental) Support for [asyncio][async-mode]**

### Plugin API features:

- Listen and respond to any regular expression
- Capture parts of messages to use as variables in your functions
- Respond to messages in channels, groups and direct message conversations
- Respond with Emoji
- Respond with reactions
- Respond in threads
- Respond with ephemeral messages
- Send DMs to any user
- Support for [message attachments](https://api.slack.com/docs/message-attachments)
- Support for [blocks](https://api.slack.com/reference/block-kit/blocks)
- Listen and respond to any [Slack event](https://api.slack.com/events) supported by the RTM API
- Listen and respond to any [Slack event](https://api.slack.com/events) supported by the RTM API (or the Events API
with Socket Mode in the case of using async mode)
- Store and retrieve any kind of data in persistent storage (currently Redis and in-memory storage are supported)
- Schedule actions and messages
- Schedule actions and messages (note: currently not supported in async mode)
- Emit and listen for events
- Help texts for Plugins
- Built in web server for webhooks
- Built in web server for webhooks (note: currently not supported in async mode)


### Coming Soon

Expand Down
2 changes: 1 addition & 1 deletion docs/plugins/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ more!

Being able to talk in Slack is only half the story for plugins. The
functions in your plugin have to be triggered somehow. Slack Machine
provides [Decorators][decorators] for that.
provides [Decorators](../../api#decorators) for that.
You can decorate the functions in your plugin class to tell them what
they should react to.

Expand Down
10 changes: 5 additions & 5 deletions docs/plugins/interacting.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ message: *@superbot greet me in the future*.
You cannot schedule a reaction to a message. It doesn't make sense to react to a message in the future.

For more information about scheduling message, have a look at the
[api documentation](../api.md).
[api documentation](../api/index.md).

## Protecting messages
## Protecting commands

Sometimes you may want to restrict certain commands in your bot, so they
can only be invoked by certain users.
Expand All @@ -196,7 +196,7 @@ PLUGINS = [
]
```

You can get the *member ID* from the slack profile, by clicking *more*
You can get the *member ID* from a user by going to their Slack profile, clicking *more*
and selecting *Copy member ID*.

If you wish to share the powers of root you can enable the RBAC admin
Expand All @@ -206,8 +206,8 @@ The RBAC plugin provides you with three new commands that lets you
lookup, grant and revoke roles to users:

- *@superbot who has role admin*
- *@superbot grant role admin to @trusted_user*
- *@superbot revoke role admin from @trusted_user*.
- *@superbot grant role admin to @trusted-user*
- *@superbot revoke role admin from @trusted-user*.

Now you can decorate certain functions in your plugin with the
[`@require_any_role`][machine.plugins.decorators.require_any_role] or
Expand Down
2 changes: 1 addition & 1 deletion docs/user/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ These are non-goals for me at the moment, but that might change in the
future:

- Support for other backends than Slack
- Build-in AI. At least not in the core, although whatever
- Built-in AI. At least not in the core, although whatever
intelligence developers put into their plugins, is cool of course

## Why Slack Machine was built
Expand Down
4 changes: 3 additions & 1 deletion docs/user/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ is easy:
token)
4. Add the Slack API token to your `local_settings.py` like this:

SLACK_API_TOKEN = 'xox-my-slack-token'
``` title="local_settings.py"
SLACK_API_TOKEN = 'xox-my-slack-token'
```
5. Start the bot with `slack-machine`
6. ...
Expand Down
Loading

0 comments on commit 65605c8

Please sign in to comment.