Slack API - chat.update:"message_not_found"

Pas*_*ers 5 node.js slack-api

我正在使用'slack-node'npm包为我的团队构建一个简单的Slack App.到目前为止,机器人的初始化工作很好.我使用自定义斜杠命令后将消息发送到通道.此消息包含一个交互式按钮,在用户按下该消息后,该消息应该更新.

从我的日志:

// User uses /event slash command
18:48:50 UTC - - Received [POST] to path [/event]

// API response after posting the message
{ ok: true, message_ts: '1506538130.000343' }

// Incoming request after user pressed the button
18:49:32 UTC - - Received [POST] to path [/button_reply]

// Extracted channel id and message ts
C3PEQESP5 1506538130.000343

// respond after using chat.update
{ ok: false, error: 'message_not_found' }
Run Code Online (Sandbox Code Playgroud)

我确保传递正确的通道ID和消息ts,我100%确定这些参数是正确的,因为您只需要比较响应(对象).

这是我正在使用的软件包:https://www.npmjs.com/package/slack-node

这是我正在使用的代码片段:

module.exports = {

 postEphemeral : function(text, body, callback) {

  slack.api('chat.postEphemeral', {
     attachments:JSON.stringify(text.attachments) ,
     channel:body.channel_id,
     user:body.user_id
  }, function(err, response){
     console.log(response);
     return callback(null);
  });

 },

 updateMessage : function(text, body, callback) {

  console.log(body.channel.id, body.message_ts)

  slack.api('chat.update', {
     attachments:JSON.stringify(text.attachments) ,
     channel:body.channel.id,
     ts:body.message_ts
  }, function(err, response){
     console.log(response);
     return callback(null);
  });

 }

}
Run Code Online (Sandbox Code Playgroud)

我认为slack-node包使用了来自slack但不确定的web-api.根据Slack的'message_not_found'表示没有带有请求的时间戳的消息,这不是真的,消息就在那里.

有没有人之前有过相同/类似的问题?谢谢你的建议.

Phi*_*tan 6

我可以确认chat.postEphemeral无法更新的生成消息:

curl -X POST 'https://slack.com/api/chat.postEphemeral?token=xoxp-mytoken&channel=C143CVGRE&text=blabla&user=U334D3FFF&pretty=1'
{
  "ok": true,
  "message_ts": "1506544109.000059"
}
Run Code Online (Sandbox Code Playgroud)

然后 :

curl -X POST 'https://slack.com/api/chat.update?token=xoxp-mytoken&channel=C143CVGRE&text=updated&ts=1506544109.000059&pretty=1'
{
  "ok": false,
  "error": "channel_not_found"
}
Run Code Online (Sandbox Code Playgroud)

然而,发送的消息chat.postMessage可以是:

curl -X POST 'https://slack.com/api/chat.postMessage?token=mytoken&channel=C143CVGRE&text=blabla&pretty=1'
{
    "ok": true,
    "channel": "C143CVGRE",
    "ts": "1506544023.000474",
    "message": {
        "text": "blabla",
        "username": "Slack API Tester",
        "bot_id": "B4X35D333",
        "type": "message",
        "subtype": "bot_message",
        "ts": "1506544023.000474"
    }
}
Run Code Online (Sandbox Code Playgroud)

然后 :

curl -X POST 'https://slack.com/api/chat.update?token=mytoken&channel=C143CVGRE&text=updated&ts=1506544023.000474&pretty=1'
{
    "ok": true,
    "channel": "C143CVGRE",
    "ts": "1506544023.000474",
    "text": "updated",
    "message": {
        "text": "updated",
        "username": "Slack API Tester",
        "bot_id": "B4X35D333",
        "type": "message",
        "subtype": "bot_message"
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为这只是因为短暂消息的本质,它是针对通道中的最终用户而不是持久性的.引用自https://api.slack.com/methods/chat.postEphemeral:

短暂的邮件传递无法保证 - 用户必须当前在Slack中处于活动状态并且是指定频道的成员.从本质上讲,短暂的消息不会在重新加载,桌面和移动应用程序或会话中持续存在.

希望这可以帮助!


Pas*_*ers 6

在用户按下按钮后更新机器人消息的另一种方法(交互式消息)。您可以使用response_url也包含在来自按钮按下的有效负载中的 。只需对此提出另一个POST请求,response_url就像您发布一条简单的消息一样。

这也适用于 ephemeral 消息!

var response_url = body.response_url;

var options = {
  url: response_url,
  method: 'POST',
  body: JSON.stringify(text)
}

request(options,function(err, response, body) {
  // Continue doing stuff
});
Run Code Online (Sandbox Code Playgroud)

阅读本文(https://api.slack.com/interactive-messages),您可能还可以使用其他参数(虽然我不知道何时以及如何使用)"replace_original": true:,但还没有尝试过。