poshbotio / PoshBot

Powershell-based bot framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there a variable for the user that initiated the command?

kmickeletto opened this issue · comments

Example, user executes whoami command. How can the function lookup who actually executed the command?

Expected Behavior

If user executed custom whoami command, there would be a variable or lookup function to determine who executed the command.

Current Behavior

I am not sure

Possible Solution

Variable or lookup function

@kmickeletto Yes, when the command is executed, PoshBot populates a variable called $global:PoshBotContext. In that, you'll find properties like the ID and name of the user who executed the command, the channel ID and name, the raw message PoshBot received, and other info.

Here is a demo command dump the variable contents back to the the backend: https://github.com/poshbotio/PoshBot.Demo/blob/master/PoshBot.Demo/PoshBot.Demo.psm1#L421

Here is the code showing how PoshBot populates it.

# Context for who/how the command was called
$parsedCommandExcludes = @('From', 'FromName', 'To', 'ToName', 'CallingUserInfo', 'OriginalMessage')
$global:PoshBotContext = [pscustomobject]@{
Plugin = $options.ParsedCommand.Plugin
Command = $options.ParsedCommand.Command
From = $options.ParsedCommand.From
FromName = $options.ParsedCommand.FromName
To = $options.ParsedCommand.To
ToName = $options.ParsedCommand.ToName
CallingUserInfo = $options.CallingUserInfo
ConfigurationDirectory = $options.ConfigurationDirectory
ParsedCommand = $options.ParsedCommand | Select-Object -ExcludeProperty $parsedCommandExcludes
OriginalMessage = $options.OriginalMessage
BackendType = $options.BackendType
}

Thanks so much! Works like a charm!