poshbotio / PoshBot

Powershell-based bot framework
MIT License
536 stars 108 forks source link

Channel Rules won't apply #172

Closed pauby closed 5 years ago

pauby commented 5 years ago

When I leave the configuration with the default channel rules, everything works as expected. When I specify a channel rule, no commands can be accessed.

Expected Behavior

I expect the channel rules to apply to the channel names specified.

Current Behavior

When a specific channel rule is applied, it stop all commands working. The configuration I have is:

ChannelRules = @(
    @{
        Channel = 'private-channel1'
        IncludeCommands = @('*')
        ExcludeCommands = @()
    },
    @{
        Channel = 'private-channel2'
    IncludeCommands = @('Builtin:Get-CommandHelp')
    ExcludeCommands = @()
    }
)

When I try and execute the !man command or !Get-CommandHelp in private-channel2 I get the error:

Sorry :( PoshBot has been configured to not allow that command in this channel. Please contact your bot administrator.

I have noticed this in the logs - no idea if it's relevant:

{"DataTime":"2019-08-21 16:31:30Z","Class":"SlackBackend","Method":"ChannelIdToName","Severity":"Warning","LogLevel":"Debug","Message":"Could not resolve channel [<CHANNEL ID>]","Data":{}}

and then later:

{"DataTime":"2019-08-21 16:31:30Z","Class":"SlackBackend","Method":"GetUserInfo","Severity":"Warning","LogLevel":"Debug","Message":"Could not resolve channel []","Data":{}}
{"DataTime":"2019-08-21 16:31:30Z","Class":"PluginManager","Method":"MatchCommand","Severity":"Warning","LogLevel":"Info","Message":"Unable to match parsed command [:] to a plugin command","Data":{}}

Steps to Reproduce (for bugs)

  1. Just enter a command.

Your Environment

devblackops commented 5 years ago

Hey @pauby That channel resolution error is probably the cause of this. Can you run this and let me know if all channels (public and private) are returned?

$getChannelParams = @{
    Token           = '<MY-TOKEN>'
    ExcludeArchived = $true
    Verbose         = $false
    Paging          = $true
    }
$allChannels = Get-SlackChannel @getChannelParams
pauby commented 5 years ago

Hey @devblackops, that code shows me all of the public channels with member count, topic and purpose populated. It shows me two private channels (the ones the bot has been invited to) with no member count, topic or purpose, just the channel name.

Is this maybe a limitation of poshbot to work with channel rules in private channels, Slack not providing the correct access to poshbot so it can resolve the private channel name and match it to the channel rule name? Do you know if anybody else using channel rules in private channels?

devblackops commented 5 years ago

There was an issue with PSSlack retrieving info on private channels that was fixed in https://github.com/RamblingCookieMonster/PSSlack/pull/80 and https://github.com/RamblingCookieMonster/PSSlack/pull/90.

Because PoshBot wasn't resolving the private channel names/IDs, channels rules wasn't working.

To fix, I pinned the updated PSSlack version in PoshBot and implemented paging support for workspaces with a large number of channels in a62ef43ba859c59cd55a1c04e72758f2adc8e268.

Maybe there is another bug somewhere. I'll take a look!

pauby commented 5 years ago

Thanks @devblackops - Just in case I checked what version of PSSlack were installed and it's just 1.0.1 which is the latest.

pauby commented 5 years ago

As an update to this.

I moved PoshBot to it's own non-admin account (it was previously running under another non-admin account but wanted to isolate it). I ran the code from above and noticed that the channels PoshBot was invited to now had the purpose populated. There is nothing running in the other account that is not runnign here, it's on the same server, same installed modules etc.

I went ahead and run PoshBot and got a bit more info from the logs. It looked like it was failing because the version hadn't been specified.

I ended up with this config, which is now working:

  ChannelRules = @(
    @{
        Channel = 'private-channel1'
        IncludeCommands = @('*')
        ExcludeCommands = @()
    },
    @{
        Channel = 'private-channel2'
        IncludeCommands = @(
            'module:command1:*', 
            'module:command2:*', 
            'chocolatey.poshbot:sing:*', 
            'Builtin:Get-CommandHelp:*',
            'Builtin:slap:*',
            'Builtin:Get-ScheduledCommand:*',
            'Builtin:New-ScheduledCommand:*',
            'Builtin:Remove-ScheduledCommand:*'
        )
        ExcludeCommands = @()
    }
  )     

Note the wildcard for the version.

I have found another bug (I think) which I'll raise an issue for though. This one, at the moment seems resoolved. If it crops up again I will re-open it.