public async openBot()

in packages/app/main/src/protocolHandler.ts [239:315]


  public async openBot(protocol: Protocol): Promise<void> {
    const { path, secret }: { path: string; secret: string } = protocol.parsedArgs;

    const endpointOverrides: Partial<IEndpointService> = parseEndpointOverrides(protocol.parsedArgs);
    const overrides: BotConfigOverrides = endpointOverrides ? { endpoint: endpointOverrides } : null;

    let bot: BotConfigWithPath;
    try {
      bot = (await this.commandService.call(SharedConstants.Commands.Bot.Open, path, secret)) as BotConfigWithPath;
      if (!bot) {
        throw new Error(
          `Error occurred while trying to open bot at ${path} inside of protocol handler: Bot is invalid.`
        );
      }
    } catch (e) {
      throw new Error(`Error occurred while trying to open bot at ${path} inside of protocol handler: ${e}`);
    }

    // apply any overrides
    if (overrides) {
      bot = applyBotConfigOverrides(bot, overrides);
    }

    const appSettings: FrameworkSettings = getSettings().framework;
    if (appSettings.ngrokPath) {
      const ngrok = Emulator.getInstance().ngrok;
      let ngrokSpawnStatus = ngrok.getSpawnStatus();
      // if ngrok hasn't spawned yet, we need to start it up
      if (!ngrokSpawnStatus.triedToSpawn) {
        await ngrok.recycle();
      }
      ngrokSpawnStatus = ngrok.getSpawnStatus();
      if (ngrokSpawnStatus.triedToSpawn && ngrokSpawnStatus.err) {
        throw new Error(`Error while trying to spawn ngrok instance: ${ngrokSpawnStatus.err || ''}`);
      }

      if (ngrok.running) {
        try {
          await this.commandService.call(SharedConstants.Commands.Bot.SetActive, bot);
          await this.commandService.remoteCall(SharedConstants.Commands.Bot.Load, bot);
        } catch (e) {
          throw new Error(`(ngrok running) Error occurred while trying to deep link to bot project at ${path}: ${e}`);
        }
      } else {
        // if ngrok hasn't connected yet, wait for it to connect and load the bot
        ngrok.ngrokEmitter.once(
          'connect',
          async (...args: any[]): Promise<void> => {
            try {
              await this.commandService.call(SharedConstants.Commands.Bot.SetActive, bot);
              await this.commandService.remoteCall(SharedConstants.Commands.Bot.Load, bot);
            } catch (e) {
              throw new Error(
                `(ngrok running but not connected) Error occurred while ` +
                  `trying to deep link to bot project at ${path}: ${e}`
              );
            }
          }
        );
      }
    } else {
      try {
        await this.commandService.call(SharedConstants.Commands.Bot.SetActive, bot);
        await this.commandService.remoteCall(SharedConstants.Commands.Bot.Load, bot);
      } catch (e) {
        throw new Error(
          `(ngrok not configured) Error occurred while trying to deep link to bot project at ${path}: ${e}`
        );
      }
    }
    const numOfServices = bot.services && bot.services.length;
    TelemetryService.trackEvent('bot_open', {
      method: 'protocol',
      numOfServices,
      source: 'path',
    });
  }