Hello,
I'm looking for a way to have my bot messaging to a channel without previous human request/interaction.
I found this case: https://github.com/slackhq/hubot-slack/issues/70
and as far as I understand it should work with:
robot.messageRoom(channel, message);
where "channel" is the chan's code (not the name) and message is what I want the bot to say.
So I tried with:
robot.messageRoom('C0000000', 'Arrr... here I am, infederls...');
but all I get is the following error:
ERROR Unable to load /src/scripts/namshi: TypeError: Cannot read property 'send' of undefined
at SlackBot.send (/src/node_modules/hubot-slack/src/slack.coffee:206:16, <js>:248:32)
at Robot.messageRoom (/src/node_modules/hubot/src/robot.coffee:457:19, <js>:398:42)
which to me sounds like: I don't have a writable stream where to put your message to.
Any suggestion?
I've got a couple of modules that do this, all you need to do is:
robot.send room: 'general', "This is a 'spontaneous' message"
robot.send just takes an envelope, so you can use it to DM users/groups too if you want.
thank you, works! :D
I think the problem you were seeing with messageRoom was this line: https://github.com/slackhq/hubot-slack/blob/af37d933671423e5c00966395c9852ee7123bab7/src/slack.coffee#L209 (it's hard to tell without knowing what version of hubot-slack you are using).
Looking at the code for messageRoom and send, messageRoom just wraps send, so it's weird one would work and the other.
Actually, looking at the original code, the problem is sending a channel ID (C000000) rather than a channel name (#general).
slightly tangent, but cc https://github.com/github/hubot/issues/1058 where I talk about adapters documenting things like messages, what data type a 'room' is, etc
_Hopefully a helpful tip for anyone who stumbles over this thread:_
For me, the messageRoom-command worked as expected when sending messages to public rooms, but failed when I try to set up messaging to private rooms.
The remaining issues around sending messages to private groups _should_ be cleared up in https://github.com/slackhq/hubot-slack/pull/309 but I'll be checking of course.
I'm using 4.0.1. This method only works with channel id
robot.send room: 'channel_id', 'message'
The channel id is found here:
https://slack.com/api/channels.list?token=HUBOT_SLACK_TOKEN
I wrote a file in my project 'scripts/slack-rooms.coffee' to fix the messageRoom:
module.exports = (robot) ->
request = require('request')
Promise = require('es6-promise').Promise
idsPromise = new Promise((resolve, reject) ->
request 'https://slack.com/api/channels.list?token=' + process.env.HUBOT_SLACK_TOKEN, (error, response, body) ->
name2id = {}
if !error and response.statusCode == 200
json = JSON.parse(body)
for channel in json.channels
name2id['#' + channel.name] = channel.id
resolve name2id
)
robot.messageRoom = (roomNameOrId, txt) ->
idsPromise.then (name2id) ->
if roomNameOrId[0] == '#'
robot.send { room: name2id[roomNameOrId] }, txt
else
robot.send { room: roomNameOrId }, txt
usage:
robot.messageRoom #yourChannelName, 'message'
robot.messageRoom 'CHANNEL_ID', 'message'
The contribution from @fabiooshiro would be pretty great for usability. After updating from a (rather old) previous version I find I now have to change all my human readable channel names to channel ids across the hooks I've set up.
Ideally it would be awesome to be able to specify either
thanks for chiming in folks. it looks like the actual issue here is that the envelope property is not sufficiently documented (thanks @technicalpickles for the link to hubotio/hubot#1058).
for folks coming in late, the room property of the envelope in this adapter is a channel ID (not the channel name). this aligns well with the announcement for usernames (see section "Just one more thing: channels").
if you'd like a helper of some sort to bridge channel names to channel IDs, i'm happy to have that discussion in a new issue, so please start one. off the top of my head, i'd comment that i think persisting the mapping to the hubot brain, rather than a variable in your script, would be more reliable. but in general, i don't think you should need channel names because you should write your scripts to identify channels programmatically. when all you have is a channel name, you could use the channels.list Web API method to find the ID (but this will not work for private channels). anyway, create that issue if you really want this feature!
@aoberoi I'm still getting TypeError: Cannot read property 'send' of undefined, it seems that @client in @client.send(envelope, message) in bot.coffee is undefined.
I'm using "hubot": "^3.0.1" and "hubot-slack": "^4.3.4"
I'm calling this method:
module.exports = robot => {
robot.messageRoom({ room: "general" }, "hello");
}
how is it possible that @client is undefined? shall I bind it somehow?
thanks
@cirpo: i looked through the source in the versions you mentioned, and i have no idea how that could happen. i think this is an unrelated issue. if you are still experiencing it, would you mind creating a new issue? we can discuss steps to reproduce it there.
@aoberoi @cirpo I'm seeing this too. Is there a separate issue open? I can't find it
@mhemmings i don't see one either. happy to work with you on that if you create a new issue. (sorry for the delayed response)
I think we've done a decent job of describing the solution to this issue in the documentation here.
I was getting same error as @cirpo with "hubot": "^3.0.1" and "hubot-slack": "^4.5.3", I downgraded hubot to 2.19.0 and it was resolved.
Most helpful comment
I'm using 4.0.1. This method only works with channel id
robot.send room: 'channel_id', 'message'
The channel id is found here:
https://slack.com/api/channels.list?token=HUBOT_SLACK_TOKEN
I wrote a file in my project 'scripts/slack-rooms.coffee' to fix the messageRoom:
usage: