1

We've been using Botbuilder.js version 4.15.0 for our Teams Bot.

When user invoke task module using task/fetch, Bot calls handleTeamsTaskModuleFetch handler and respond with 501 because invokeResponse is undefined (see botFrameworkAdapter) and right after that it tries to respond with 200 and req.body with actual task which leads to this error:

UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Messenger Bot:

export class MessengerBot extends TeamsActivityHandler {
  constructor() {
    super();
    this.onMessage(async (context, next) => {
      this.handleMessage(context);
      await next();
    });
  }

  async handleMessage(context: TurnContext) {
    const activity = MessageFactory.attachment(CardFactory.adaptiveCard({
      $schema: 'http://adaptivecards.io/schemas/adaptive-card.json',
      version: '1.5',
      type: 'AdaptiveCard',
      body: [
        {
          type: 'TextBlock',
          text: 'Task Module Invocation from Adaptive Card',
          weight: 'bolder',
          size: 3
        }
      ],
      actions: [{
        type: 'Action.Submit',
        title: 'Open',
        data: {
          msteams: { type: 'task/fetch' },
          data: {
            foo: 'bar'
          }
        }
      }]
    }));
    activity.summary = 'Open URL';
    await context.sendActivity(activity);
  }

  async handleTeamsTaskModuleFetch(context: TurnContext, taskModuleRequest: TaskModuleRequest): Promise<TaskModuleResponse> {
    const url = 'https://some valid domain to navigate to'; // Domain included in "validDomains" and use HTTPS
    return {
      task: {
        type: 'continue',
        value: {
          title: 'Create Ticket',
          height: 700,
          width: 700,
          fallbackUrl: url,
          url: url
        },
      },
    };
  }

  async handleTeamsTaskModuleSubmit(context: TurnContext, taskModuleRequest: TaskModuleRequest): Promise<TaskModuleResponse> {
    return {
      task: {
        type: 'message',
        value: 'Thanks!'
      }
    };
  }
}

How can I find out the cause of this error? Appreciate any ideas!

  • The "Error: Can't set headers after they are sent." means that you're already in the Body or Finished state, but some function tried to set a header or statusCode. When you see this error, try to look for anything that tries to send a header after some of the body has already been written. Please check the following threads where similar issue has been reported - https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client, https://stackoverflow.com/questions/52122272/err-http-headers-sent-cannot-set-headers-after-they-are-sent-to-the-client – Meghana-MSFT Feb 28 '22 at 17:40
  • Yes, thanks for your help. The problem was `processActivity` method was called twice. – Nazim Kurtametov Mar 07 '22 at 06:31

0 Answers0