slack
#
Description#
Plugin used to interact with Slack instances.
You’ll have to generate your own Slack app and tokens in order to use this plugin. Steps:
Create a new Slack app here <https://api.slack.com/apps> and associate a Slack workspace to it.
In the configuration panel of your app scroll to Socket Mode and select Enable Socket Mode.
Scroll to Event Subscriptions and select Enable Events.
Choose the type of events that you want to subscribe to. You can select bot events (i.e. when somebody in the channel mentions the name of your app) or any of the workspace events (e.g. creation of messages, user events etc.).
Scroll to App-Level Tokens and generate a new token with
connections:write
scope. This token will be used to receive Slack events over websocket.Scroll to OAuth & Permissions and select the scopes that you want to enable. You may usually want to enable Bot Token Scopes ->
app_mentions:read
, so the script can react when somebody mentions its name. You may also want to select the user scopes relevant to your application - e.g. read/write messages, manage users etc.If you changed scopes and settings, you may have to reinstall the app in the workspace through the Install App menu.
Navigate to the Install App menu. If the app has been correctly installed in your workspace then you should see a Bot User OAuth Token, used to authenticate API calls performed as the app/bot. If you also granted user permissions to the app then you should also see a User OAuth Token on the page.
Configuration#
slack:
# [Required]
# Your Slack app token.
app_token: # type=str
# [Required]
# Bot OAuth token reported on the *Install App* menu.
bot_token: # type=str
# [Optional]
# User OAuth token reported on the *Install App* menu.
# user_token: # type=Optional[str]
# [Optional]
# How often the `RunnablePlugin.loop <https://docs.platypush.tech/platypush/plugins/.html#platypush.plugins.RunnablePlugin.loop>`_ function should be
# executed (default: 15 seconds). *NOTE*: For back-compatibility
# reasons, the `poll_seconds` argument is also supported, but it's
# deprecated.
# poll_interval: 15 # type=Optional[float]
# [Optional]
# How long we should wait for any running
# threads/processes to stop before exiting (default: 5 seconds).
# stop_timeout: 5 # type=Optional[float]
# [Optional]
# If set to True then the plugin will not monitor
# for new events. This is useful if you want to run a plugin in
# stateless mode and only leverage its actions, without triggering any
# events. Defaults to False.
# disable_monitor: False # type=bool
Triggered events#
Actions#
Module reference#
- class platypush.plugins.slack.SlackPlugin(app_token: str, bot_token: str, user_token: str | None = None, **kwargs)[source]#
Bases:
ChatPlugin
,RunnablePlugin
Plugin used to interact with Slack instances.
You’ll have to generate your own Slack app and tokens in order to use this plugin. Steps:
Create a new Slack app here <https://api.slack.com/apps> and associate a Slack workspace to it.
In the configuration panel of your app scroll to Socket Mode and select Enable Socket Mode.
Scroll to Event Subscriptions and select Enable Events.
Choose the type of events that you want to subscribe to. You can select bot events (i.e. when somebody in the channel mentions the name of your app) or any of the workspace events (e.g. creation of messages, user events etc.).
Scroll to App-Level Tokens and generate a new token with
connections:write
scope. This token will be used to receive Slack events over websocket.Scroll to OAuth & Permissions and select the scopes that you want to enable. You may usually want to enable Bot Token Scopes ->
app_mentions:read
, so the script can react when somebody mentions its name. You may also want to select the user scopes relevant to your application - e.g. read/write messages, manage users etc.If you changed scopes and settings, you may have to reinstall the app in the workspace through the Install App menu.
Navigate to the Install App menu. If the app has been correctly installed in your workspace then you should see a Bot User OAuth Token, used to authenticate API calls performed as the app/bot. If you also granted user permissions to the app then you should also see a User OAuth Token on the page.
- __init__(app_token: str, bot_token: str, user_token: str | None = None, **kwargs)[source]#
- Parameters:
app_token – Your Slack app token.
bot_token – Bot OAuth token reported on the Install App menu.
user_token – User OAuth token reported on the Install App menu.
- send_message(channel: str, as_user: bool = False, text: str | None = None, blocks: Iterable[str] | None = None, **kwargs)[source]#
Send a message to a channel. It requires a token with
chat:write
bot/user scope.- Parameters:
channel – Channel ID or name.
as_user – If true then the message will be sent as the authorized user, otherwise as the application bot (default: false).
text – Text to be sent.
blocks – Extra blocks to be added to the message (e.g. images, links, markdown). See Slack documentation for blocks.
- start()#
Start the plugin.
- wait_stop(timeout=None)#
Wait until a stop event is received.