将附件添加到slacker聊天消息

M. *_*ara 1 python message chat slack

我正在尝试使用slacker python api为Slack消息发布消息我无法附加到我的消息的链接,如下面的代码所示:

    attachments = [title, link_to_events, "More details"]

    print type(attachments) # this is a list 

    slack = Slacker(slack_api_token)

    # Send a message to #general channel
    slack.chat.post_message(slack_channel, message, attachments=attachments)
Run Code Online (Sandbox Code Playgroud)

在slacker代码中,看起来我们正在寻找一个"列表"类型的变量:

https://github.com/os/slacker/blob/master/slacker/ init .py 第241行:

    # Ensure attachments are json encoded
    if attachments:
        if isinstance(attachments, list):
            attachments = json.dumps(attachments)

    return self.post('chat.postMessage',
                     data={
                         'channel': channel,
                         'text': text,
                         'username': username,
                         'as_user': as_user,
                         'parse': parse,
                         'link_names': link_names,
                         'attachments': attachments,
                         'unfurl_links': unfurl_links,
                         'unfurl_media': unfurl_media,
                         'icon_url': icon_url,
                         'icon_emoji': icon_emoji
                     })
Run Code Online (Sandbox Code Playgroud)

我的代码有问题吗?

fyi:这是我在slack api文档https://api.slack.com/custom-integrations中发现的 :

{
    "text": "New Help Ticket Received:",
    "attachments": [
        {
            "title": "App hangs on reboot",
            "title_link": "http://domain.com/ticket/123456",
            "text": "If I restart my computer without quitting your app, it stops the reboot sequence.\nhttp://domain.com/ticket/123456",
        }
    ]
} 
Run Code Online (Sandbox Code Playgroud)

M. *_*ara 7

好吧,我已经找到了,我需要一个列表类型的字典

one_attachement = {
        "title": "App hangs on reboot",
        "title_link": "http://example.com/ticket/123456",
        "text": "If I restart my computer without quitting your app, it stops the reboot sequence.\nhttp://example.com/ticket/123456",
    }


attachements = [one_attachement]
slack.chat.post_message(slack_channel, message, attachments=attachments)
Run Code Online (Sandbox Code Playgroud)