Skip to content

Instantly share code, notes, and snippets.

@ab9-er
Last active November 13, 2017 09:21
Show Gist options
  • Save ab9-er/e47695429262f8274d1b0a9b19114db0 to your computer and use it in GitHub Desktop.
Save ab9-er/e47695429262f8274d1b0a9b19114db0 to your computer and use it in GitHub Desktop.
Method to send slack button attachments messages
from slackclient import SlackClient
BOT_TOKEN = "xoxb-12345678"
SLACK_CLIENT = SlackClient(BOT_TOKEN)
SLACK_CLIENT.rtm_connect()
def slacker_buttons(destination=None, as_user=None, text="", at_text=None, attachments=None, color="good"):
"""
destination: String identifier for receiver (ex: @slackbot/#general)
as_user: String identifier of the sender (ex: @slackbot)
text: The plain text message
at_text: Text message of the attachment
attachments: A list of attachment objects as per Slack API
color: Color of the sidebar of the attachment
"""
if not attachments:
attachments=[
{
"text": at_text,
"fallback": "Ping {}".format(destination),
"callback_id": "callback_button_1",
"color": color,
"attachment_type": "default",
"actions": [
{
"name": "btn1",
"text": "Hello",
"type": "button",
"value": True,
"style": "primary"
},
{
"name": "btn2",
"text": "Bye",
"type": "button",
"value": False,
"style": "danger"
}
]}]
SLACK_CLIENT.api_call("chat.postMessage",
channel=destination,
as_user=as_user,
text=str(text),
attachments=attachments)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment