How to set module-specific variables?

Hi,

I’m trying to test bot — API interaction. As a starting point, I choose weather bot from example module.
And I cannot figure where to set variable openWeatherApiKey.

I tried to set it in the out/bp/data/bots/<BOT_ID>/bot.config.json. I tried to create a file out/bp/data/bots/<BOT_ID>/config/channel-messenger.json and set the variable there.
Anyway, I get the same error:

17:16:25.600 ActionService An error occurred while executing the action "getweather [Error, Request failed with status code 401. Have you set up your OpenWeather API Key properly?]

I even tried to debug action service src/bp/core/services/action/action-service.ts. So, I modified method runAction in this way:

async runAction(actionName: string, incomingEvent: any, actionArgs: any): Promise<any> {
    process.ASSERT_LICENSED()

    debug.forBot(incomingEvent.botId, 'run action', { actionName, incomingEvent, actionArgs })

    const action = await this.findAction(actionName)
    const code = await this.getActionScript(action)
    const api = await createForAction()

    // I added this variable to inspect it in debug mode
    const checkConfig = api.config.getModuleConfig('examples');
    

    const botFolder = action.location === 'global' ? 'global' : 'bots/' + this.botId
    const dirPath = path.resolve(path.join(process.PROJECT_LOCATION, `/data/${botFolder}/actions/${actionName}.js`))

    const _require = this._prepareRequire(path.dirname(dirPath))

    const modRequire = new Proxy(
      {},
      {
        get: (_obj, prop) => _require(prop)
      }
    )

    const vm = new NodeVM({
      wrapper: 'none',
      sandbox: {
        bp: api,
        event: incomingEvent,
        user: incomingEvent.state.user,
        temp: incomingEvent.state.temp,
        session: incomingEvent.state.session,
        args: actionArgs,
        printObject: printObject,
        process: UntrustedSandbox.getSandboxProcessArgs()
      },
      require: {
        external: true,
        mock: modRequire
      },
      timeout: 5000
    })

    const runner = new VmRunner()

    try {
      const result = await runner.runInVm(vm, code, dirPath)
      debug.forBot(incomingEvent.botId, 'done running', { result, actionName, actionArgs })

      return result
    } catch (err) {
      this.logger
        .forBot(this.botId)
        .attachError(err)
        .error(`An error occurred while executing the action "${actionName}`)
      throw new ActionExecutionError(err.message, actionName, err.stack)
    }
  }

And this is the debug output:

Promise [Object] {
  _bitField: 67108864,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0:
   Promise [Object] {
     _bitField: 0,
     _fulfillmentHandler0: undefined,
     _rejectionHandler0: undefined,
     _promise0: undefined,
     _receiver0: undefined },
  _promise0: undefined,
  _receiver0: undefined }

As I could see, no openWeatherApiKey property at all.

Could you please clarify for me, where to set custom module variables?

And I finally found the answer and created PR with it :slight_smile: : https://github.com/botpress/botpress/pull/2487

Hey @titusjaka ,

Thanks a lot for your PR, this is amazing! We always love seeing contributions from our community. Keep them coming! :mechanical_arm:

Have a good day :smile_cat: