llimllib / limbo

A simple, clean, easy to modify Slack chatbot
MIT License
402 stars 162 forks source link

Messages with attachments #108

Open bsmoo opened 7 years ago

bsmoo commented 7 years ago

Is there anyway to send message attachments with buttons such as the below:

{
    "text": "Would you like to play a game?",
    "attachments": [
        {
            "text": "Choose a game to play",
            "fallback": "You are unable to choose a game",
            "callback_id": "wopr_game",
            "color": "#3AA3E3",
            "attachment_type": "default",
            "actions": [
                {
                    "name": "game",
                    "text": "Chess",
                    "type": "button",
                    "value": "chess"
                },
                {
                    "name": "game",
                    "text": "Falken's Maze",
                    "type": "button",
                    "value": "maze"
                },
                {
                    "name": "game",
                    "text": "Thermonuclear War",
                    "style": "danger",
                    "type": "button",
                    "value": "war",
                    "confirm": {
                        "title": "Are you sure?",
                        "text": "Wouldn't you prefer a good game of chess?",
                        "ok_text": "Yes",
                        "dismiss_text": "No"
                    }
                }
            ]
        }
    ]
}

And clicking the buttons can send data back to the bot which can be logged in a database or something?

llimllib commented 7 years ago

Yes! To send the message you will need to use the slack API instead of just returning a string; the github plugin does this, for example.

So you could do something like:

def on_message(msg, server):
    # here we'll assume that `handle_message` returns the dict above ^^^
    fancy_response = handle_message(msg)
    server.slack.post_message(
            msg['channel'],
            '',
            as_user=server.slack.username,
            **fancy_response)

I haven't actually dealt with handling the responses; I'd have to look at what happens when the user clicks on the buttons. I hope I'll get some free time to look at that soon, but if you dig in first please let me know!

bsmoo commented 7 years ago

Thanks @llimllib - likewise let me know if you come up with anything first. I'm just going to be deploying this to a new environment so need to get everything setup again.