Slackbot

Small demo to show how to post a message to the OpenGrid Slack Channel

Imports and config


In [ ]:
from opengrid.library.slack import Slack
from opengrid import config

Get URL from config file


In [ ]:
config = config.Config()
slack_url = config.get('Slack', 'webhook')

Create Slackbot

These settings are all optional


In [ ]:
username = 'DemoBot'
channel = "junk" # we don't want to clutter up everything
emoji = ':jack_o_lantern:'

Create the actual communication object


In [ ]:
slack = Slack(url=slack_url, username=username, channel=channel, emoji=emoji)

Post simple string messages

Now create a message and post it


In [ ]:
message = "Hello from the demo notebook"

In [ ]:
slack.post_text(message)

You can also post multiple strings, which will be printed on separate lines


In [ ]:
slack.post_text('Hello', 'this', 'is', 'a', 'multiline', 'post')

Post complex JSONs


In [ ]:
json_message = {
    "text": "This is the title text of the message",
    "attachments": [
        {
            "title": "This is a title",
            "text": """This is the main text block of the attachment. It will collapse the text if it is longer than 700 characters or 5 lines
            
            Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
            Aenean commodo ligula eget dolor. Aenean massa.
            Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
            Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem.
            Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.
            In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.
            Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi.
            Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim.
            Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet.
            Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi.
            Nam eget dui.
            """,
            "fallback": "You need this little text, it is displayed in the pop-up messages and notifications",
            "color": "danger", # this will create a red line
            "fields": [
                {
                    "title": "Field 1",
                    "value": "This is a short field",
                    "short": True
                },
                {
                    "title": "Field 2",
                    "value": "This is another short field",
                    "short": True
                },
                {
                    "title": "Field 3",
                    "value": "This is a long field, it will take the full width of the screen",
                    "short": False
                }
            ]
        }
    ]
}

In [ ]:
slack.post_json(json_message)

Post a message with buttons


In [ ]:
button_message = {
    "text": "This is a test message with buttons.",
    "attachments": [
        {
            "text": "Do you want to press a button?",
            "fallback": "There are no buttons to press",
            "callback_id": "button_test",
            "color": "#3AA3E3",
            "attachment_type": "default",
            "actions": [
                {
                    "name": "neutral",
                    "text": "Neutral button",
                    "type": "button",
                    "value": "neutral"
                },
                {
                    "name": "danger",
                    "text": "Dangerous button",
                    "style": "danger",
                    "type": "button",
                    "value": "danger",
                    "confirm": {
                        "title": "Are you sure?",
                        "text": "Really?",
                        "ok_text": "Yes",
                        "dismiss_text": "No"
                    }
                }
            ]
        }
    ]
}

In [ ]:
slack.post_json(button_message)

In [ ]: