简体   繁体   中英

How do I get a Slack message's id/timestamp from a Hubot script?

There doesn't seem to be a timestamp property and the id property is undefined . Here is the hubot's plugin script code:

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

The response that I get back from the Slack API is:

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

When I log msg.message it looks like this:

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

How can I get a timestamp or id for the message that triggered the listener?

I heard back from Slack's team and there is a newer property called rawMessage that you have access to when up upgrade to the newer API. Here's the steps I went through to get it working:

  1. Upgrade nodejs & hubot (this may be optional, but our versions were very out of date)
  2. Upgrade the slack-adapter and follow their instructions to connect to the newer API https://github.com/slackhq/hubot-slack#upgrading-from-earlier-versions-of-hubot
  3. You should now get access to the newer properties from your scripts.

Here's the code that worked for me after upgrading: 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM