【问题标题】:Query Auto-Completion in c# bot in bot framework在 bot 框架中的 c# bot 中查询自动完成
【发布时间】:2018-09-06 17:43:20
【问题描述】:

我想在我使用 microsoft bot 框架开发并作为 iframe 添加到我的门户中的 c# bot 中实现自动完成功能。 是否可以在此机器人中实现查询建议或查询自动完成? 我也试过这个解决方案

Auto complete in Bot Framework

但我发现它对我没有帮助。 我可以在这里使用 jquery 自动完成吗?

https://jqueryui.com/autocomplete/

请帮帮我。

提前致谢。

【问题讨论】:

    标签: c# jquery autocomplete botframework


    【解决方案1】:

    我可以在这里使用 jquery 自动完成吗?

    根据我的测试,我们可以将jQuery Autocomplete widgets附加到网络聊天输入框。以下示例代码供您参考。

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta charset="utf-8" />
        <link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"/>
        <link rel="stylesheet" href="https://jqueryui.com/resources/demos/style.css">
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
        <script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
        <style>
            .wc-chatview-panel {
                width: 350px;
                height: 500px;
                position: relative;
            }
        </style>
    </head>
    <body>
        <div id="mybot" />
    </body>
    </html>
    <script>
        BotChat.App({
            directLine: { secret: "{your_directline_secret}" },
            user: { id: 'You' },
            bot: { id: '{your_bot_id}' },
            resize: 'detect'
        }, document.getElementById("mybot"));
    
        $(function () {
    
            var availableTags = ["ActionScript", "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL"];
    
            $("input.wc-shellinput").autocomplete({
                source: availableTags
            });
    
        })
    </script>
    

    测试结果:

    注意:我输入a,它显示一个列表项供我选择,然后我选择一个项目,例如ActionScript,如果我直接点击发送按钮,它只会向机器人发送a。为了避免这种情况,我们可以手动输入一个空格(或其他字符)并在点击发送按钮之前将其删除。

    【讨论】:

    • 我试过这个..当我输入 a 时,如果我选择其中任何一个,如 Asp 并尝试发送消息,它会显示一些相关的输出,它只发送 a 而不是 Asp
    • 我怎样才能克服这个问题..它不会是用户友好的,我必须添加任何空格或字符eveytime
    • @chandra,你能解决这个问题吗?我们必须每次都添加空格。
    【解决方案2】:

    自 4 天以来我一直面临这个问题。最后我成功了。

    您需要为 input.wc-shellinput 元素编写 jQuery。我已经用 azure search 实现了它。

    <!DOCTYPE html>
    <html>
    <head>
        <link href="../Styles/Skins/botchat.css" rel="stylesheet" />
        <link href="../Styles/skins/customwebchat.css" rel="stylesheet">
        <link href="styles/pages.css" rel="stylesheet" />
    </head>
    <body>`enter code here`
        <div id="body-container">
    
            <div class="bot-container">
                <div id="bot" class="snow"></div>
            </div>
        </div>
    
        <!--  <script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>-->
        <script src="js/botchat.js"></script>
        <script src="https://cdn.botframework.com/botframework-webchat/latest/CognitiveServices.js"></script>
        <script>
    
            const speechOptions = {
    
                speechRecognizer: new CognitiveServices.SpeechRecognizer({ subscriptionKey: '' }),
    
                speechSynthesizer: new CognitiveServices.SpeechSynthesizer({
    
                    gender: CognitiveServices.SynthesisGender.Female,
    
                    subscriptionKey: '',
    
                    voiceName: 'Microsoft Server Speech Text to Speech Voice (en-US, JessaRUS)'
    
                })
    
            };
    
            BotChat.App({
                directLine: { secret: '' },
                user: { id: 'userid' },
                bot: { id: 'botid' },
                speechOptions: speechOptions,
                resize: 'detect'
            }, document.getElementById("bot"));
        </script>
        <script src="/Scripts/jquery-1.10.2.js"></script>
        <script src="/Scripts/jquery-ui.js"></script>
    
        <script src="/Scripts/jquery.autocompleteInline.js"></script>
    
    
        <link href="/Content/jquery-ui.css" rel="stylesheet" />
        <link href="/Content/tutorial.css" rel="stylesheet" />
        <link href="/Content/jquery.autocompleteInline.css" rel="stylesheet" />
    
        <script  type="text/javascript">
    
            $(function () {
                $("input.wc-shellinput").autocomplete({
                    source: function (request, response) {
                        $.ajax({
                            type: "POST",
                            url: suggestUri,
                            dataType: "json",
                            headers: {
                                "api-key": searchServiceApiKey,
                                "Content-Type": "application/json",
    
                                "Access-Control-Allow-Origin": "*",
                                "Access-Control-Allow-Methods": "GET,PUT,POST,DELETE"
                            },
                            data: JSON.stringify({
                                top: 5,
                                fuzzy: false,
                                suggesterName: "", //Suggester Name according to azure search index.
                                search: request.term
                            }),
                            success: function (data) {
                                if (data.value && data.value.length > 0) {
                                    userinput = data.value;
                                    console.log(userinput);
                                    response(data.value.map(x => x["@search.text"]));
                                }
                            }
                        });
                    },
                    minLength: 3,
                    position: {
                        my: "left top",
                        at: "left bottom",
                        collision: "fit flip"
                    },
                    select: function (Event, ui) {
    
                        $(document).ready(function () {
    
                            var input = document.getElementsByClassName("wc-shellinput")[0];
                            var lastValue = input.value;
                            input.value = ui.item.value;
                            var event = new CustomEvent('input', { bubbles: true });
                            // hack React15
                            event.simulated = true;
                            // hack React16
                            var tracker = input._valueTracker;
                            if (tracker) {
                                tracker.setValue(lastValue);
                            }
    
                            input.dispatchEvent(event);
                        })
    
                        $('wc-textbox').val("");
                        Event.preventDefault();
    
                        $(".wc-send:first").click();
                    }
    
                });
            });
    
    
        </script>
        <script>
            var searchServiceName = "";
            var searchServiceApiKey = "";
            var indexName = "";
            var apiVersion = "";
    
            var suggestUri = "https://" + searchServiceName + ".search.windows.net/indexes/" + indexName + "/docs/suggest?api-version=" + apiVersion;
            var autocompleteUri = "https://" + searchServiceName + ".search.windows.net/indexes/" + indexName + "/docs/autocomplete?api-version=" + apiVersion;
            var searchUri = "https://" + searchServiceName + ".search.windows.net/indexes/" + indexName + "/docs/search?api-version=" + apiVersion;
        </script>
    </body>
    
    </html>
    

    【讨论】:

    • 我的示例 API 输出:{ "@odata.context": "URL", "value": [{ "suggestedItems": ["你在哪里", "你去过哪里", ] }, { "suggestedItems": [ "你好吗?" ] } ] } 在响应中捕获错误(data.value.map(x => x["@search.text"]));错误消息:未捕获的类型错误:无法读取未定义的属性“标签”我尝试用“值”和“@data.context”替换@search.text,但仍然出现错误。我要显示所有问题数据
    • 我为我的修复找到了解决方案stackoverflow.com/questions/55101639/…
    猜你喜欢
    • 2019-06-29
    • 1970-01-01
    • 2018-10-24
    • 2018-04-20
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多