将附件添加到 Slackbot

Posted

技术标签:

【中文标题】将附件添加到 Slackbot【英文标题】:Adding attachment to Slackbot 【发布时间】:2017-03-17 09:30:35 【问题描述】:

我正在尝试通过他们的 API 将附件添加到松弛消息中。我正在使用他们推荐的 python 包装器。我可以发送和接收基本消息,但是当我尝试以 2 个按钮的形式添加附件时,它失败了。我制作了一个 Slack 应用程序,并按照他们在 API 中的说明链接了机器人。我已经仔细查看了 API,但无法弄清楚发生了什么。

def process_message(message, channel):
    intro_msg = json.loads('
                      "text": "What would you like to do?",
                      "attachments": [
                        
                          "text": "Choose an action",
                          "fallback": "You are unable to choose an option",
                          "callback_id": "lunch_intro",
                          "color": "#3AA3E3",
                          "attachment_type": "default",
                          "actions": [
                            
                              "name": "enroll",
                              "text": "Enroll",
                              "type": "button",
                              "value": "enroll"
                            ,
                            
                              "name": "leave",
                              "text": "Leave",
                              "type": "button",
                              "value": "leave"
                            
                          ]
                        
                      ]
                    ')
    r = sc.api_call("chat.postMessage", channel=channel, attachments=intro_msg)

回复只有u'ok': False, u'error': u'no_text'

【问题讨论】:

不要用来自stringdict对象创建intro_msg,你能告诉我当你直接传递dict对象时你看到了什么吗? 你完成了吗?如何获取响应负载? 我在下面找到了它。我的有效载荷中有 text 字段。这需要从有效负载中分离出来。 【参考方案1】:

我想通了。

python 包装器分离出有效负载。

intro_msg  = json.dumps(["text":"Choose an action","fallback":"You are unable to choose an option","callback_id":"lunch_intro","color":"#3AA3E3","attachment_type":"default","actions":["name":"enroll","text":"Enroll","type":"button","value":"enroll","name":"leave","text":"Leave","type":"button","value":"leave"]])

sc.api_call("chat.postMessage", channel=channel, text="What would you like to do?", attachments=intro_msg, as_user=True)

我的有效负载都在附件中,因为这是他们在 API 文档中对其进行格式化的方式。附件只需是附件键之后的数组。

【讨论】:

【参考方案2】:

我猜这个基本的简单例子是可行的。

例子:

from slackclient import SlackClient

slack_token = os.environ["SLACK_API_TOKEN"]
sc = SlackClient(slack_token)

sc.api_call(
  "chat.postMessage",
  channel="#python",
  text="Hello from Python! :tada:"
)

根据https://api.slack.com/methods/chat.postMessage 和https://api.slack.com/docs/message-buttons#readying_your_application_for_message_buttons,附件必须是一个数组。把它作为数组发送怎么样:

json.loads('["text":"What would you like to do?","attachments":["text":"Choose an action","fallback":"You are unable to choose an option","callback_id":"lunch_intro","color":"#3AA3E3","attachment_type":"default","actions":["name":"enroll","text":"Enroll","type":"button","value":"enroll","name":"leave","text":"Leave","type":"button","value":"leave"]]]')

除了请求模块https://github.com/slackapi/python-slackclient/blob/ddf9d8f5803040f0397d68439d3217d1e1340d0a/slackclient/_slackrequest.py 之外没有涉及更多魔法,我会尝试以数组形式发送。

【讨论】:

以上是关于将附件添加到 Slackbot的主要内容,如果未能解决你的问题,请参考以下文章

Acumatica - 将合并报告添加到电子邮件附件

Microsoft Flow将Outlook附件添加到VSTS工作项

无法使用烧瓶邮件python将附件添加到邮件

SharePoint 2010 - 客户端对象模型 - 将附件添加到 ListItem

键盘可见时,将输入附件视图添加到UITextField

直接从 MIME 字符串将附件添加到 MailMessage