将附件添加到懒人聊天消息

时间:2016-04-25 01:52:47

标签: python chat message 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)

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

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
                     })

我的代码有问题吗?

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",
        }
    ]
} 

1 个答案:

答案 0 :(得分:6)

好吧我找到了, 我需要一个列表 dicts

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)