0
votes

I'm creating a bot using Bot Framework in C#

I have this piece of code :

  var faq = await result;

  if (faq == "Faq with menu")
  {
      await context.PostAsync("Under construction");
  }
  else if (faq == "Faq with dialog")
  {
      context.Call(new FaqDialog(), this.ResumeAfterOptionDialog);
  }

Faq with dialog I have connected with a dialog class.

I want to connect Faq with menu with my client in Api.ai. Do you have any idea how to do it?

2

2 Answers

0
votes

What I would do is to create an enum with the Faq values:

Public enum Faq{
 Undefined,
 Menu,
 Dialog
}

Then create a method that will call Api.ai with the user message and map the intent response to the enum:

  public T MatchAiIntent<T>(string message) where T : struct, IConvertible
    {
        if (!typeof(T).IsEnum)
        {
            throw new ArgumentException("T must be an enum type!");
        }

    T result = default(T);
    try
    {
        var response = apiAi.TextRequest(message);
        var intentName = response?.Result?.Metadata?.IntentName;

        if (intentName == null)
        {
            return result;
        }

        Enum.TryParse<T>(intentName, true, out result);

        return result;
    }
    catch (Exception exception)
    {
        //logit
        throw;
    }
} 

Then you can use it in your code:

var response = MatchAiIntent(faq);
if (response == Faq.Menu)
  {
      await context.PostAsync("Under construction");
  }
0
votes

[UPDATE]

CONNECTING TO Dialogflow (previously known as API.AI) FROM C#

Follow these steps (working example in C#)

  1. After you create a Dialogflow agent go to the agent's settings --> General --> click on the Service Account link
  2. You will be sent to to google cloud platform where you can create a service account
  3. After you create a service account, there will be an option to create a KEY, create it and download the (JSON) format of it
  4. This key will be used to connect from your C# project to the Dialogflow agent
  5. Install Google.Cloud.Dialogflow.V2 package in your project
  6. Create for example a Dialogflow manager class (check below for an example)

        public class DialogflowManager {
        private string _userID;
        private string _webRootPath;
        private string _contentRootPath;
        private string _projectId;
        private SessionsClient _sessionsClient;
        private SessionName _sessionName;
    
        public DialogflowManager(string userID, string webRootPath, string contentRootPath, string projectId) {
    
            _userID = userID;
            _webRootPath = webRootPath;
            _contentRootPath = contentRootPath;
            _projectId = projectId;
            SetEnvironmentVariable();
    
        }
    
        private void SetEnvironmentVariable() {
            try {
                Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", _contentRootPath + "\\Keys\\{THE_DOWNLOADED_JSON_FILE_HERE}.json");
            } catch (ArgumentNullException) {
                throw;
            } catch (ArgumentException) {
                throw;
            } catch (SecurityException) {
                throw;
            }
        }
    
        private async Task CreateSession() {
            // Create client
            _sessionsClient = await SessionsClient.CreateAsync();
            // Initialize request argument(s)
            _sessionName = new SessionName(_projectId, _userID);
    
        }
    
        public async Task < QueryResult > CheckIntent(string userInput, string LanguageCode = "en") {
            await CreateSession();
            QueryInput queryInput = new QueryInput();
            var queryText = new TextInput();
            queryText.Text = userInput;
            queryText.LanguageCode = LanguageCode;
            queryInput.Text = queryText;
    
            // Make the request
            DetectIntentResponse response = await _sessionsClient.DetectIntentAsync(_sessionName, queryInput);
            return response.QueryResult;
        }
    }
    
  7. And then this can be called like this for example to get detect Intents

         DialogflowManager dialogflow = new DialogflowManager("{INSERT_USER_ID}",
        _hostingEnvironment.WebRootPath,
        _hostingEnvironment.ContentRootPath,
        "{INSERT_AGENT_ID");
    
    var dialogflowQueryResult = await dialogflow.CheckIntent("{INSERT_USER_INPUT}");