使用C#

时间:2018-08-24 16:44:35

标签: botframework chatbot luis

我正在尝试实现一个C#代码,该代码可以像this tutorial中所示那样在我的Luis API中添加话语。

所以我只对功能AddUtterances()感兴趣,为了对其进行测试,我将其放在功能ShowLuisResult()中,以确保每次我向聊天机器人发送消息时都会使用它,但是当我看一下API,发现没有添加任何语音...

我将文件utterances.jsonBasicLuisDialog.cs放在同一位置,并放在/d/home控制台的kudu中,以确保它可以正常工作。

这是代码:

using System;
using System.Configuration;
using System.Threading.Tasks;

using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Luis;
using Microsoft.Bot.Builder.Luis.Models;

using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Net.Http;
using System.Xml.Linq;

namespace Microsoft.Bot.Sample.LuisBot
{
    // For more information about this template visit http://aka.ms/azurebots-csharp-luis
    [Serializable]
    public class BasicLuisDialog : LuisDialog<object>
    {

        // NOTE: Replace this example LUIS application ID with the ID of your LUIS application.
        static string appID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

        // NOTE: Replace this example LUIS application version number with the version number of your LUIS application.
        static string appVersion = "0.1";

        // NOTE: Replace this example LUIS authoring key with a valid key.
        static string authoringKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

        static string host = "https://westus.api.cognitive.microsoft.com";
        static string path = "/luis/api/v2.0/apps/" + appID + "/versions/" + appVersion + "/";


    public BasicLuisDialog() : base(new LuisService(new LuisModelAttribute(
        ConfigurationManager.AppSettings["LuisAppId"], 
        ConfigurationManager.AppSettings["LuisAPIKey"], 
        domain: ConfigurationManager.AppSettings["LuisAPIHostName"])))
    {
    }

    [LuisIntent("None")]
    public async Task NoneIntent(IDialogContext context, LuisResult result)
    {
        await this.ShowLuisResult(context, result);
    }

    // Go to https://luis.ai and create a new intent, then train/publish your luis app.
    // Finally replace "Greeting" with the name of your newly created intent in the following handler
    [LuisIntent("Greeting")]
    public async Task GreetingIntent(IDialogContext context, LuisResult result)
    {
        await this.ShowLuisResult(context, result);
    }

    [LuisIntent("Cancel")]
    public async Task CancelIntent(IDialogContext context, LuisResult result)
    {
        await this.ShowLuisResult(context, result);
    }

    [LuisIntent("Help")]
    public async Task HelpIntent(IDialogContext context, LuisResult result)
    {
        await this.ShowLuisResult(context, result);
    }

    private async Task ShowLuisResult(IDialogContext context, LuisResult result) 
    {
        AddUtterances("utterances.json");
        await context.PostAsync($"You have reached {result.Intents[0].Intent}. You said: {result.Query}");
        context.Wait(MessageReceived);
    }


    async static Task AddUtterances(string input_file)
    {
        string uri = host + path + "examples";
        string requestBody = File.ReadAllText(input_file);
        var response = await SendPost(uri, requestBody);
        var result = await response.Content.ReadAsStringAsync();
    }

    async static Task<HttpResponseMessage> SendPost(string uri, string requestBody)
    {
        using (var client = new HttpClient())
        using (var request = new HttpRequestMessage())
        {
            request.Method = HttpMethod.Post;
            request.RequestUri = new Uri(uri);
            request.Content = new StringContent(requestBody, Encoding.UTF8, "text/json");
            request.Headers.Add("Ocp-Apim-Subscription-Key", authoringKey);
            return await client.SendAsync(request);
        }
    }


}

}

这是utterances.json的内容:

[
    {
        "text": "go to Seattle",
        "intentName": "Help",
        "entityLabels": []
    },
    {
        "text": "book a flight",
        "intentName": "Greeting",
        "entityLabels": []
    }
]

2 个答案:

答案 0 :(得分:0)

无论出于何种原因,Luis的Botbuilder工具都已从Github中删除,就像两周前在这里一样-

Google cache of Luis Bot Builder tools git

Image capture of the git with all functionality of Luis CLI form - no need for full program

我还无法发布图片,因此这里是图片https://imgur.com/Rl41wtn

的链接

答案 1 :(得分:0)

我尚未测试您的代码,但确实注意到您没有等待ShowLuisResult内部对AddUtterances的调用:

private async Task ShowLuisResult(IDialogContext context, LuisResult result)
{
    AddUtterances("utterances.json"); //<-- NOTE: this line should be awaited
    await context.PostAsync($"You have reached {result.Intents[0].Intent}. You said: {result.Query}");
    context.Wait(MessageReceived);
}