scrapinghub / slackbot

A chat bot for Slack (https://slack.com).
MIT License
1.26k stars 394 forks source link

Fix users groups #117

Closed StewPoll closed 7 years ago

StewPoll commented 7 years ago

This should solve the issue in #115 and #61. As it uses events, is a nicer implementation than #64 as it is immediate and not delayed

Now I'm slightly confused as how your branches work...

roperi commented 7 years ago

Thanks, @TetraEtc

I created a new user and sent a text message to the bot and it sure listens!! BUT it quickly throws error..I'm getting the following:

.../.virtualenvs/slackbot/lib/python2.7/site-packages/slackbot/dispatcher.py", line 141, in loop
if event['type'] == 'message':
KeyError: 'type'
roperi commented 7 years ago

This works though (at least for the bot to recognise an user that just signed up to the team). The key here is event.get('type') instead of event['type']:

    def loop(self):
        while True:
            events = self._client.rtm_read()
            for event in events:
                if event.get('type') == 'message':
                    self._on_new_message(event)
                elif event.get('type') in ['channel_created', 'group_joined', 'im_created']:
                    channel = [event['channel']]
                    self._client.parse_channel_data(channel)
                elif event.get('type') == 'team_join':
                    user = event.get('user')
                    self._client.users[user['id']] = user                
                else:
                    continue
            time.sleep(1)
StewPoll commented 7 years ago

Yeah, I can fix this shortly.

This is slightly confusing to me though, as far as I know every event from Slack should have a type. I am confused how the bot is getting an event without a type parameter

I am not a fan of using .get in python, personally I would rather wrap this in a try/except block, but using .get would work as well.

roperi commented 7 years ago

Thanks, @TetraEtc

I also prefer the traditional dict key myself instead of .get()

Believe me, I'm more confused than you :) so I wonder how can I check which users are logged into the team from another process? Thing is I have a loop running in another script monitoring events and communicating to slack via incoming webhooks. Any ideas, or pointers on How could I retrieve who's signed in by, say, username (i.e. @username) so I can start sending them event messages?

Thanks so much for your time.

StewPoll commented 7 years ago

I think that falls a little outside this PR specifically, but slack has the users.list method that you could for this. Simple use their presence parameter in a call to that end point and you'll get all users in your account and their presence, which can be used to determine online/offline members.

roperi commented 7 years ago

@TetraEtc Got it! Thanks so much! And thanks again for your contribution!

StewPoll commented 7 years ago

Man... This is old.

I have deleted my original fork of this, so I'll have to close this and re-open a new PR