【问题标题】:How do I get a Slack message's id/timestamp from a Hubot script?如何从 Hubot 脚本中获取 Slack 消息的 ID/时间戳?
【发布时间】:2015-09-29 22:46:15
【问题描述】:

似乎没有timestamp 属性,而id 属性是undefined。这是hubot的插件脚本代码:

module.exports = (robot) ->
  robot.hear /\bclocks?\b/i, (msg) ->
    msg.http("https://slack.com/api/reactions.add")
      .query({
        token: process.env.SLACK_API_TOKEN
        name: "bomb"
        timestamp: msg.message.timestamp # This property doesn't exist
      })
      .post() (err, res, body) ->
        console.log(body)
        return

我从 Slack API 得到的响应是:

{"ok":false,"error":"bad_timestamp"}

当我登录 msg.message 时,它看起来像这样:

{ user: 
  { id: 'abc123',
    name: 'travis',
    room: 'test-bots',
    reply_to: 'zyx987' },
 text: 'clock',
 id: undefined,
 done: false,
 room: 'test-bots' }

如何获取触发侦听器的消息的时间戳或 ID?

【问题讨论】:

    标签: coffeescript hubot slack-api


    【解决方案1】:

    我收到了 Slack 团队的回复,当升级到更新的 API 时,您可以访问一个名为 rawMessage 的新属性。以下是我完成它的步骤:

    1. 升级 nodejs 和 hubot(这可能是可选的,但我们的版本非常过时)
    2. 升级 slack-adapter 并按照他们的说明连接到更新的 API https://github.com/slackhq/hubot-slack#upgrading-from-earlier-versions-of-hubot
    3. 您现在应该可以从脚本中访问更新的属性了。

    这是升级后对我有用的代码: https://gist.github.com/dieseltravis/253eb1c6fea97f116ab0

    module.exports = (robot) ->
      robot.hear /\bclocks?\b/i, (msg) ->
        queryData =  {
            token: process.env.HUBOT_SLACK_TOKEN
            name: "bomb"
            channel: msg.message.rawMessage.channel # required with timestamp, uses rawMessage to find this
            timestamp: msg.message.id # this id is no longer undefined
          }
    
        if (queryData.timestamp?)
          msg.http("https://slack.com/api/reactions.add")
            .query(queryData)
            .post() (err, res, body) ->
              #TODO: error handling
              return
    

    【讨论】:

      猜你喜欢
      • 2023-02-01
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 2020-12-06
      相关资源
      最近更新 更多