我试图通过他们的API向松弛消息添加一个附件。我正在使用他们推荐的python包装。我可以发送和接收基本的消息,但是当我试图以两个按钮的形式添加附件时,它就失败了。我已经做了一个松弛的应用程序,并链接的机器人,因为他们在他们的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'}
发布于 2016-11-03 18:28:22
我想通了。
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文档中格式化它的方式。附件只需要是“附件”键之后的数组。
发布于 2016-11-03 18:12:35
我想这个简单的基本例子是可行的。
示例:
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和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"}]}]}]')由于没有进一步的神奇之处,但是请求模块slackrequest.py,我想尝试一下发送作为数组。
https://stackoverflow.com/questions/40407574
复制相似问题