如何从Hubot脚本中获取Slack消息的id / timestamp?

时间:2015-09-29 22:46:16

标签: coffeescript hubot slack-api

似乎没有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?

1 个答案:

答案 0 :(得分:0)

我从Slack的团队那里听说过,有一个名为rawMessage的新属性,当你升级到新的API时,你可以访问它。以下是我为实现目标而采取的步骤:

  1. 升级nodejs& hubot(这可能是可选的,但我们的版本已经过时了)
  2. 升级slack-adapter并按照其说明连接到较新的API https://gist.github.com/dieseltravis/253eb1c6fea97f116ab0
  3. 您现在应该可以从脚本中访问较新的属性。
  4. 以下是升级后对我有用的代码: this one

    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