botframework - 如何发送 1 :1 welcome message?

标签 botframework microsoft-teams

我希望在用户安装我的 Teams 机器人时发送欢迎消息。

我查看了 Teams API 文档,并收到了关于这是否应该可行的混合消息。我已经在不同的地方读到我的机器人在安装机器人时应该收到一个对话更新,以及在各种问题中我不会收到这样的事件。

但是,存在具有此功能的机器人。 Hipmunk,当以私有(private)范围安装时,会向我发送一条消息而不会被进一步激怒。这个机器人是如何做到这一点的,我该如何复制这个功能?

谢谢

最佳答案

文档可能会发生冲突,因为 MS Teams 团队在实现所有 botframework 功能方面取得了非常快的进展。我们还对 activity handlers 进行了一些相当大的更改。 --我个人不知道这些特定的更改是否使机器人可以接收 Teams ConversationUpdate 或者它是否因为其他原因而工作。

These tables应该按 channel 相当准确地反射(reflect)事件的当前状态。

我刚刚测试了一个 Teams 机器人,它通过几个场景捕获每个事件,以下是事件处理程序触发的内容:

当用户首次添加机器人时(1:1 欢迎信息):

  • 对话更新
  • OnTurn
  • OnMembers 已添加
  • 对话

  • 当 Bot 安装到 channel 时(群组欢迎消息):

    注意:这些 应该 当用户被添加到机器人已经存在的团队(而不是团队内的 channel )时也会触发,但我无法对此进行测试。
  • OnTurn
  • 对话更新
  • OnMembers 已添加
  • 对话

  • 当机器人收到消息时:
  • OnTurn
  • 在线留言
  • 对话

  • 这是我用来测试的代码(来自 bot.ts ,由 Echo Bot Sample 构建):

    import { ActivityHandler, MessageFactory, TurnContext } from 'botbuilder';
    
    export class MyBot extends ActivityHandler {
        constructor() {
            super();
            // See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
            this.onTurn(async (turnContext, next) => { await this.sendTeamsMessage('onTurn', turnContext); await next();});
            this.onMembersAdded(async (turnContext, next) => { await this.sendTeamsMessage('onMembersAdded', turnContext); await next();});
            this.onMembersRemoved(async (turnContext, next) => { await this.sendTeamsMessage('onMembersRemoved', turnContext); await next();});
            this.onEvent(async (turnContext, next) => { await this.sendTeamsMessage('onEvent', turnContext); await next();});
            this.onConversationUpdate(async (turnContext, next) => { await this.sendTeamsMessage('onConversationUpdate', turnContext); await next();});
            this.onMessage(async (turnContext, next) => { await this.sendTeamsMessage('onMessage', turnContext); await next();});
            this.onTokenResponseEvent(async (turnContext, next) => { await this.sendTeamsMessage('onTokenResponseEvent', turnContext); await next();});
            this.onUnrecognizedActivityType(async (turnContext, next) => { await this.sendTeamsMessage('onUnrecognizedActivityType', turnContext); await next();});
            this.onDialog(async (turnContext, next) => { await this.sendTeamsMessage('onDialog', turnContext); await next();});
        }
    
        private sendTeamsMessage = async (activityHandlerName: string, turnContext: TurnContext) => {
    
            const message = MessageFactory.text(`**[${activityHandlerName}]** event received`);
    
            await turnContext.sendActivity(message);
            console.log(`Sent: ${message.text}`)
        }
    }
    

    注:await next()确保可以为给定的事件调用所有适当的事件处理程序,而不是在调用第一个事件 (onTurn) 后停止。

    发送 1:1 欢迎信息

    像这样的东西应该可以工作(来自 the Core Bot Sample ):

    this.onMembersAdded(async (context) => {
        const membersAdded = context.activity.membersAdded;
        for (const member of membersAdded) {
            if (member.id !== context.activity.recipient.id) {
                const welcomeCard = CardFactory.adaptiveCard(WelcomeCard);
                await context.sendActivity({ attachments: [welcomeCard] });
            }
        }
    });
    

    我们正在使用新的事件处理程序编写示例,但您可以 comb through this Samples Branch得到一些想法。我用 TypeScript 写了我的,但它可以工作并且 there are samples in C# , 也。

    关于botframework - 如何发送 1 :1 welcome message?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55517906/

    相关文章:

    node.js - Azure 函数的 Node 和 DirectlineJS es6 导出的版本兼容性问题

    botframework - Azure BOT 框架,将 QnA Maker 与 LUIS 集成

    python - 我从 Graph API 得到不完整的输出

    azure-devops - 适用于 Azure DevOps 的 Microsoft Teams 连接器不可用

    powershell - 如何将联系人迁移到 Teams?

    botframework - 我们是否可以构建一个产品,使最终用户能够在自托管环境中使用机器人框架创建 session 聊天机器人?

    node.js - Azure Web 聊天测试显示 "There was an error sending this message to your bot: HTTP status code Unauthorized"

    c# - 如何使用SDK V4在C#中修改WEBCHAT中BOT和USER输入的UI?

    json - 发布到 Microsoft Teams 连接器 Webhook 时,消息可以持续多长时间?

    python - MS Teams 中的主动消息传递