Skip to content

Slack Templates (Raw & Blocks API JSON) #1336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

operatorequals
Copy link

Description:

Related issue (if applicable): #641

This PR adds the following functionality to the Slack plugin
(Similar to MSTeams implementation)

  • template key to all Slack URLs accepts a file (or any attachment) which is used to generate the sent message

  • Tokens prepended with : are dynamically added to the template (e.g: ...&template=file.txt&:token=value)

  • Full Support of the Block Kit API
    The templates can utilize the full power of the Blocks Kit API. If the AppRise URL contains blocks=yes, then the template file is parsed as JSON, and it is passed as-is. An example follows:
    template-blocks.json

{
  "blocks": [
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": "Hey there 👋: {{ app_desc }}."
      }
    },
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": "{{ app_body }}"
      }
    },
    {
      "type": "actions",
      "elements": [
        {
          "type": "button",
          "text": {
            "type": "plain_text",
            "emoji": true,
            "text": "{{ app_id }}"
          },
          "style": "primary",
          "value": "click_me_123"
        }
      ]
    }
  ]
}
apprise_url = 'slack://x/x/x?blocks=true&template=template-blocks.json'
apprise_obj = apprise.Apprise()
apprise_obj.add(apprise_url)

apprise_obj.notify(body="body of the message", title="TITLE")

image

NOTE: As you can see the title is not rendered, as it was not used in the template. With great power comes great responsibility.
NOTE: footer and params that traditionally modify the message are ignored when template is used with blocks.
  • If the URL does not contain blocks param, then the template is free text, only sent as body of the notification. The title is processed as usually.

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • No lint errors (use flake8)
  • 100% test coverage

Testing

This PR's feature is testable by everyone!

We can use a Github Gist containing a template!

The Gist below contains the Blocks template used above:
https://gist.github.com/operatorequals/16864f51c215a0a9628f934b0740095a
We can use it by filling its Raw URL to the AppRise template, getting an AppRise URL as below:

slack://x/x/x?blocks=true&template=https://gist.githubusercontent.com/operatorequals/16864f51c215a0a9628f934b0740095a/raw/fd582669e3e35f9a9705946568989f41a3fe8166/slack-blocks-apprise-template.json
NOTE: fill the x to your Slack Incoming Webhook values

Anyone can help test this source code as follows:

# Create a virtual environment to work in as follows:
python3 -m venv apprise

# Change into our new directory
cd apprise

# Activate our virtual environment
source bin/activate

# Install the branch
pip install git+https://github.com/caronc/apprise.git@<this.branch-name>

# Test out the changes with the following command:
apprise -t "Test Title" -b "Test Message" \
  <apprise url related to ticket>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant