关于slack服务器发送消息的demo

base

  • 使用ngrok内网穿透
./ngrok http 5000
关于slack服务器发送消息的demo_第1张图片
ngrok
  • slack api 配置


    关于slack服务器发送消息的demo_第2张图片
    slack app 配置
# -*- coding: utf-8 -*-
from flask import Flask, request, make_response, Response
import json
from slackclient import SlackClient

SLACK_BOT_TOKEN = '你的bot token值'
slack_client = SlackClient(SLACK_BOT_TOKEN)
app = Flask(__name__)

DATA = {'CHOICES1': None, 'CHOICES2': None, 'CHOICES3': None}


@app.route("/slack/message_actions", methods=["POST"])
def message_actions():
    form_json = json.loads(request.form["payload"])
    callback_id = form_json.get(u'callback_id')

    if callback_id == u'data_save':
        attachments_json = [
            {
                "color": "#08b085",
                "text": "",
                "fields": [
                    {
                        "title": u"姓名:",
                        "value": 'zexin.zhang',
                        "short": True
                    },
                    {
                        "title": u"请假起止日期:",
                        "value": '2018-5-1 -- 2018-5-3',
                        "short": True
                    }
                ],
            },
        ]
        slack_client.api_call(
            "chat.update",
            channel=form_json['channel']['id'],
            ts=form_json["original_message"]['ts'],
            text='已确认',
            attachments=attachments_json
        )
        return make_response("", 200)

    if callback_id == u'data_delete':
        attachments_json = [
            {
                "fallback": "Upgrade your Slack client to use messages like these.",
                "color": "#3AA3E3",
                "attachment_type": "danger",
                "callback_id": "menu_options_1",
                "actions": [
                    {
                        "name": "bev_list",
                        "text": "请选择...",
                        "type": "select",
                        "options": [
                            {
                                "text": "Division",
                                "value": "division"
                            },
                            {
                                "text": "Department",
                                "value": "department"
                            },
                        ],
                        "selected_options": [
                            {
                                "text": "Division",
                                "value": "division"
                            }
                        ],
                    }
                ],

            },
            {
                "fallback": "Upgrade your Slack client to use messages like these.",
                "color": "#3AA3E3",
                "attachment_type": "danger",
                "callback_id": "menu_options_2",
                "actions": [
                    {
                        "name": "bev_list",
                        "text": "请选择...",
                        "type": "select",
                        "options": [
                            {
                                "text": "084",
                                "value": "084"
                            },
                            {
                                "text": "085",
                                "value": "085"
                            },
                        ],
                    }
                ]
            },
            {
                "text": "",
                "callback_id": "data_save",
                "color": "#3AA3E3",
                "attachment_type": "default",
                "actions": [
                    {
                        "name": "offer",
                        "text": u'确认',
                        "type": 'button',
                        "value": 'offer',
                        "style": 'primary'
                    },
                    {
                        "name": "interview",
                        "text": u'取消',
                        "type": 'button',
                        "value": 'interview',
                        "style": 'danger'
                    },
                ]
            }
        ]
        slack_client.api_call(
            "chat.update",
            channel=form_json['channel']['id'],
            ts=form_json["original_message"]['ts'],
            text='已选择department',
            attachments=attachments_json
        )
        return make_response("", 200)
    selection = form_json["actions"][0].get("selected_options")[0].get("value")
    print(form_json)
    if callback_id == u'menu_options_1':
        if selection == 'division':
            attachments_json = [
                {
                    "fallback": "Choice 1",
                    "color": "#3AA3E3",
                    "attachment_type": "danger",
                    "callback_id": "menu_options_1",
                    "actions": [
                        {
                            "name": "bev_list",
                            "text": "请选择...",
                            "type": "select",
                            "options": [
                                {"text": "Division", "value": "division"},
                                {"text": "Department", "value": "department"},
                            ],
                            "selected_options": [{"text": "Division", "value": "division"}], },
                    ],

                },
                {
                    "fallback": "Upgrade your Slack client to use messages like these.",
                    "color": "#3AA3E3",
                    "attachment_type": "danger",
                    "callback_id": "menu_options_2",
                    "actions": [
                        {
                            "name": "bev_list",
                            "text": "请选择...",
                            "type": "select",
                            "options": [
                                {"text": "084", "value": "084"},
                                {"text": "085", "value": "085"},
                            ],
                        }
                    ]
                },
                {
                    "text": "",
                    "callback_id": "data_save",
                    "color": "#3AA3E3",
                    "attachment_type": "default",
                    "actions": [
                        {
                            "name": "offer",
                            "text": u'确认',
                            "type": 'button',
                            "value": 'offer',
                            "style": 'primary'
                        },
                        {
                            "name": "interview",
                            "text": u'取消',
                            "type": 'button',
                            "value": 'interview',
                            "style": 'danger'
                        },
                    ]
                }

            ]
            slack_client.api_call(
                "chat.update",
                channel=form_json['channel']['id'],
                ts=form_json["original_message"]['ts'],
                text='已选择division',
                attachments=attachments_json
            )
        else:
            attachments_json = [
                {
                    "fallback": "Upgrade your Slack client to use messages like these.",
                    "color": "#3AA3E3",
                    "attachment_type": "danger",
                    "callback_id": "menu_options_1",
                    "actions": [
                        {
                            "name": "bev_list",
                            "text": "请选择...",
                            "type": "select",

                            "options": [
                                {
                                    "text": "Division",
                                    "value": "division"
                                },
                                {
                                    "text": "Department",
                                    "value": "department"
                                },
                            ],
                            "selected_options": [
                                {
                                    "text": "Department",
                                    "value": "department"
                                }
                            ],
                        }
                    ]
                },
                {
                    "fallback": "Upgrade your Slack client to use messages like these.",
                    "color": "#3AA3E3",
                    "attachment_type": "danger",
                    "callback_id": "menu_options_2",
                    "actions": [
                        {
                            "name": "bev_list",
                            "text": "请选择...",
                            "type": "select",
                            "options": [
                                {
                                    "text": "D1",
                                    "value": "D1"
                                },
                                {
                                    "text": "D2",
                                    "value": "D2"
                                },
                            ],
                        }
                    ]
                },
                {
                    "text": "",
                    "callback_id": "data_save",
                    "color": "#3AA3E3",
                    "attachment_type": "default",
                    "actions": [
                        {
                            "name": "offer",
                            "text": u'确认',
                            "type": 'button',
                            "value": 'offer',
                            "style": 'primary'
                        },
                        {
                            "name": "interview",
                            "text": u'取消',
                            "type": 'button',
                            "value": 'interview',
                            "style": 'danger'
                        },
                    ]
                }
            ]
            slack_client.api_call(
                "chat.update",
                channel=form_json['channel']['id'],
                ts=form_json["original_message"]['ts'],
                text='已选择department',
                attachments=attachments_json
            )
    if callback_id == u'menu_options_2':
        attachments_json = [
            {
                "fallback": "Upgrade your Slack client to use messages like these.",
                "color": "#3AA3E3",
                "attachment_type": "danger",
                "callback_id": "menu_options_1",
                "actions": [
                    {
                        "name": "bev_list",
                        "text": "请选择...",
                        "type": "select",
                        "options": [
                            {
                                "text": "Division",
                                "value": "division"
                            },
                            {
                                "text": "Department",
                                "value": "department"
                            },
                        ],
                        "selected_options": [
                            {
                                "text": "Division",
                                "value": "division"
                            }
                        ],
                    }
                ],

            },
            {
                "text": "",
                "callback_id": "data_delete",
                "color": "#3AA3E3",
                "attachment_type": "default",
                "actions": [
                    {
                        "name": "offer",
                        "text": selection,
                        "type": 'button',
                        "value": 'offer',
                        "style": 'primary'
                    },
                ]
            },
            {
                "fallback": "Upgrade your Slack client to use messages like these.",
                "color": "#3AA3E3",
                "attachment_type": "danger",
                "callback_id": "menu_options_2",
                "actions": [
                    {
                        "name": "bev_list",
                        "text": "请选择...",
                        "type": "select",
                        "options": [
                            {
                                "text": "084",
                                "value": "084"
                            },
                            {
                                "text": "085",
                                "value": "085"
                            },
                        ],
                    }
                ]
            },
            {
                "text": "",
                "callback_id": "data_save",
                "color": "#3AA3E3",
                "attachment_type": "default",
                "actions": [
                    {
                        "name": "offer",
                        "text": u'确认',
                        "type": 'button',
                        "value": 'offer',
                        "style": 'primary'
                    },
                    {
                        "name": "interview",
                        "text": u'取消',
                        "type": 'button',
                        "value": 'interview',
                        "style": 'danger'
                    },
                ]
            }
        ]
        slack_client.api_call(
            "chat.update",
            channel=form_json['channel']['id'],
            ts=form_json["original_message"]['ts'],
            text='已选择department',
            attachments=attachments_json
        )

    return make_response("", 200)


attachments_json = [
    {
        "fallback": "Upgrade your Slack client to use messages like these.",
        "color": "#3AA3E3",
        "attachment_type": "danger",
        "callback_id": "menu_options_1",
        "actions": [
            {
                "name": "bev_list",
                "text": "请选择...",
                "type": "select",
                "options": [
                    {
                        "text": "Division",
                        "value": "division"
                    },
                    {
                        "text": "Department",
                        "value": "department"
                    },
                ],
            }
        ]
    },
    {
        "fallback": "Upgrade your Slack client to use messages like these.",
        "color": "#3AA3E3",
        "attachment_type": "danger",
        "callback_id": "menu_options_2",
        "actions": [
            {
                "name": "bev_list",
                "text": "请选择...",
                "type": "select",
                "options": [
                ],
            }
        ]
    },
    {
        "text": "",
        "callback_id": "data_save",
        "color": "#3AA3E3",
        "attachment_type": "default",
        "actions": [
            {
                "name": "offer",
                "text": u'确认',
                "type": 'button',
                "value": 'offer',
                "style": 'primary'
            },
            {
                "name": "interview",
                "text": u'取消',
                "type": 'button',
                "value": 'interview',
                "style": 'danger'
            },
        ]
    }
]
slack_client.api_call(
    "chat.postMessage",
    channel="UA32KCGE6",
    text="请选择部门....",
    attachments=attachments_json
)

# Start the Flask server
if __name__ == "__main__":
    app.run()

你可能感兴趣的:(关于slack服务器发送消息的demo)