我正在使用 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 组件时,这条消息会在用户发起对话时发送,即当用户输入内容并将其发送给机器人时。这不是我想要的,而是我希望在加载 Web 聊天控件后立即从机器人显示消息,因为消息将包含有关如何使用机器人的一些说明。

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

    最佳答案

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

    以下是网络聊天端的实现示例:

    <!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

    关于c# - 加载 Webchat 控件后立即从机器人发送问候/欢迎消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50982168/

    10-17 01:00