如何发送1:1欢迎信息?

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

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

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

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

谢谢

botframework microsoft-teams
1个回答
6
投票

文档可能存在冲突,因为MS团队团队在实现所有botframework功能方面取得了非常快的进展。我们也对activity handlers进行了一些非常大的改动 - 我个人不知道这些具体的改变是否能够让机器人接收到Teams ConversationUpdate,或者是否因为其他原因而起作用。

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

我刚刚测试了一个团队机器人,它通过几个场景捕获每个活动,这里是处理程序触发的活动:

当用户第一次添加机器人时(1:1欢迎消息):

  • OnConversationUpdate
  • OnTurn
  • OnMembersAdded
  • OnDialog

当Bot安装到通道(组欢迎消息)时:

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

  • OnTurn
  • OnConversationUpdate
  • OnMembersAdded
  • OnDialog

当Bot被传递时:

  • OnTurn
  • 的onMessage
  • OnDialog

这是我用来测试它的代码(来自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)后停止。

Sending a 1:1 Welcome Message

这样的事情应该有效(来自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#也是如此。

© www.soinside.com 2019 - 2024. All rights reserved.