Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

robot.enter and robot.leave doesn't work #296

Closed
Kenshinhu opened this issue May 19, 2016 · 8 comments · Fixed by #513
Closed

robot.enter and robot.leave doesn't work #296

Kenshinhu opened this issue May 19, 2016 · 8 comments · Fixed by #513
Labels
bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented good first issue

Comments

@Kenshinhu
Copy link

Kenshinhu commented May 19, 2016

I build hubot-slack in 3.4.2

I write it on example.coffee , but isn't work .

It's mean tigger enter on click bot's use?
It's mean tigger leave on click other use?

`module.exports = (robot) ->

         robot.enter (msg) ->.....

         robot.leave (msg) ->.....`
@DEGoodmanWilson
Copy link

Hello, would love to help out, but I'm afraid I need more details on your problem. Can you post a more detailed chunk of code that illustrates the problem you are having?

@leonfortej
Copy link

Having the same problem, pulled code directly from samples. Never kicks off.

module.exports = (robot) ->
enterReplies = ['Hi', 'Target Acquired', 'Firing', 'Hello friend.', 'Gotcha', 'I see you']
leaveReplies = ['Are you still there?', 'Target lost', 'Searching']

module.exports = (robot) ->
robot.enter (res) ->
res.send res.random enterReplies
robot.leave (res) ->
res.send res.random leaveReplies

@aoberoi
Copy link
Contributor

aoberoi commented Oct 17, 2017

the adapter seem to be hooked up correctly to channel_join and group_join events. @leonfortej @Kenshinhu are you sure you are using the API correctly? specifically, the messages should trigger when any user joins or leaves a channel that the bot is in. if that's what you expected and it didn't work, let me know so i can try to reproduce it.

@aoberoi aoberoi changed the title How to Work for enter/leave event? robot.enter and robot.leave doesn't work Oct 17, 2017
@aoberoi aoberoi added the needs info An issue that is claimed to be a bug and hasn't been reproduced, or otherwise needs more info label Oct 17, 2017
@JoniSykes
Copy link

I'm testing Hubot with Slack now and I can get the channel enter to trigger but not the channel leave.
Upon entering the channel a user is greeted with one of my custom greetings.
When a user leaves the channel.....nothing.

@aoberoi aoberoi added bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented and removed needs info An issue that is claimed to be a bug and hasn't been reproduced, or otherwise needs more info labels Oct 19, 2017
@aoberoi
Copy link
Contributor

aoberoi commented Oct 19, 2017

@JoniSykes thanks for the feedback! you inspired me to try reproducing the problem and even before getting there, i think i found it.

the current implementation relies on RTM "message" events, with subtype "channel_join"/"group_join" and "channel_leave"/"group_leave". Back in May, there was an announcement that Slack no longer guaranteed delivery of these events, in favor of new events with types "member_joined_channel" and "member_left_channel".

we need to update the implementation of the adapter to listen for and handle those events to generate the EnterMessage and LeaveMessage objects.

@alagos
Copy link

alagos commented Nov 27, 2017

👍
Weird stuff is, I already have a bot working in a slack space with a greeting message for new users, but I created another space today with this same script and is not working:

module.exports = (robot) ->
  robot.enter (msg) ->
    general = robot.adapter.client.rtm.dataStore.getChannelByName '#general'
    if msg.message.room == general.id
      robot.send {room: msg.message.user.id}, "Hi, *#{msg.message.user.name}*! :wave:"

I tried with the same hubot/hubot-slack versions, but nothing happens.

@aoberoi
Copy link
Contributor

aoberoi commented Dec 18, 2017

@alagos thanks for the data point. the best i can do to explain that is to say that the older event types are no longer guaranteed, so its possible that the differences in your workspaces are responsible for the different behavior.

@aoberoi
Copy link
Contributor

aoberoi commented Jul 17, 2018

for @alagos and anyone else that comes across this issue, i wanted to update the code sample shared above, since the dataStore is deprecated.

first, make sure you install the latest @slack/client using npm:

$ npm install --save @slack/client

next, follow the instructions in the docs for sending a message to a different channel, except listen for the enter event. once the above code is merged, this should work consistently for all users who enter the channel.

{WebClient} = require "@slack/client"

module.exports = (robot) ->

  # create a dedicated Web API client
  web = new WebClient robot.adapter.options.token

  # use the Web API client to get a list of all channels
  web.channels.list()
    .then (api_response) ->
      # Search the list for the channels for #general
      conversation = api_response.channels.find (channel) -> channel.name is "general"
      if conversation
        conversation.id
      else
        throw new Error('general room not found')
    .then (conversationId) ->
      # Now that we have the conversation ID for #general, start listening for users who join
      robot.enter (res) ->
        if res.message.room is conversationId
          # Create a DM conversation with the user who just joined
          web.im.open({ user: res.message.user.id })
            .then (create_response) ->
              # In the new DM conversation, send them a welcome message.
              res.send { room: create_response.channel.id }, "Hi, *<@#{res.message.user.id}>, :wave:"
            .catch (error) ->
              @robot.logger.error "Could not create an IM with user #{res.message.user.id}: #{error.message}"
    .catch (error) ->
      @robot.logger.error error.message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented good first issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants