c# - 带有 v4 Webchat channel 的 Bot 框架问候消息

标签 c# azure botframework chatbot web-chat

我正在尝试在网络加载且聊天机器人初始化时发送问候消息。 它可以与模拟器一起使用,但似乎不能直接在网络聊天 channel 上工作。

我研究并发现了一些有用的链接,但我遗漏了一些东西......

[BotFramework]: How to fix:Welcome message is not getting displayed to the user in C# WebChatBot developed in V4 but displayed in Emulator?

Display Welcome Message in v4 Bot Framework Bot (C# + .Net Core Web Application)

https://github.com/microsoft/BotFramework-WebChat/issues/1397

到目前为止,这是我的代码:

default.htm

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Core Bot Sample</title>
    <style>
        body {
            margin: 0px;
            padding: 0px;
            font-family: Segoe UI;
        }

        html,
        body {
            height: 100%;
        }

        header {
            background-image: url("data:image/svg+xml,%3Csvg version='1.1' id='Layer_1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' viewBox='0 0 4638.9 651.6' style='enable-background:new 0 0 4638.9 651.6;' xml:space='preserve'%3E%3Cstyle type='text/css'%3E .st0%7Bfill:%2355A0E0;%7D .st1%7Bfill:none;%7D .st2%7Bfill:%230058A8;%7D .st3%7Bfill:%23328BD8;%7D .st4%7Bfill:%23B6DCF1;%7D .st5%7Bopacity:0.2;fill:url(%23SVGID_1_);enable-background:new ;%7D%0A%3C/style%3E%3Crect y='1.1' class='st0' width='4640' height='646.3'/%3E%3Cpath class='st1' d='M3987.8,323.6L4310.3,1.1h-65.6l-460.1,460.1c-17.5,17.5-46.1,17.5-63.6,0L3260.9,1.1H0v646.3h3660.3 L3889,418.7c17.5-17.5,46.1-17.5,63.6,0l228.7,228.7h66.6l-260.2-260.2C3970.3,369.8,3970.3,341.1,3987.8,323.6z'/%3E%3Cpath class='st2' d='M3784.6,461.2L4244.7,1.1h-983.9l460.1,460.1C3738.4,478.7,3767.1,478.7,3784.6,461.2z'/%3E%3Cpath class='st3' d='M4640,1.1h-329.8l-322.5,322.5c-17.5,17.5-17.5,46.1,0,63.6l260.2,260.2H4640L4640,1.1L4640,1.1z'/%3E%3Cpath class='st4' d='M3889,418.8l-228.7,228.7h521.1l-228.7-228.7C3935.2,401.3,3906.5,401.3,3889,418.8z'/%3E%3ClinearGradient id='SVGID_1_' gradientUnits='userSpaceOnUse' x1='3713.7576' y1='438.1175' x2='3911.4084' y2='14.2535' gradientTransform='matrix(1 0 0 -1 0 641.3969)'%3E%3Cstop offset='0' style='stop-color:%23FFFFFF;stop-opacity:0.5'/%3E%3Cstop offset='1' style='stop-color:%23FFFFFF'/%3E%3C/linearGradient%3E%3Cpath class='st5' d='M3952.7,124.5c-17.5-17.5-46.1-17.5-63.6,0l-523,523h1109.6L3952.7,124.5z'/%3E%3C/svg%3E%0A");
            background-repeat: no-repeat;
            background-size: 100%;
            background-position: right;
            background-color: #55A0E0;
            width: 100%;
            font-size: 44px;
            height: 120px;
            color: white;
            padding: 30px 0 40px 0px;
            display: inline-block;
        }

        .header-icon {
            background-image: url("data:image/svg+xml;utf8,%3Csvg%20version%3D%221.1%22%20id%3D%22Layer_1%22%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20xmlns%3Axlink%3D%22http%3A//www.w3.org/1999/xlink%22%20x%3D%220px%22%20y%3D%220px%22%0A%09%20viewBox%3D%220%200%20150.2%20125%22%20style%3D%22enable-background%3Anew%200%200%20150.2%20125%3B%22%20xml%3Aspace%3D%22preserve%22%3E%0A%3Cstyle%20type%3D%22text/css%22%3E%0A%09.st0%7Bfill%3Anone%3B%7D%0A%09.st1%7Bfill%3A%23FFFFFF%3B%7D%0A%3C/style%3E%0A%3Crect%20x%3D%220.5%22%20class%3D%22st0%22%20width%3D%22149.7%22%20height%3D%22125%22/%3E%0A%3Cg%3E%0A%09%3Cpath%20class%3D%22st1%22%20d%3D%22M59%2C102.9L21.8%2C66c-3.5-3.5-3.5-9.1%2C0-12.5l37-36.5l2.9%2C3l-37%2C36.4c-1.8%2C1.8-1.8%2C4.7%2C0%2C6.6l37.2%2C37L59%2C102.9z%22%0A%09%09/%3E%0A%3C/g%3E%0A%3Cg%3E%0A%09%3Cpath%20class%3D%22st1%22%20d%3D%22M92.5%2C102.9l-3-3l37.2-37c0.9-0.9%2C1.4-2%2C1.4-3.3c0-1.2-0.5-2.4-1.4-3.3L89.5%2C20l2.9-3l37.2%2C36.4%0A%09%09c1.7%2C1.7%2C2.6%2C3.9%2C2.6%2C6.3s-0.9%2C4.6-2.6%2C6.3L92.5%2C102.9z%22/%3E%0A%3C/g%3E%0A%3Cg%3E%0A%09%3Cpath%20class%3D%22st1%22%20d%3D%22M90.1%2C68.4c-4.5%2C0-8-3.5-8-8.1c0-4.5%2C3.5-8.1%2C8-8.1c4.4%2C0%2C8%2C3.7%2C8%2C8.1C98.1%2C64.7%2C94.4%2C68.4%2C90.1%2C68.4z%0A%09%09%20M90.1%2C56.5c-2.2%2C0-3.8%2C1.7-3.8%2C3.9c0%2C2.2%2C1.7%2C3.9%2C3.8%2C3.9c1.9%2C0%2C3.8-1.6%2C3.8-3.9S91.9%2C56.5%2C90.1%2C56.5z%22/%3E%0A%3C/g%3E%0A%3Cg%3E%0A%09%3Cpath%20class%3D%22st1%22%20d%3D%22M61.4%2C68.4c-4.5%2C0-8-3.5-8-8.1c0-4.5%2C3.5-8.1%2C8-8.1c4.4%2C0%2C8%2C3.7%2C8%2C8.1C69.5%2C64.7%2C65.8%2C68.4%2C61.4%2C68.4z%0A%09%09%20M61.4%2C56.5c-2.2%2C0-3.8%2C1.7-3.8%2C3.9c0%2C2.2%2C1.7%2C3.9%2C3.8%2C3.9c1.9%2C0%2C3.8-1.6%2C3.8-3.9S63.3%2C56.5%2C61.4%2C56.5z%22/%3E%0A%3C/g%3E%0A%3C/svg%3E%0A");
            background-repeat: no-repeat;
            float: left;
            height: 140px;
            width: 140px;
            display: inline-block;
            vertical-align: middle;
        }

        .header-text {
            padding-left: 1%;
            color: #FFFFFF;
            font-family: "Segoe UI";
            font-size: 72px;
            font-weight: 300;
            letter-spacing: 0.35px;
            line-height: 96px;
            display: inline-block;
            vertical-align: middle;
        }

        .header-inner-container {
            min-width: 480px;
            max-width: 1366px;
            margin-left: auto;
            margin-right: auto;
            vertical-align: middle;
        }

            .header-inner-container::after {
                content: "";
                clear: both;
                display: table;
            }

        .main-content-area {
            padding-left: 30px;
        }

        .content-title {
            color: #000000;
            font-family: "Segoe UI";
            font-size: 46px;
            font-weight: 300;
            line-height: 62px;
        }

        .main-text {
            color: #808080;
            font-size: 24px;
            font-family: "Segoe UI";
            font-size: 24px;
            font-weight: 200;
            line-height: 32px;
        }

        .main-text-p1 {
            padding-top: 48px;
            padding-bottom: 28px;
        }

        .endpoint {
            height: 32px;
            width: 571px;
            color: #808080;
            font-family: "Segoe UI";
            font-size: 24px;
            font-weight: 200;
            line-height: 32px;
            padding-top: 28px;
        }

        .how-to-build-section {
            padding-top: 20px;
            padding-left: 30px;
        }

            .how-to-build-section > h3 {
                font-size: 16px;
                font-weight: 600;
                letter-spacing: 0.35px;
                line-height: 22px;
                margin: 0 0 24px 0;
                text-transform: uppercase;
            }

        .step-container {
            display: flex;
            align-items: stretch;
            position: relative;
        }

            .step-container dl {
                border-left: 1px solid #A0A0A0;
                display: block;
                padding: 0 24px;
                margin: 0;
            }

                .step-container dl > dt::before {
                    background-color: white;
                    border: 1px solid #A0A0A0;
                    border-radius: 100%;
                    content: '';
                    left: 47px;
                    height: 11px;
                    position: absolute;
                    width: 11px;
                }

                .step-container dl > .test-bullet::before {
                    background-color: blue;
                }

                .step-container dl > dt {
                    display: block;
                    font-size: inherit;
                    font-weight: bold;
                    line-height: 20px;
                }

                .step-container dl > dd {
                    font-size: inherit;
                    line-height: 20px;
                    margin-left: 0;
                    padding-bottom: 32px;
                }

            .step-container:last-child dl {
                border-left: 1px solid transparent;
            }

        .ctaLink {
            background-color: transparent;
            border: 1px solid transparent;
            color: #006AB1;
            cursor: pointer;
            font-weight: 600;
            padding: 0;
            white-space: normal;
        }

            .ctaLink:focus {
                outline: 1px solid #00bcf2;
            }

            .ctaLink:hover {
                text-decoration: underline;
            }

        .step-icon {
            display: flex;
            height: 38px;
            margin-right: 15px;
            width: 38px;
        }

            .step-icon > div {
                height: 30px;
                width: 30px;
                background-repeat: no-repeat;
            }

        .ms-logo-container {
            min-width: 580px;
            max-width: 980px;
            margin-left: auto;
            margin-right: auto;
            left: 0;
            right: 0;
            transition: bottom 400ms;
        }

        .ms-logo {
            float: right;
            background-image: url("data:image/svg+xml;utf8,%0A%3Csvg%20version%3D%221.1%22%20id%3D%22MS-symbol%22%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20xmlns%3Axlink%3D%22http%3A//www.w3.org/1999/xlink%22%20x%3D%220px%22%20y%3D%220px%22%0A%09%20viewBox%3D%220%200%20400%20120%22%20style%3D%22enable-background%3Anew%200%200%20400%20120%3B%22%20xml%3Aspace%3D%22preserve%22%3E%0A%3Cstyle%20type%3D%22text/css%22%3E%0A%09.st0%7Bfill%3Anone%3B%7D%0A%09.st1%7Bfill%3A%23737474%3B%7D%0A%09.st2%7Bfill%3A%23D63F26%3B%7D%0A%09.st3%7Bfill%3A%23167D3E%3B%7D%0A%09.st4%7Bfill%3A%232E76BC%3B%7D%0A%09.st5%7Bfill%3A%23FDB813%3B%7D%0A%3C/style%3E%0A%3Crect%20x%3D%220.6%22%20class%3D%22st0%22%20width%3D%22398.7%22%20height%3D%22119%22/%3E%0A%3Cpath%20class%3D%22st1%22%20d%3D%22M171.3%2C38.4v43.2h-7.5V47.7h-0.1l-13.4%2C33.9h-5l-13.7-33.9h-0.1v33.9h-6.9V38.4h10.8l12.4%2C32h0.2l13.1-32H171.3%0A%09z%20M177.6%2C41.7c0-1.2%2C0.4-2.2%2C1.3-3c0.9-0.8%2C1.9-1.2%2C3.1-1.2c1.3%2C0%2C2.4%2C0.4%2C3.2%2C1.3c0.8%2C0.8%2C1.3%2C1.8%2C1.3%2C3c0%2C1.2-0.4%2C2.2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.7c-1.1%2C0.2-2.1%2C0.3-3.2%2C0.3c-3.1%2C0-5.4-0.8-6.9-2.5c-1.5-1.6-2.3-4.1-2.3-7.4%0A%09V56.6z%22/%3E%0A%3Cg%3E%0A%09%3Crect%20x%3D%2231%22%20y%3D%2224%22%20class%3D%22st2%22%20width%3D%2234.2%22%20height%3D%2234.2%22/%3E%0A%09%3Crect%20x%3D%2268.8%22%20y%3D%2224%22%20class%3D%22st3%22%20width%3D%2234.2%22%20height%3D%2234.2%22/%3E%0A%09%3Crect%20x%3D%2231%22%20y%3D%2261.8%22%20class%3D%22st4%22%20width%3D%2234.2%22%20height%3D%2234.2%22/%3E%0A%09%3Crect%20x%3D%2268.8%22%20y%3D%2261.8%22%20class%3D%22st5%22%20width%3D%2234.2%22%20height%3D%2234.2%22/%3E%0A%3C/g%3E%0A%3C/svg%3E%0A");
        }

        .ms-logo-container > div {
            min-height: 60px;
            width: 150px;
            background-repeat: no-repeat;
        }

        .row {
            padding: 90px 0px 0 20px;
            min-width: 480px;
            max-width: 1366px;
            margin-left: auto;
            margin-right: auto;
        }

        .column {
            float: left;
            width: 45%;
            padding-right: 20px;
        }

        .row:after {
            content: "";
            display: table;
            clear: both;
        }

        a {
            text-decoration: none;
        }

        .download-the-emulator {
            height: 20px;
            color: #0063B1;
            font-size: 15px;
            line-height: 20px;
            padding-bottom: 70px;
        }

        .how-to-iframe {
            max-width: 700px !important;
            min-width: 650px !important;
            height: 700px !important;
        }

        .remove-frame-height {
            height: 10px;
        }

        @media only screen and (max-width: 1300px) {
            .ms-logo {
                padding-top: 30px;
            }

            .header-text {
                font-size: 40x;
            }

            .column {
                float: none;
                padding-top: 30px;
                width: 100%;
            }

            .ms-logo-container {
                padding-top: 30px;
                min-width: 480px;
                max-width: 650px;
                margin-left: auto;
                margin-right: auto;
            }

            .row {
                padding: 20px 0px 0 20px;
                min-width: 480px;
                max-width: 650px;
                margin-left: auto;
                margin-right: auto;
            }
        }

        @media only screen and (max-width: 1370px) {
            header {
                background-color: #55A0E0;
                background-size: auto 200px;
            }
        }

        @media only screen and (max-width: 1230px) {
            header {
                background-color: #55A0E0;
                background-size: auto 200px;
            }

            .header-text {
                font-size: 44px;
            }

            .header-icon {
                height: 120px;
                width: 120px;
            }
        }

        @media only screen and (max-width: 1000px) {
            header {
                background-color: #55A0E0;
                background-image: none;
            }
        }

        @media only screen and (max-width: 632px) {
            .header-text {
                font-size: 32px;
            }

            .row {
                padding: 10px 0px 0 10px;
                max-width: 490px !important;
                min-width: 410px !important;
            }

            .endpoint {
                font-size: 25px;
            }

            .main-text {
                font-size: 20px;
            }

            .step-container dl > dd {
                font-size: 14px;
            }

            .column {
                padding-right: 5px;
            }

            .header-icon {
                height: 110px;
                width: 110px;
            }

            .how-to-iframe {
                max-width: 480px !important;
                min-width: 400px !important;
                height: 650px !important;
                overflow: hidden;
            }
        }

        .remove-frame-height {
            max-height: 10px;
        }
    </style>
</head>

<body>
    <header class="header">
        <div class="header-inner-container">
            <div class="header-icon" style="display: inline-block"></div>
            <div class="header-text" style="display: inline-block">Core Bot Sample</div>
        </div>
    </header>
    <div class="row">
        <div class="column" class="main-content-area">
            <div class="content-title">Your bot is ready!</div>
            <div class="main-text main-text-p1">
                You can test your bot in the Bot Framework Emulator<br />
                by connecting to http://localhost:3978/api/messages.
            </div>
            <div class="main-text download-the-emulator">
                <a class="ctaLink" href="https://aka.ms/bot-framework-F5-download-emulator"
                   target="_blank">Download the Emulator</a>
            </div>
            <div class="main-text">
                Visit <a class="ctaLink" href="https://aka.ms/bot-framework-F5-abs-home" target="_blank">
                    Azure
                    Bot Service
                </a> to register your bot and add it to<br />
                various channels. The bot's endpoint URL typically looks
                like this:
            </div>
            <div class="endpoint">https://<i>your_bots_hostname</i>/api/messages</div>

        </div>
        <div class="column how-to-iframe" id="how-to-iframe">

        </div>
    </div>
    <div class="ms-logo-container">
        <div class="ms-logo"></div>
    </div>
    <div id="botDiv" style="height:38px; position:fixed; bottom:0; z-index:1000; background:#fff">
        <div id="botTitleBar" style="height:38px; width:400px; position:fixed; cursor:pointer; background:blue; color:#fff; font-weight:bold;" onclick="toggleChatbot()">
            ChatBot
        </div>
        <iframe id="botFrame" style="height:600px; width:400px" src="https://webchat.botframework.com/embed/TestBotBasicCSharp?s=[KEY]">
        </iframe>
    </div>

    <!--<script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>-->
    <script>
        var toggleChatbot = function () {
            var botDiv = document.querySelector('#botDiv');
            botDiv.style.height = botDiv.style.height == '600px' ? '38px' : '600px';
        };

        //var user = {
        //    id: 'user-id',
        //    name: 'user name'
        //};
        //var botConnection = new BotChat.DirectLine({
        //    token: '[KEY]',
        //    user: user
        //});
        //botConnection
        //    .postActivity({
        //        from: user,
        //        name: 'requestWelcomeDialog',
        //        type: 'event',
        //        value: ''
        //    })
        //    .subscribe(function (id) {
        //        console.log('"trigger requestWelcomeDialog" sent');
        //    });

        document.addEventListener('DOMContentLoaded', function () {

        });

    </script>
</body>
</html>

</script>

DialogBot.cs

    public class DialogBot<T> : ActivityHandler
        where T : Dialog
    {
        protected readonly Dialog Dialog;
        protected readonly BotState ConversationState;
        protected readonly BotState UserState;
        protected readonly ILogger Logger;

        public DialogBot(ConversationState conversationState, UserState userState, T dialog, ILogger<DialogBot<T>> logger)
        {
            ConversationState = conversationState;
            UserState = userState;
            Dialog = dialog;
            Logger = logger;
        }

        public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
        {
            await base.OnTurnAsync(turnContext, cancellationToken);

            // Save any state changes that might have occured during the turn.
            await ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
            await UserState.SaveChangesAsync(turnContext, false, cancellationToken);
        }

        protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            Logger.LogInformation("Running dialog with Message Activity.");

            // Run the Dialog with the new message Activity.
            var welcomeUserStateAccessor = UserState.CreateProperty<WelcomeUserState>(nameof(WelcomeUserState));
            var didBotWelcomeUser = await welcomeUserStateAccessor.GetAsync(turnContext, () => new WelcomeUserState());

            if (didBotWelcomeUser.DidBotWelcomeUser == false)
            {
                didBotWelcomeUser.DidBotWelcomeUser = true;

                // the channel should sends the user name in the 'From' object
                var userName = turnContext.Activity.From.Name;

                await turnContext.SendActivityAsync($"You are seeing this message because this was your first message ever to this bot.", cancellationToken: cancellationToken);
                await turnContext.SendActivityAsync($"It is a good practice to welcome the user and provide personal greeting. For example, welcome {userName}.", cancellationToken: cancellationToken);
            }
            else
            {
                await Dialog.RunAsync(turnContext, ConversationState.CreateProperty<DialogState>("DialogState"), cancellationToken);
            }

            // Save any state changes.
            await UserState.SaveChangesAsync(turnContext);
        }
    }

DialogAndWelcomeBot.cs

    public class DialogAndWelcomeBot<T> : DialogBot<T>
        where T : Dialog
    {


        public DialogAndWelcomeBot(ConversationState conversationState, UserState userState, T dialog, ILogger<DialogBot<T>> logger)
            : base(conversationState, userState, dialog, logger)
        {
        }

        protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
        {
            foreach (var member in membersAdded)
            {
                // Greet anyone that was not the target (recipient) of this message.
                // To learn more about Adaptive Cards, see https://aka.ms/msbot-adaptivecards for more details.
                if (member.Id != turnContext.Activity.Recipient.Id)
                {
                    var welcomeCard = CreateAdaptiveCardAttachment();
                    var response = MessageFactory.Attachment(welcomeCard, ssml: "Welcome to Bot Framework!");
                    await turnContext.SendActivityAsync(response, cancellationToken);
                    await Dialog.RunAsync(turnContext, ConversationState.CreateProperty<DialogState>("DialogState"), cancellationToken);
                }
            }
        }

        // Load attachment from embedded resource.
        private Attachment CreateAdaptiveCardAttachment()
        {
            var cardResourcePath = "CoreBot.Cards.welcomeCard.json";

            using (var stream = GetType().Assembly.GetManifestResourceStream(cardResourcePath))
            {
                using (var reader = new StreamReader(stream))
                {
                    var adaptiveCard = reader.ReadToEnd();
                    return new Attachment()
                    {
                        ContentType = "application/vnd.microsoft.card.adaptive",
                        Content = JsonConvert.DeserializeObject(adaptiveCard),
                    };
                }
            }
        }
    }

BotController.cs

    [Route("api/messages")]
    [ApiController]
    public class BotController : ControllerBase
    {
        private readonly IBotFrameworkHttpAdapter Adapter;
        private readonly IBot Bot;

        public BotController(IBotFrameworkHttpAdapter adapter, IBot bot)
        {
            Adapter = adapter;
            Bot = bot;
        }

        [HttpPost, HttpGet]
        public async Task PostAsync()
        {
            // Delegate the processing of the HTTP POST to the adapter.
            // The adapter will invoke the bot.
            await Adapter.ProcessAsync(Request, Response, Bot);
        }
    }

https://testbotbasiccsharp.azurewebsites.net/

最佳答案

您的问题看起来与事件名称有关。 WebChat 正在发送名为 requestWelcomeDialog 的事件,您的机器人代码将在其中查找名为 webchat/join 的事件。如果你改变其中之一,它应该可以工作。

处理欢迎消息有两种可能性。

  1. 对话更新。 DirectLine 默认广播对话更新事件 however this is not preferred 。此事件将在 OnMembersAddedAsync 中结束。

  2. 自定义事件。使用 WebChat v4 发送自定义事件,如 this sample 中所述。 。此事件将在 OnEventActivityAsync 中结束。

我的建议是升级到新的 WebChat (v4) 并查看 this sample 。 ConversationUpdate 有限制,通过发送自定义事件可以更加灵活。

关于c# - 带有 v4 Webchat channel 的 Bot 框架问候消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60390564/

相关文章:

botframework - 使用 botframework 将字节数组显示为图像

node.js - Azure Bot Service SDK 是否需要 Restify? ( Node )

c# - 如何修复 C# 中的 'Cannot implicitly convert type' 错误?

c# - Entity Framework 核心代码优先 : Cascade delete on a many-to-many relationship

javascript - Azure 应用服务在 URL 中添加 '/'

azure - 如何从托管标识中删除 Azure 角色分配?

azure - 在 botframework-sdk 中,我的机器人突然开始出现此错误 Error : Get Token request returned http error: 401

c# - 绑定(bind)到通用 UI 元素/XAML 接口(interface)

c# - 使用 webkit 的 html5 编辑器

c# - 如何从 C# 返回字符串 Kusto 查询的结果