Python - 在 Slack API 中添加链接

W. *_*ens 2 python slack-api slack

目前我有一个 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'
                }
            ]
Run Code Online (Sandbox Code Playgroud)

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

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

小智 5

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

"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
            }
        ]
Run Code Online (Sandbox Code Playgroud)