OfficeDev / teams-toolkit

Developer tools for building Teams apps
Other
457 stars 187 forks source link

Update bot templates to stop sending a message on error, when the activity is not a message directed to it #10865

Open aosolis opened 8 months ago

aosolis commented 8 months ago

Describe the bug We have a problem in Teams with bots replying to an invoke or conversationUpdate activity unexpectedly, which shows up as spam messages to the General channel. One source of this spam are bots that indiscriminately reply with an error message in their error handler.

Could we update the TTK templates to handle this error condition correctly? Patterns like https://github.com/OfficeDev/TeamsFx/blob/da2c454a281ee75ae00206915a3caf4a70319077/templates/ts/default-bot/index.ts#L47 are problematic when the error happens in the context of an activity that's not a message directed at the bot.

To Reproduce Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

VS Code Extension Information (please complete the following information):

CLI Information (please complete the following information):

Additional context Add any other context about the problem here.

microsoft-github-policy-service[bot] commented 8 months ago

Thank you for contacting us! Any issue or feedback from you is quite important to us. We will do our best to fully respond to your issue as soon as possible. Sometimes additional investigations may be needed, we will usually get back to you within 2 days by adding comments to this issue. Please stay tuned.

therealjohn commented 8 months ago

@aosolis and I chatted about this and a message type check works well. The code can be improved like this:

const onTurnErrorHandler = async (context, error) => {  
     console.error(`\n [onTurnError] unhandled error: ${error}`);

    // Only send error message for user messages, not for other message types so the bot doesn't spam a channel or chat.
    if(context.activity.type === 'message') {
        // Send a trace activity, which will be displayed in Bot Framework Emulator
        await context.sendTraceActivity(
          "OnTurnError Trace",
          `${error}`,
          "https://www.botframework.com/schemas/error",
          "TurnError"
        );

        // Send a message to the user
        await context.sendActivity("The bot encountered an error or bug.");
    }
};

With this change an error message outputs as a message only when a user is messaging the bot. Other activity types like conversationUpdate, invoke, etc. won't result in an error in the chat.

microsoft-github-policy-service[bot] commented 7 months ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 7 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

microsoft-github-policy-service[bot] commented 7 months ago

Due to lack of details for further investigation, we will archive the issue for now. In case you still have following-up questions on this issue, please always feel free to reopen the issue by clicking ‘reopen issue’ button below the comment box. We will get back to you as soon as possible.

qinezh commented 7 months ago

@aosolis the bot template is updated in latest Teams Toolkit (v5.6.0), please feel free to have a try😄 (For C#, the bot template changes will be released along with VS 17.10 Preview 3)