【问题标题】:Sending a greeting/welcome message from the bot as soon as the Webchat control is loaded加载 Webchat 控件后立即从机器人发送问候/欢迎消息
【发布时间】:2018-06-22 06:46:48
【问题描述】:

我正在使用 Microsoft 的 C# Bot 框架开发一个机器人。我正在尝试在用户发送任何内容之前向用户发送欢迎消息作为介绍。

经过研究,我使用HandleSystemMessage函数在一定程度上实现了这一点,并在ConversationUpdate的情况下发送消息如下:

if (activity.Type == ActivityTypes.ConversationUpdate) 
{
    IConversationUpdateActivity update = activity;
    if (update.MembersAdded.Any())
    {
        foreach (var newMember in update.MembersAdded)
        {
            if (newMember.Id != activity.Recipient.Id)
            {
                ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

                Activity reply = activity.CreateReply();
                reply.Text("Hello, how can I help you?");
                await connector.Conversations.ReplyToActivityAsync((Activity)bubble);
            }
        }
    }
}

我使用这种方法面临的问题:

  • 在模拟器中,当您点击上面的刷新按钮或用户开始输入时(如果它已经空闲了一段时间)会出现欢迎消息。这是我正在寻找的行为,它按预期工作。
  • 现在,当使用 Bot Framework Web Chat 组件时,当用户发起对话时发送此消息,即当用户键入内容并将其发送给机器人时。这不是我想要的,而是我希望在加载网络聊天控件后立即从机器人显示消息,因为该消息将包含有关如何使用机器人的一些说明。

我认为我的问题可以使用另一个 ActivityType 或者一些 Javascript 'hacky' 方式来解决,但我直到现在才找到解决方案。

【问题讨论】:

  • 尽管您最初发表了评论,但这仍然是 stackoverflow.com/questions/50363035/… 的副本;-)
  • 嗯,你是对的。我没有在寻找正确的标签/词,这就是为什么这个问题从未出现过。

标签: c# botframework


【解决方案1】:

对于网络聊天组件,您可以使用反向通道功能向您的机器人发送隐藏消息,以启动问候。

这里是网络聊天端的实现示例:

<!DOCTYPE html>
<html>
<head>
    <link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
</head>
<body>
    <div id="bot" />
    <script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
    <script>
        // Get parameters from query
        const params = BotChat.queryParams(location.search);
        // Language definition
        var chatLocale = params['locale'] || window.navigator.language;

        // Connection settings
        const botConnectionSettings = new BotChat.DirectLine({
            domain: params['domain'],
            secret: 'YOUR_SECRET',
            webSocket: params['webSocket'] && params['webSocket'] === 'true'
        });

        // Webchat init
        BotChat.App({
            botConnection: botConnectionSettings,
            user: { id: 'userid' },
            bot: { id: 'botid' },
            locale: chatLocale,
            resize: 'detect'
        }, document.getElementById('bot'));

        // Send hidden message to do what you want
        botConnectionSettings.postActivity({
            type: 'event',
            from: { id: 'userid' },
            locale: chatLocale,
            name: 'myCustomEvent',
            value: 'test'
        }).subscribe(function (id) { console.log('event sent'); });
    </script>
</body>
</html>

在您的机器人端,您将在 Message Controlelr 上收到此事件:

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
    // DEMO PURPOSE: echo all incoming activities
    Activity reply = activity.CreateReply(Newtonsoft.Json.JsonConvert.SerializeObject(activity, Newtonsoft.Json.Formatting.None));

    var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
    connector.Conversations.SendToConversation(reply);

    // Process each activity
    if (activity.Type == ActivityTypes.Message)
    {
        await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
    }
    // Webchat: getting an "event" activity for our js code
    else if (activity.Type == ActivityTypes.Event && activity.ChannelId == "webchat")
    {
        var receivedEvent = activity.AsEventActivity();

        if ("myCustomEvent".Equals(receivedEvent.Name, StringComparison.InvariantCultureIgnoreCase))
        {
            // DO YOUR GREETINGS FROM HERE
        }
    }
    // Sample for Skype: in ContactRelationUpdate event
    else if (activity.Type == ActivityTypes.ContactRelationUpdate && activity.ChannelId == "skype")
    {
        // DO YOUR GREETINGS FROM HERE
    }
    // Sample for emulator, to debug locales
    else if (activity.Type == ActivityTypes.ConversationUpdate && activity.ChannelId == "emulator")
    {
        foreach (var userAdded in activity.MembersAdded)
        {
            if (userAdded.Id == activity.From.Id)
            {
                // DO YOUR GREETINGS FROM HERE
            }
        }
    }

    var response = Request.CreateResponse(HttpStatusCode.OK);
    return response;
}

我使用这个功能做了一个工作演示来发送用户语言环境,它是 Github 上的here

【讨论】:

  • Github上的demo代码也很方便,谢谢!
【解决方案2】:

这是网络聊天中的一个已知问题。

使用 WebChat 或 directline,机器人的 ConversationUpdate 在创建对话时发送,而用户的 ConversationUpdate 在他们第一次发送消息时发送。 [1]

解决方法是通过网络聊天向机器人发送消息。

<!DOCTYPE html>

<script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
<script>

    var user = {
        id: 'user-id',
        name: 'user name'
    };

    var botConnection = new BotChat.DirectLine({
        token: '[DirectLineSecretHere]',
        user: user
    });

    BotChat.App({
        user: user,
        botConnection: botConnection,
        bot: { id: 'bot-id', name: 'bot name' },
        resize: 'detect'
    }, document.getElementById("bot"));

    botConnection
        .postActivity({
            from: user,
            name: 'requestWelcomeDialog',
            type: 'event',
            value: ''
        })
        .subscribe(function (id) {
            console.log('"trigger requestWelcomeDialog" sent');
        });
</script>

[2]

我没有更改我的机器人代码中的任何内容来实现这一点。

【讨论】:

    猜你喜欢
    • 2018-02-07
    • 2021-02-22
    • 1970-01-01
    • 2022-01-23
    • 2020-05-18
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多