poshbotio / PoshBot

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

Config Parameters not loading #147

Closed mgeorgebrown89 closed 5 years ago

mgeorgebrown89 commented 5 years ago

Parameters aren't being pulled from config. Maybe I've set it up incorrectly.

In my plugin:

[CmdletBinding()]
[PoshBot.BotCommand(
    Aliases = ('nfHeader'),
    Permissions = 'update'
)]
param(
[PoshBot.FromConfig('nfUsername')]
$nfusername,
[PoshBot.FromConfig('nfPassword')]
$nfPassword
)

Those two variables are then used to make API calls, but it's not working. This is my config file:

  PluginConfiguration = @{
    vectoria = @{
      nfUsername = "xxxxxxxx"
      nfPassword = "xxxxxxxxx"
   }
  }

When i hard code the values above, it works fine. I'm not sure what's going on here. I've tried a few different things like making the parameter mandatory, but its not working.

Context: This function is called by another function. It creates the header for use with other API calls. My intention is to call it from another function, so this function itself isn't exported to the plugin. Could that be the issue? ###

devblackops commented 5 years ago

@mgeorgebrown89 PoshBot will only automatically supply those configuration-provided parameters to the command that was executed. It doesn't transfer to any other functions you call. In your primary command, define those parameters and then pass them to your other function.

mgeorgebrown89 commented 5 years ago

@devblackops Do the parameters need the mandatory option?

mgeorgebrown89 commented 5 years ago

I'm still having issues. I must be missing something.

config.psd1

@{
  ApprovalConfiguration = @{
    Commands = @()
    ExpireMinutes = 30
  }
  DisallowDMs = $False
  ModuleManifestsToLoad = @()
  LogDirectory = 'C:\poshbot\logs'
  Name = 'vectoria'
  BotAdmins = @('michael.g.brown')
  AlternateCommandPrefixSeperators = @(':',',',';')
  CommandHistoryMaxLogSizeMB = 10
  MiddlewareConfiguration = @{
    PreReceive = @()
    PostReceive = @()
    PreExecute = @()
    PostResponse = @()
    PreResponse = @()
    PostExecute = @()
  }
  FormatEnumerationLimitOverride = -1
  LogLevel = 'Verbose'
  SendCommandResponseToPrivate = @()
  ChannelRules = @{
    IncludeCommands = @('*')
    Channel = '*'
    ExcludeCommands = @()
  }
  ConfigurationDirectory = 'C:\poshbot'
  MaxLogsToKeep = 5
  AddCommandReactions = $True
  CommandHistoryMaxLogsToKeep = 5
  MaxLogSizeMB = 10
  PluginDirectory = 'C:\poshbot\plugins'
  MuteUnknownCommand = $False
  PluginConfiguration = @{
    vectoria = @{
        nfUsername = 'username'
        nfPassword = 'password'
    }
  }
  AlternateCommandPrefixes = @('VEC','Vec','vec')
  CommandPrefix = '!'
  BackendConfiguration = @{
    Token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    Name = 'SlackBackend'
  }
  PluginRepository = @('PSGallery')
  LogCommandHistory = $True
}

And the primary command:

function Get-NfClouds {
    [CmdletBinding()]
    param(
        [PoshBot.FromConfig('nfUsername')]
        [parameter(mandatory)]
        $Username,
        [PoshBot.FromConfig('nfPassword')]
        [parameter(mandatory)]
        $Password
     )
    $session = Get-NfAPISession -nfusername $Username -nfPassword $Password
    $Clouds_Uri = $session.api + "/clouds"
    $Clouds = Invoke-RestMethod -Uri $Clouds_Uri -Method Get -Headers $session.headers - 

ContentType "application/json"

Write-Output $Clouds.Name

    return $Clouds
}

I pass the username and password to construct the URIs and session headers, but I get this error in slack:

CommandRequirementsNotMet: Mandatory parameters for [Get-NfClouds] not provided.
Usage:
    syntaxitem
    ----------
    {@{name=get-nfclouds; commonparameters=true; workflowcommonparameters=false;...

When I hard-code the values into those parameters, everything works as expected, so for some reason it's not pulling the values from the config file....

Edit: Something else I just thought of for context: I'm using Export-ModuleMember to choose which functions are actually exported, rather than determining this in the config file. I saw @RamblingCookieMonster do this on his blog and for some reason I don't remember I went this way. Perhaps this is a problem?

Edit2: Even just copying and pasting the Get-foo example and trying to print the parameter from the config as ouput gives me the same error as above.

devblackops commented 5 years ago

@mgeorgebrown89 I'll take your config and command and see if I can repro. Is your plugin named vectoria?

Your plugin configuration has the following:

PluginConfiguration = @{
    vectoria = @{
      nfUsername = "xxxxxxxx"
      nfPassword = "xxxxxxxxx"
   }
}

Is your plugin that contains the Get-NfClouds commands called vectoria? Those names must match.

mgeorgebrown89 commented 5 years ago

So I think I'm a moron... Anytime I edited the config file, I was updating the plugin and testing and I kept getting the same results, but obviously that would be the case because that doesn't update the configuration the bot is running off of. Ugh. So I would make a change, and sometimes I would take effect, and other times not, so I would get varying results from doing the same thing.

Long story short, I fixed it. @devblackops

devblackops commented 5 years ago

Glad to hear it @mgeorgebrown89 👍