travisghansen / node-red-slack

A node-red module to post to Slack.com
MIT License
22 stars 29 forks source link

Invalid channel when targeting a user #40

Open natcl opened 4 years ago

natcl commented 4 years ago

I often get a "invalid channel: @username" message when targeting a user using the @username syntax in msg.topic. I noticed it often happens shortly after a deploy but seems to happen randomly too. What could be causing this ? Thanks !

travisghansen commented 4 years ago

Behind the scenes we keep a list of all channels etc (slack-state node(s)) so when you use that syntax what happens is the app does a lookup from the slack state data and then replaces the channel with the real channel id. If the slack state hasn’t loaded yet (the startup situation) or is behind (new users getting created etc) then it may not be able to do the lookup. Or the channel may simply not exist.

At deploy doesn’t bother me and makes sense... after things have loaded (an event gets fired from the second output on the slack-state node where you can detect this in your flows) does bother me. Do you have many users/channels? Is the issue (after startup) generally happening for users who have just been created? Or don’t yet have a dm channel with the bot?

natcl commented 4 years ago

Hello, yes we have many users (400+) and a lot of channels too (200+) These are not new users, they've been there several years. These users never interacted with the bot however. Le me know if I can provide more info and thanks for the followup !

travisghansen commented 4 years ago

Yeah, my guess is the fact the bot and user have never conversed means there is no channel created. I would be hesitant to add channel creation logic to the node to handle these scenarios but am open to suggestions for sure. It's been a while since I reviewed the api as well so maybe they have a way to just send the username and the channel gets created on the fly.

natcl commented 4 years ago

Thanks, would there be alternatives ? I'll try with other users that I know have never conversed with the bot to see if it does it for them.

travisghansen commented 4 years ago

You could inject a node in between that would use the slack-state data before and do the lookup yourself. If the lookup fails then you could create the conversation using the http api nodes and get the response from the http call and use the more explicit syntax.

But I may have some bad logic as well. I'd have to research the api again to see if it allows for pure @username syntax or if we must send the action channel id...it's been quite a while since I coded it all up :)

natcl commented 4 years ago

Thanks, I just tested and it seems your therory is good, it happens with users that never interacted with the bot.

travisghansen commented 4 years ago

Yeah, and the spec seems to indicate the rtm api does not allow sending the username directly: https://api.slack.com/events/message

However, https://api.slack.com/methods/chat.postMessage#channels indicates that it accepts id or # or @ style syntax.

Unless you really need to use rtm for messages I would consider using the slack-web-out node + a function node (so you don't have to rewrite all your flows) instead of the slack-rtm-out node. In the function just 'translate' what you currently send to the rtm node to the proper syntax for the slack-web-out node.

Make sense?

natcl commented 4 years ago

Yes thanks, will try that !

natcl commented 4 years ago

just to make sure that I'm on the good path, I would use chat.postMessage ?

travisghansen commented 4 years ago

Yes, use chat.postMessage.

natcl commented 4 years ago

Seems to work ! Thanks a lot ! Closing this.

travisghansen commented 4 years ago

Going to reopen as the logic in the web out node is too strict.