【问题标题】:Slack API - chat.update : "message_not_found"Slack API - chat.update:“message_not_found”
【发布时间】:2017-09-27 19:10:51
【问题描述】:

我正在使用“slack-node”npm 包为我的团队构建一个简单的 Slack 应用程序。到目前为止,机器人的初始化效果很好。在使用自定义斜杠命令后,我让它向频道发布消息。此消息包含一个交互式按钮,用户按下该按钮后,消息应更新。

来自我的日志:

// 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' }

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

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

这是我正在使用的代码 sn-p:

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

 }

}

我认为 slack-node 包使用来自 slack 的 web-api 但不确定。根据 Slack 'message_not_found' 意味着不存在具有请求时间戳的消息,这是不正确的,消息就在那里。

以前有没有人遇到过同样/类似的问题? 感谢您的任何建议。

【问题讨论】:

    标签: node.js slack-api


    【解决方案1】:

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

    这也适用于 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
    });
    

    阅读此内容 (https://api.slack.com/interactive-messages),您可能还可以使用一个额外的参数(虽然我不知道何时以及如何使用):"replace_original": true,但还没有尝试过.

    【讨论】:

    • 感谢您深入研究 'response_url' 方法!
    【解决方案2】:

    我可以确认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"
    }
    

    然后:

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

    而使用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"
        }
    }
    

    然后:

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

    我认为这只是因为临时消息的本质,它们在频道中被定向到最终用户并且不是持久的。引用自https://api.slack.com/methods/chat.postEphemeral

    无法保证临时消息传递 - 用户必须是 当前活跃在 Slack 中并且是指定频道的成员。经过 自然,短暂的消息不会在重新加载、桌面和 移动应用程序或会话。

    希望这会有所帮助!

    【讨论】:

    • 这是一个非常有趣的细节,我以前从未听说过。谢谢@phsultan,我会用 chat.postMessage 试一试!稍后回复您。
    • 工作就像一个魅力!我认为还有另一种更新临时消息的方法,即使用 response_url。那我可能会检查一下。再次感谢!
    • 伟大的帕斯卡,请告诉我们您的发现。我很想知道是否有办法更新临时消息。谢谢!
    • 所以还有另一种方法可以更新 slack 中的消息,即使它们是 ephemeral:Slack 有效负载几乎每次都包含 response_url。因此,如果有效负载来自按下按钮,您可以使用 response_url 简单地创建另一个请求来更新此消息。
    • 在下面发布了另一个答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    • 2019-01-04
    • 2019-12-11
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多