RocketChat / hubot-rocketchat-boilerplate

An example Hubot demonstrating usage of the Rocket.Chat adaptor.
MIT License
36 stars 32 forks source link

robot messageRoom does not working #4

Open noobbbbb opened 6 years ago

noobbbbb commented 6 years ago

I'm using Google's translation application...

I have failed to call "robot.messageRoom":

module.exports = function(robot) {
  robot.router.post('/hubot/hoge/:room', function(req, res) {
    let room = req.params.room
    data = req.body.payload
    robot.messageRoom(room, 'hoge Posted!')
    res.send('OK')
  })
}

Error Message:

TypeError: Cannot read property 'roomID' of undefined
  at strings.map (/home/hubot/hubot-rocketchat-boilerplate/node_modules/hubot-rocketchat/index.js:155:25)
  at Array.map (<anonymous>:null:null)
  at Robot.send (/home/hubot/hubot-rocketchat-boilerplate/node_modules/hubot-rocketchat/index.js:154:20)
  at Robot.messageRoom (/home/hubot/hubot-rocketchat-boilerplate/node_modules/hubot/src/robot.js:608:23)
  at /home/hubot/hubot-rocketchat-boilerplate/scripts/main.js:31:13
  at callbacks (/home/hubot/hubot-rocketchat-boilerplate/node_modules/express/lib/router/index.js:164:37)
  at param (/home/hubot/hubot-rocketchat-boilerplate/node_modules/express/lib/router/index.js:138:11)
  at param (/home/hubot/hubot-rocketchat-boilerplate/node_modules/express/lib/router/index.js:135:11)
  at pass (/home/hubot/hubot-rocketchat-boilerplate/node_modules/express/lib/router/index.js:145:5)
  at Router._dispatch (/home/hubot/hubot-rocketchat-boilerplate/node_modules/express/lib/router/index.js:173:5)
  at Object.router (/home/hubot/hubot-rocketchat-boilerplate/node_modules/express/lib/router/index.js:33:10)
  at next (/home/hubot/hubot-rocketchat-boilerplate/node_modules/connect/lib/proto.js:174:15)
  at Object.multipart [as handle] (/home/hubot/hubot-rocketchat-boilerplate/node_modules/connect-multiparty/index.js:42:27)
  at next (/home/hubot/hubot-rocketchat-boilerplate/node_modules/connect/lib/proto.js:174:15)
  at Object.urlencodedParser [as handle] (/home/hubot/hubot-rocketchat-boilerplate/node_modules/body-parser/lib/types/urlencoded.js:81:44)
  at next (/home/hubot/hubot-rocketchat-boilerplate/node_modules/connect/lib/proto.js:174:15)
  at /home/hubot/hubot-rocketchat-boilerplate/node_modules/body-parser/lib/read.js:121:5
  at invokeCallback (/home/hubot/hubot-rocketchat-boilerplate/node_modules/raw-body/index.js:262:16)
  at done (/home/hubot/hubot-rocketchat-boilerplate/node_modules/raw-body/index.js:251:7)
  at IncomingMessage.onEnd (/home/hubot/hubot-rocketchat-boilerplate/node_modules/raw-body/index.js:307:7)
  at emitNone (events.js:106:13)
  at IncomingMessage.emit (events.js:208:7)
  at endReadableNT (_stream_readable.js:1056:12)
  at _combinedTickCallback (internal/process/next_tick.js:138:11)
  at process._tickCallback (internal/process/next_tick.js:180:9)

/hubot-rocketchat-boilerplate/node_modules/hubot-rocketchat/index.js:155:25

if (envelope.user.roomID) driver.sendToRoomId(text, envelope.user.roomID)
else driver.sendToRoom(text, envelope.room)

If this is done as below it will work normally

//if (envelope.user.roomID) driver.sendToRoomId(text, envelope.user.roomID)
//else driver.sendToRoom(text, envelope.room)
driver.sendToRoom(text, envelope.room)

What are the reasons why 'roomID' can not be recognized? Please advise me.

regards.

timkinnane commented 6 years ago

Cannot read property 'roomID' of undefined means the whole object is undefined, not just the roomID property.

This is a bug, thanks for reporting. The adapter should check if there's a user, before checking properties of the user. e.g. at https://github.com/RocketChat/hubot-rocketchat/blob/master/index.js#L146 - should be if (envelope.user && envelope.user.roomID) this is probably something that came through from the coffee script conversion, because coffeescript has a nice existential operator that's missing in standard JS.

I will fix this in the adapter asap. But for now, you can include an empty user in your envelope and call the send from the adapter directly which should satisfy the condition...

module.exports = function(robot) {
  robot.router.post('/hubot/hoge/:room', function(req, res) {
    let room = req.params.room
    data = req.body.payload
    robot.adapter.send({ room, user: {} }, 'hoge Posted!')
    res.send('OK')
  })
}

Please try that and comment if it works or not.

noobbbbb commented 6 years ago

Thank you for your reply. I corrected the script according to the proposal, and confirmed that it works normally!

timkinnane commented 6 years ago

Great. I'll just leave this open though until we've address the bug handling sendToRoom.