我试图使用松散的机器人发送带附件的消息进行闲聊。与api的连接是通过WebSocket完成的,直到现在,一切正常,除了发送带附件的消息之外的一切。
以下是发送消息的代码段:
public final void replyInteractive(WebSocketSession session, Event event, InteractiveMessage reply) {
try {
reply.setText(encode(reply.getText()));
reply.setType(EventType.MESSAGE.name().toLowerCase());
if (reply.getChannel() == null && event.getChannelId() != null) {
reply.setChannel(event.getChannelId());
}
session.sendMessage(new TextMessage(reply.toJSONString()));
if (logger.isDebugEnabled()) { // For debugging purpose only
logger.debug("Reply (Message): {}", reply.toJSONString());
}
} catch (IOException e) {
logger.error("Error sending event: {}. Exception: {}", event.getText(), e.getMessage());
}
}
这是此代码发送的json:
{"type":"message","channel":"D4CJ8B337","text":"New book!","attachments":[{"fallback":"Something went wrong","color":"#3AA3E3","title":"Do you want to buy this book?","actions":[{"name":"Buy","value":"Yes","text":"Buy","type":"button"},{"name":"No","value":"No","text":"No","type":"button"}],"attachment_type":"default","callback_id":"Djisda"}]}
但是,正如我所说,只有文本字段,#34;新书!",正在被接收并显示在聊天中。当我使用Slack Message Builder测试它时,整个消息似乎是有效的。 我想是的,提前谢谢。
答案 0 :(得分:2)
我发现了问题。 RTM API不接受消息中的附件。
来自Slack docs(https://api.slack.com/rtm):
RTM API仅支持发布使用我们的默认邮件格式化模式格式化的简单邮件。它不支持附件或其他消息格式化模式。
要发送附件,您需要使用chat.postMessage方法https://api.slack.com/methods/chat.postMessage