【问题标题】:How do I send a 1:1 welcome message?如何发送 1:1 欢迎信息?
【发布时间】:2019-04-05 05:50:26
【问题描述】:

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

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

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

谢谢

【问题讨论】:

    标签: botframework microsoft-teams


    【解决方案1】:

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

    These tables 应该相当准确地反映渠道活动的当前状态。

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

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

    • OnConversationUpdate
    • OnTurn
    • 已添加 OnMembers
    • 打开对话框

    将机器人安装到频道时(群组欢迎消息):

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

    • OnTurn
    • OnConversationUpdate
    • 已添加 OnMembers
    • 打开对话框

    当机器人收到消息时:

    • OnTurn
    • OnMessage
    • 打开对话框

    这是我用来测试的代码(来自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# 也一样。

    【讨论】:

    • 当我第一次创建应用程序并添加它时,这似乎工作得很好。有没有办法在卸载和重新安装应用程序时收到通知,或者我应该只在第一次添加我的应用程序时期待一个事件?感谢您的帮助!
    • 不幸的是,您几乎必须在 Azure 中创建一个新的 Web App Bot 才能获得一个新的 AppId。请参阅来自 MS Teams 团队的 Wajeed 的this answer
    • ...我在测试时也遇到了同样的问题。我没有每次都制作新的 Web App Bot,而是让我的同事尝试安装它。如果您遇到更容易的事情,请告诉我!我没有使用 Team scope 进行测试,就像 Wajeed 在那个答案中提到的那样。
    • 只是为了更新这个问题,如果有人想在将来测试他们的欢迎消息而不必使用不同的用户安装机器人,可以使用 Bot Framework Emulator 开始新的对话每次都会收到欢迎信息。
    猜你喜欢
    • 2019-04-04
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 2021-10-07
    相关资源
    最近更新 更多