How to communicate single Bot with two differnet QnA Data sets based on user requrest? - bots

How to communicate Single ChatBot with different QnA data sets(JSON)..
Ex :
QnA1 (JSON file)
QnA2 (JSON file)
and Single Bot application.
when I launch the with site1, Bot will communicate to QnA1 data.
when I launch the with site2, Bot will communicate to QnA2 data.
Here I have only one Bot.
please let me know how to pass KNOWLEDGE_BASE_ID to Bot.

when I launch the with site1, Bot will communicate to QnA1 data. when I launch the with site2, Bot will communicate to QnA2 data.
The UI of BotFramework are based on Dialog, so I can only guess that your site 1 and site 2 means two dialogs and each dialog are built based on QnA.
please let me know how to pass KNOWLEDGE_BASE_ID to Bot.
Then to pass KNOWLEDGE_BASE_ID to your bot, you can use QnAMakerAttribute for your dialog. In .Net SDK for example:
[QnAMakerAttribute("Your-subscription-key", "Your-Qna-KnowledgeBase-ID", "No Answer in Knowledgebase.", 0.5)]
[Serializable]
public class QnADialog1 : QnAMakerDialog
{
}
And if you're using node.js SDK for development, you can pass the id like this:
var recognizer = new builder_cognitiveservices.QnAMakerRecognizer({
knowledgeBaseId: 'Your-Qna-KnowledgeBase-ID', // process.env.QnAKnowledgebaseId,
subscriptionKey: 'Your-Qna-KnowledgeBase-Password'}); //process.env.QnASubscriptionKey});
For more information, you can refer to the Blog samples, there're both C# and node.js version of demos.
If you still want to ask how to use two knowledge-bases in one bot, please leave a comment and tell me which sdk are you using for development, .net or node.js? I will come back and update my answer.
UPDATE:
You can code for example like this:
[Serializable]
public class RootDialog : IDialog<object>
{
private string currentKB;
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
if (activity.Text == "reset") //handle reset
currentKB = null;
else if (activity.Text == "qna1" || currentKB == "qna1")
{
currentKB = "qna1";
if (activity.Text == "qna1")
await context.PostAsync("this is qna 1");
else
await context.Forward(new Dialogs.QnADialog1(), this.QnAReceivedAsync, activity, CancellationToken.None);
}
else if (activity.Text == "qna2" || currentKB == "qna2")
{
currentKB = "qna2";
if (activity.Text == "qna2")
await context.PostAsync("this is qna 2");
else
await context.Forward(new Dialogs.QnADialog2(), this.QnAReceivedAsync, activity, CancellationToken.None);
}
else
{
var reply = activity.CreateReply("Please choose a knowledge base...");
var heroCard = new HeroCard
{
Title = "Knowledge bases",
Text = "Which one do you want to choose?",
Buttons = new List<CardAction>
{
new CardAction(ActionTypes.ImBack, "QnA base 1", value:"qna1"),
new CardAction(ActionTypes.ImBack, "QnA base 2", value:"qna2")
}
};
Attachment attachment = heroCard.ToAttachment();
reply.Attachments.Add(attachment);
await context.PostAsync(reply);
context.Wait(MessageReceivedAsync);
}
}
public async Task QnAReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
context.Wait(MessageReceivedAsync);
}
}
And in the MessagesController make the RootDialog as the root of dialog stack:
if (activity.Type == ActivityTypes.Message)
{
await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
}
Finally by QnADialog1 and QnADialog2, I only passed knowledge base ID and key there.

Related

Azure Bot fails after a time [duplicate]

Good day everyone,
I'm creating a chatbot for my company and I started with the samples on github and the framework docs.
We decided to host it on Azure and added LUIS and Table Storage to it. The Bot runs fine locally in Botframework Emulator, but on Azure (WebChat, Telegram) it will only run for approximatly an hour to an hour and fifteen minutes, if no one tries to communicate with the bot. After this period of time, the bot will just run into an internal server error. When you ask the bot something, you can stretch this time window (For how long I don't know and why I don't know either, sorry).
In Azure "Always On" is set to true.
I'm really frustrated at this point, because I cannot find the problem and I'm pretty sure there must be something wrong with my code, because I don't properly understand the framework. I'm still a beginner with Azure, C# and Bot Framework.
Also I have already read everything on "internal server error's" on here and github. Also tried Debugging, even with extra Debbug options in VS. We have not tried Application Insights yet.
At the moment I'm doing everything with the LUIS Dialog which calls / Forwards to other IDialogs:
[LuisIntent(Intent_Existens)]
public async Task ExistensOf(IDialogContext context, IAwaitable<IMessageActivity> message, LuisResult result)
{
var existens = new ExistensDialog();
var messageToForward = await message;
if (result.Entities.Count == 1)
{
messageToForward.Value = result.Entities[0].Entity;
await context.Forward(existens, AfterDialog, messageToForward);
}
else
{
context.Wait(this.MessageReceived);
}
}
I know that "Value" is for CardActions, but I don't know how else I could pass Entities to the child dialog.
[Serializable]
public class ExistensDialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
if (message.Text.Contains("specificWord"))
{
await context.Forward(new ExistensHU(), AfterDialog, message);
}
else
{
await context.Forward(new ExistensBin(), AfterDialog, message);
}
}
private async Task AfterDialog(IDialogContext context, IAwaitable<object> result)
{
context.Done<object>(null);
}
}
then:
[Serializable]
internal class ExistensHU : IDialog<object>
{
private Renamer renamer = new Renamer(); // Just for renaming
private ExternalConnection ec = new ExternalConnection(); //Just a HTTP connection to a WebApp to get data from it
public async Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
const string apiCallURL = "some/API/"; // ExternalConnection...
var message = await result;
string nameHU = renamer.RemoveBlanks(message.Value.ToString());
StringBuilder answerBuilder = new StringBuilder();
var name = ec.CreateSingleAPIParameter("name", nameHU);
Dictionary<string, string> wms = await ec.APIResultAsDictionary(apiCallURL, name);
foreach (var item in wms)
{
if (item.Key.Equals("none") && item.Value.Equals("none"))
{
answerBuilder.AppendLine($"Wrong Answer");
}
else
{
answerBuilder.AppendLine($"Correct Answer");
}
}
await context.PostAsync(answerBuilder.ToString());
context.Done<object>(null);
}
}
That's basically every Dialog in my project.
Also I have an IDialog which looks like this:
[Serializable]
public class VerificationDialog : IDialog<object>
{
[NonSerializedAttribute]
private readonly LuisResult _luisResult;
public VerificationDialog(LuisResult luisResult)
{
_luisResult = luisResult;
}
public async Task StartAsync(IDialogContext context)
{
var message = _luisResult.Query;
if (!message.StartsWith("Wie viele"))
{
context.Call(new ByVerificationDialog(_luisResult), AfterDialog);
}
else
{
context.Call(new CountBinsByVerification(_luisResult), AfterDialog);
}
}
private async Task AfterDialog(IDialogContext context, IAwaitable<object> result)
{
context.Done<object>(null);
}
}
I don't know if I'm allowed to pass the luisResult like this from BasicLuisDialog. This could be the issue or one of the issues.
Basically that's it and I'm still getting used to the framework. I'm not expecting an absolute answer. Just hints/tips and advice how to make everything better.
Thanks in advance!
If you are using the .NET SDK version 3.14.0.7. There is currently a bug we are tracking in this version. There has been a number of reports and we are actively investigating. Please try downgrading to 3.13.1. This should fix the issue for you until we can release a new version.
for reference we are tracking the issue on these GitHub issues:
https://github.com/Microsoft/BotBuilder/issues/4322
https://github.com/Microsoft/BotBuilder/issues/4321
Update 3/21/2018:
We have pushed a new version of the SDK which includes a fix for this issue https://www.nuget.org/packages/Microsoft.Bot.Builder/3.14.1.1
Internal error usually means exceptions in .NET application.
Use AppDomain.CurrentDomain.UnhandledException to receive all unhandled exceptions and log them somewhere (consider using Application Insights).
After you investigate logged information fix that.

Microsoft Bot Emulator throws "An error occurred while sending the request" when sending a message that expects a Herocard as a response

I am developing a chat bot using QnAMakerServices and an emulator to train the bot.
In my QnaDialog.cs, I override one method from the QnAMakerDialog which is the RespondFromQnAMakerResultAsync.
This is for me to customize the method and create a Herocard as a response. My code goes like this:
namespace IMPBot.Dialogs
{
[Serializable]
public class QnaDialog : QnAMakerDialog
{
public QnaDialog() : base(new QnAMakerService(new QnAMakerAttribute(ConfigurationManager.AppSettings["QnaSubscriptionKey"], ConfigurationManager.AppSettings["QnaKnowledgebaseId"], "Sorry, I couldn't find an answer for that. Please rephrase your question and make sure to ask questions only related to Insights Marketplace.", 0.5)))
{
}
protected override async Task RespondFromQnAMakerResultAsync(IDialogContext context, IMessageActivity message, QnAMakerResults result)
{
// answer is a string
var answer = result.Answers.First().Answer;
if (answer.Contains(';'))
{
Activity reply = ((Activity)context.Activity).CreateReply();
string[] qnaAnswerData = answer.Split(';');
string title = qnaAnswerData[0];
string description = qnaAnswerData[1];
string url = qnaAnswerData[2];
string imageURL = qnaAnswerData[3];
HeroCard card = new HeroCard
{
Title = title,
Subtitle = description,
};
card.Buttons = new List<CardAction>
{
new CardAction(ActionTypes.OpenUrl, "See more", value: url)
};
card.Images = new List<CardImage>
{
new CardImage( url = imageURL)
};
reply.Attachments.Add(card.ToAttachment());
//await context.PostAsync(reply);
}
else
{
await context.PostAsync(answer);
context.Wait(MessageReceivedAsync);
}
}
}
}
However, when I run the Bot Emulator it throws the following error:
Exception: An error occurred while sending the request. [File of type 'text/plain']
And it did not show any card.
On the other hand, the log says,
Warning: The latest bot SDK version is 3.13.0 but the bot is running
SDK version 3.12.2. Consider upgrading the bot to the latest SDK.
Is there any connection with the error to what the log says?
Or did I miss something in the code?

C# Bot Framework : Form flow set value for the field based on previous Answer [duplicate]

Hello I'm new to Microsoft Bot Framework and I have a question that I couldn't find an answer to.
I have a FormFlow that ask the user for some question, after a specific question I want the bot to do some logic and show messages accordingly (for example if the user selected option 1 then show message X and if the user selected option 2 show message Y).
Here is my code:
using Microsoft.Bot.Builder.FormFlow;
using Microsoft.Bot.Builder.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Bot_CRM.FormFlow
{
public enum RequestOptions { Unknown, CheckStatus, CreateCase };
[Serializable]
public class CaseFormFlow
{
public RequestOptions RequestType;
[Prompt("What is your first name?")]
public string FirstName;
public string LastName;
public string ContactNumber;
[Prompt("Please enter your id")]
public string Id;
public static IForm<CaseFormFlow> BuildForm()
{
OnCompletionAsyncDelegate<CaseFormFlow> processRequest = async (context, state) =>
{
await context.PostAsync($#"Thanks for your request");
};
return new FormBuilder<CaseFormFlow>()
.Message("Hello and welcom to my service desk bot")
.Field(nameof(FirstName))
.Message("hello {FirstName}")
.Field(nameof(Id))
.Field(nameof(RequestType)) =>
//here if user select 1 start flow of check status and if user select 2 start flow of create case
.AddRemainingFields()
.Message("Thank you request. Our help desk team will get back to you shortly.")
.OnCompletion(processRequest)
.Build();
}
}
}
Updated code after Ezequiel's suggestion:
return new FormBuilder<CaseFormFlow>()
.Message("Hello and welcom to my service desk bot")
.Field(nameof(FirstName))
.Message("hello {FirstName}")
.Field(new FieldReflector<CaseFormFlow>(nameof(RequestType))
.SetActive(state => state.AskUserForRequestType)
.SetNext((value, state) =>
{
var selection = (RequestOptions)value;
if (selection == RequestOptions.CheckStatus)
{
return new NextStep(new[] { nameof(Id) });
}
else
{
return new NextStep();
}
}))
Thanks in advance for the help
This is a great question.The key thing is to use the SetActive and SetNext methods of the Field<T> class. You should consider using the FieldReflector class; though you can implement your own IField.
SetActive is described in the Dynamic Fields section of the FormFlow documentation. Basically it provides a delegate that enables the field based on a condition.
SetNext will allow you to decide what step of the form should come next based on your custom logic.
You can take a look to the ContosoFlowers sample. In the Order form; something similar is being done.
public static IForm<Order> BuildOrderForm()
{
return new FormBuilder<Order>()
.Field(nameof(RecipientFirstName))
.Field(nameof(RecipientLastName))
.Field(nameof(RecipientPhoneNumber))
.Field(nameof(Note))
.Field(new FieldReflector<Order>(nameof(UseSavedSenderInfo))
.SetActive(state => state.AskToUseSavedSenderInfo)
.SetNext((value, state) =>
{
var selection = (UseSaveInfoResponse)value;
if (selection == UseSaveInfoResponse.Edit)
{
state.SenderEmail = null;
state.SenderPhoneNumber = null;
return new NextStep(new[] { nameof(SenderEmail) });
}
else
{
return new NextStep();
}
}))
.Field(new FieldReflector<Order>(nameof(SenderEmail))
.SetActive(state => !state.UseSavedSenderInfo.HasValue || state.UseSavedSenderInfo.Value == UseSaveInfoResponse.Edit)
.SetNext(
(value, state) => (state.UseSavedSenderInfo == UseSaveInfoResponse.Edit)
? new NextStep(new[] { nameof(SenderPhoneNumber) })
: new NextStep()))
.Field(nameof(SenderPhoneNumber), state => !state.UseSavedSenderInfo.HasValue || state.UseSavedSenderInfo.Value == UseSaveInfoResponse.Edit)
.Field(nameof(SaveSenderInfo), state => !state.UseSavedSenderInfo.HasValue || state.UseSavedSenderInfo.Value == UseSaveInfoResponse.Edit)
.Build();
}
}
}

Starting a conversation with Microsoft bot builder and microsoft bot framework

I tried to have my bot start a conversation with the user but I don't know where in the code I should send the message from. The documentation for starting a convo is here, but it's not super helpful: http://docs.botframework.com/connector/new-conversations/#navtitle. I also tried replying in the HandleSystemMessages (which works with the emulator if I change the message type) but it still won't send the first message.
I'm using Microsoft Bot Connector and C#.
// Idk how to do syntax highlighting in stackoverflow
// This is my code from the MessageController Class
public async Task<Message> Post([FromBody]Message message)
{
if (message.Type == "Message")
{
return message.CreateReplyMessage($"You said:{message.Text}");
}
else
{
return HandleSystemMessage(message);
}
}
I spent a lot of time researching this issue. As a result, I managed to initiate sending a message on behalf of the bot. My example sends a message to a group conversation. The code below - is rough draft but it works:
class Program
{
static void Main(string[] args)
{
var connector = new ConnectorClient(new Uri("https://skype.botframework.com"));
var conversationId = "19:ConversationAccountId#thread.skype";
var conversation = new ConversationAccount(true, conversationId);
var botAccount = new ChannelAccount("28:74a05skypeBotChannelAccountId", "your bot name");
IMessageActivity message = Activity.CreateMessageActivity();
message.From = botAccount;
message.Conversation = conversation;
message.ChannelId = "skype";
message.Text = "some text";
message.Locale = "en-En";
connector.Conversations.SendToConversation((Activity)message);
}
}

.Net Mobile Service Back-End for Notification Hub

I am currently writing a mobile application with Xamarin for Android and will be adding iOS once my company purchases a Mac so that I can start developing the iOS portion. I am currently trying to write a .Net Mobile Services back-end for Azure Notification Hub that will allow me to register the devices from the back end as well as send push notification to a specific user, and/or all users.
I have followed the Azure documentation all the way through Getting Started With Notification Hub and have successfully performed a single platform push. Moving beyond this example however is where I get stuck. Every example beyond this point completely drops Android support and only focuses on Windows Phone and iOS. I have watched a few Channel 9 videos regarding this subject and again it's all Windows Phone, Windows 8, and iOS based.
Does anyone have an example of a .Net Mobile Service back-end for Azure Notification Hub that will register the device to the notification hub from the back-end? Thank you for your time.
I don't have sample code on GitHub yet, but here's a gist of how to get NotificationHub working on Android.
using Microsoft.ServiceBus.Notifications;
using Newtonsoft.Json;
public class AndroidNotificationHub
{
private readonly NotificationHubClient _hubClient;
public AndroidNotificationHub()
{
const string cn = "YourConnectionStringHere";
const string hubPath = "YourHubPathHere";
_hubClient = NotificationHubClient.CreateClientFromConnectionString(cn, hubPath);
}
public async Task<RegistrationDescription> Register(string platform, string installationId, string registrationId, string userName)
{
// Get registrations for the current installation ID.
var regsForInstId = await _hubClient.GetRegistrationsByTagAsync(installationId, 100);
var updated = false;
var firstRegistration = true;
RegistrationDescription registration = null;
// Check for existing registrations.
foreach (var registrationDescription in regsForInstId)
{
if (firstRegistration)
{
// Update the tags.
registrationDescription.Tags = new HashSet<string>() { installationId, userName };
// We need to handle each platform separately.
switch (platform)
{
case "android":
var gcmReg = registrationDescription as GcmRegistrationDescription;
gcmReg.GcmRegistrationId = registrationId;
registration = await _hubClient.UpdateRegistrationAsync(gcmReg);
break;
}
updated = true;
firstRegistration = false;
}
else
{
// We shouldn't have any extra registrations; delete if we do.
await _hubClient.DeleteRegistrationAsync(registrationDescription);
}
}
// Create a new registration.
if (!updated)
{
switch (platform)
{
case "android":
registration = await _hubClient.CreateGcmNativeRegistrationAsync(registrationId, new[] { installationId, userName });
break;
}
}
return registration;
}
// Basic implementation that sends a notification to Android clients
public async Task<bool> SendNotification(int id, string from, string text, string tag)
{
try
{
var payload = new
{
data = new
{
message = new
{
// these properties can be whatever you want
id,
from,
text,
when = DateTime.UtcNow.ToString("s") + "Z"
}
}
};
var json = JsonConvert.SerializeObject(payload);
await _hubClient.SendGcmNativeNotificationAsync(json, tag);
return true;
}
catch (ArgumentException ex)
{
// This is expected when an APNS registration doesn't exist.
return false;
}
}
public async Task<bool> ClearRegistrations(string userName)
{
// Get registrations for the current installation ID.
var regsForInstId = await _hubClient.GetRegistrationsByTagAsync(userName, 100);
// Check for existing registrations.
foreach (var registrationDescription in regsForInstId)
{
// We shouldn't have any extra registrations; delete if we do.
await _hubClient.DeleteRegistrationAsync(registrationDescription);
}
return true;
}
}
Your Android client will need to call your backend's registration API during startup. I have an MVC action for this.
[HttpPost]
public async Task<ActionResult> Register(string platform, string installationId, string registrationId, string userName)
{
try
{
var hub = new AndroidNotificationHub();
var registration = await hub.Register(platform, installationId, registrationId, userName);
return Json(registration);
}
catch (Exception ex)
{
return Content(ex.ToString());
}
}
Once the mobile client has registered, you can then start sending notifications from your backend, by calling the SendNotification method.
Hope this points you in the right direction.

Resources