poshbotio / PoshBot

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

Is there a way to have poshbot send a message before/during the execution of a command? #148

Closed mgeorgebrown89 closed 5 years ago

mgeorgebrown89 commented 5 years ago

I think I know the answer to this already, based on the Message Processing Flow part of the docs, but is this possible?

Can poshbot send a message in the middle of a function? Like right before an attempted API call is made and after?

devblackops commented 5 years ago

@mgeorgebrown89 I'm not 💯 percent sure what you're asking. Are you asking if PoshBot can execute multiple commands in parallel? If that is the question then yes, since they are run in separate jobs, PoshBot will fire them off, then continue doing what it needs to do and then check back up on the job and collect the results when it is finished. PoshBot won't interfere with what the job is doing.

There is the concept of middleware, which are scripts you can have PoshBot run before/after certain stages of processing a command. In middleware you can insert extra logic to control if/when/how a command is executed.

mgeorgebrown89 commented 5 years ago

@devblackops So for a more specific example, say I execute a command and want an immediate message sent before telling the user that it's starting, and then another after it's completed. I have a while loop that checks on the status of an object created by an API. I want a message to send each time this status changes. I'll look at the middleware stuff, thanks.

devblackops commented 5 years ago

@mgeorgebrown89 For that, you can probably look at using Pre/Post execute hooks.

mgeorgebrown89 commented 5 years ago

So I realized using Send-SlackMessage will send a message in the middle of the scripts execution, rather than wait until the end of the job like New-PoshBotTextResponse. Is there a reason not to just do this?

devblackops commented 5 years ago

Nope. Nothing stopping you from using PSSlack to send a message on your own.

mgeorgebrown89 commented 5 years ago

@devblackops so in reference to this, this works well, but how can I get my token without entering it in plain-text? I've tried $Global:PoshBotContext.ConfigurationDirectory.BackendConfiguration.Token but it doesn't like that.

devblackops commented 5 years ago

@mgeorgebrown89 With middleware scripts the bot admin has more control over what scripts are run (and are executed in the same context as the bot). Because of that you'll have the main bot instance passed as an argument to the scripts and they can pull out the token with something like: $Bot.Backend.Connection.Config.Credential.GetNetworkCredential().password

Normal plugins are written by (potentially unknown individuals) as you can just download plugins from the PS Gallery. You probably don't want to expose the bot itself to these plugins and inadvertently introduce a security issue. Because of this, plugin commands get executed in separate PS jobs and won't have direct access to the bot itself.

If you're OK with the security implications, you can pass the Slack token as a parameter to your custom plugins using plugin configuration.