欢迎消息 Microsoft 团队扩展

问题描述 投票:0回答:1

当用户首次在 Microsoft Teams 中添加机器人时,我尝试发送欢迎消息。我无法做到这一点。添加机器人的步骤如下:

这就是我希望应用程序执行的操作:

这是我正在使用的代码:

import { TeamsActivityHandler, TurnContext, ChannelAccount, ActivityTypes } from "botbuilder";

export class TeamsBot extends TeamsActivityHandler {
  constructor() {
    super();
    const card = {
      contentType: 'application/vnd.microsoft.card.adaptive',
      content: {
        type: 'AdaptiveCard',
        body: [
          {
            type: 'TextBlock',
            text: 'Thank you for installing me!',
            weight: 'bolder',
            size: 'medium'
          }
        ]
      }
    };
    // Bind the overridden onMembersAdded method
    this.onMembersAdded(this.onMembersAddedActivity.bind(this));
    // Bind the overridden onConversationUpdate method
    this.onConversationUpdate(this.onConversationUpdateActivity.bind(this));
  
  }

  /**
   * Overrides the onMembersAdded method from TeamsActivityHandler.
   * @param membersAdded List of members added to the conversation.
   * @param context The context object for this turn.
   */
  protected async onMembersAddedActivity(membersAdded: ChannelAccount[], context: TurnContext): Promise<void> {
    await context.sendActivity('Thank you for installing me!');
    console.log('onMembersAddedActivity')
    // Create a adaptive card and send it to the conversation when the bot is added to the conversation.
    const card = {
      contentType: 'application/vnd.microsoft.card.adaptive',
      content: {
        type: 'AdaptiveCard',
        body: [
          {
            type: 'TextBlock',
            text: 'Thank you for installing me!',
            weight: 'bolder',
            size: 'medium'
          }
        ]
      }
    };
    await context.sendActivity({ attachments: [card] });
  }

  protected async onConversationUpdateActivity(context: TurnContext): Promise<void> {
    const card = {
      contentType: 'application/vnd.microsoft.card.adaptive',
      content: {
        type: 'AdaptiveCard',
        body: [
          {
            type: 'TextBlock',
            text: 'Thank you for installing me!',
            weight: 'bolder',
            size: 'medium'
          }
        ]
      }
    };

    await context.sendActivity({ attachments: [card] });
    console.log('onConversationUpdateActivity')
    if (context.activity.type === ActivityTypes.ConversationUpdate) {
      // Check if the bot is the only member added
      if (context.activity.membersAdded?.length === 1 && context.activity.membersAdded[0].id === context.activity.recipient.id) {
        await context.sendActivity('Thank you for installing me!');
      }
    }
  }
  
}

关于如何实现这一目标或我在这里缺少的任何内容有什么建议吗?谢谢。

microsoft-teams teams-toolkit
1个回答
0
投票

请检查以下代码的更新版本:

import { TeamsActivityHandler, TurnContext, ChannelAccount, ActivityTypes } from "botbuilder";

export class TeamsBot extends TeamsActivityHandler {
  constructor() {
    super();

    // Bind the overridden onMembersAdded method
    this.onMembersAdded(this.onMembersAddedActivity.bind(this));
  }

  protected async onMembersAddedActivity(membersAdded: ChannelAccount[], context: TurnContext): Promise<void> {
    // Check if the bot is the only member added
    if (context.activity.membersAdded?.length === 1 && context.activity.membersAdded[0].id === context.activity.recipient.id) {
      // Send a welcome message
      await context.sendActivity('Thank you for installing me!');

      // Create an adaptive card
      const card = {
        contentType: 'application/vnd.microsoft.card.adaptive',
        content: {
          type: 'AdaptiveCard',
          body: [
            {
              type: 'TextBlock',
              text: 'Thank you for installing me!',
              weight: 'bolder',
              size: 'medium'
            }
          ]
        }
      };

      // Send the adaptive card as an attachment
      await context.sendActivity({ attachments: [card] });
    }
  }
}

请同时参考这些发送欢迎消息的示例

  1. https://github.com/OfficeDev/Microsoft-Teams-Samples/blob/b911df910ddff73cd1a7b464bff37ffcfdfe8530/samples/bot-ai-enterprise-search/nodejs/teamsBot.ts#L130
  2. https://github.com/OfficeDev/Microsoft-Teams-Samples/blob/b911df910ddff73cd1a7b464bff37ffcfdfe8530/samples/bot-all-cards/csharp/BotAllCards/Bots/TeamsBot.cs#L36
© www.soinside.com 2019 - 2024. All rights reserved.