Python - 在Slack API中添加链接

时间:2017-07-25 00:42:15

标签: python slack slack-api

目前我有一个Python脚本向slack发送消息。我想添加额外的链接,但无法弄清楚如何。这是我目前的代码。

def post_slack():
    """Post slack message."""
    try:
        token = 'xoxp-67503713541-67496795984-216701772021-c23bdfbe9635f1f63a4c802697147dfc'
        slack = Slacker(token)

        obj = slack.chat.post_message(
            channel='#dataworksapp',
            as_user= 'false',
            username = 'DataWorksBot',
            attachments=[
        {
            "color": "033E96",
            "title": "Pressure Transducer Weekly Information",
            "title_link": "https://console.cloud.google.com/storage/browser/firebase_results/?project=dataworks-356fa",
            "author_name": "Master Table",
            "author_link": "https://bigquery.cloud.google.com/table/dataworks-356fa:FirebaseArchive.PT_MasterTable?tab=preview",
            "text": "https://bigquery.cloud.google.com/table/dataworks-356fa:FirebaseArchive.PT_MasterTable?tab=preview",
            "fields": [
                {
                    "title": "Amount Used:",
                    "value": "countPTserial1",
                    "short": 'true'
                },{
                    "title": "Distinct Device ID's:",
                    "value": "countPTid1",
                    "short": 'true'
                },{
                    "title": "Total Connection Time (hr):",
                    "value": "sumPTct2",
                    "short": 'true'
                }
            ]

我无法找到与" author_link"类似的任何其他字段类别。我可以设置等于链接。我可以将"text"设置为等于链接,但如果我这样做,我宁愿将链接作为单个单词而不是在消息中发送的整个丑陋链接。

另外,我不能将链接设置为等于变量,然后将"text"设置为等于该变量。当我这样做时仍然显示整个链接。谢谢你的帮助!

1 个答案:

答案 0 :(得分:2)

我在这里看到了几个选项。在文本字段中,您可以通过将其包装在<中来更改链接的显示。 >符号并添加|定界符:

"text": "Click me: <https://foo.com|foo>"

哪个会显示为“Click me:foo”

或者您可以为每个链接创建其他字段,如下所示:

"fields": [
            {
                "title": "Link 1",
                "value": "<http://foo.com|foo>",
                "short": false
            },
                            {
                "title": "Link 2",
                "value": "<http://bar.com|bar>",
                "short": false
            }
        ]