Q&A bot for Business Central
Hello Team!
As I promised - I'm publishing description of Question & Answers bot demo for #NavTechDays (Link)
1. Go to https://www.qnamaker.ai - it's a bot service which we used for our demo.
2. Press Create a knowledge base
3. Press Create a QnA service (If you don't have it in Azure)
4. It'll open your Azure portal with service creation menu:
IMPORTANT! Remember about pricing. Search pricing tier is most expensive. B tier costs about 65 euro per month. For developing you can use F tier - it's almost free, but as I understood - you can have only one tier with such type. For Management pricing use F0 tier.
5. Wait until QnA maker will be deployed. After that you could reduce your costs changing App Service Plan (if you create this bot for development purposes). Go to your resource group, select your app service plan resource and select Scale up -> Dev/Test. Here you can select D1 service plan (or even F1).
6. Now return to your knowledge base, refresh your page and select created service in Step 2:
7. Type name of your knowledge base:
8. Select template dialog model (chit-chat) if needed. It contains a lot of basic QnA pairs to start dialog with bot. And press Create your KB.
9. Wait a little bit until service will create.
10. Your knowledge base created.
11. Let's publish it to see how it works. Press Publish -> Publish.
12. Now you get connection parameters:
13. Let's check it with Postman. Works perfectly.
14. Now let's create Bot App for Business Central.
Management codeunit (do not forget to change base ID and EndpointKey to yours):
codeunit 50110 "BotMgt" { trigger OnRun() begin end; procedure SendQuestion(Question: Text): Text var Content: HttpContent; Headers: HttpHeaders; Client: HttpClient; URL: text; ResponseMessage: HttpResponseMessage; ResponseText: text; JsonObj: JsonObject; JsonTok: JsonToken; JsonArr: JsonArray; begin JsonObj.Add('question', Question); JsonObj.WriteTo(Question); Content.WriteFrom(Question); Content.GetHeaders(Headers); Headers.Remove('Content-Type'); Headers.Add('Content-Type', 'application/json'); Client.DefaultRequestHeaders().Add('Authorization', 'EndpointKey d2d880f4-2f2e-4e2e-b5b5-4e474a2d611b'); Client.DefaultRequestHeaders().Add('Accept', 'application/json'); URL := 'https://myqnabotservice.azurewebsites.net/qnamaker/knowledgebases/a0b800a6-38be-4772-b2df-cb6c75e364d1/generateAnswer'; if not Client.Post(URL, Content, ResponseMessage) then Error('The call to the web service failed.'); if not ResponseMessage.IsSuccessStatusCode then begin ResponseMessage.Content().ReadAs(ResponseText); error('The web service returned an error message:\' + 'Status code: %1' + 'Description: %2', ResponseMessage.HttpStatusCode, ResponseText); end; ResponseMessage.Content().ReadAs(ResponseText); JsonTok.ReadFrom(ResponseText); JsonObj := JsonTok.AsObject; JsonObj.SelectToken('answers', JsonTok); Jsonarr := JsonTok.AsArray; JsonArr.Get(0, JsonTok); Exit(ReadAnswer(JsonTok)); end; procedure ReadAnswer(JsonTok: JsonToken): Text; var JsonObj: JsonObject; MessageText: Text; begin JsonObj := JsonTok.AsObject; Exit(SelectJsonToken(JsonObj, '$.answer')); end; procedure SelectJsonToken(JsonObject: JsonObject; Path: text): text; var JsonToken: JsonToken; begin if not JsonObject.SelectToken(Path, JsonToken) then exit(''); if JsonToken.AsValue.IsNull then exit(''); exit(jsontoken.asvalue.astext); end; }
And Page:
page 50110 "BotPage" { PageType = Card; ApplicationArea = All; UsageCategory = Documents; Caption = 'QnA Bot Page'; LinksAllowed = false; layout { area(Content) { group(GroupName) { field(Question; Question) { ApplicationArea = All; trigger OnValidate() var BotMgt: Codeunit BotMgt; begin Answer := BotMgt.SendQuestion(Question); Question := ''; CurrPage.Update(false); end; } field(Answer; Answer) { ApplicationArea = All; Editable = false; MultiLine = true; } } } } var Question: Text; Answer: Text; }
15. Now it's time to publish it and check. Say Hello to your bot and get Hello back :)
16. Let's add to our bot some QnA pairs. Open your knowledge base and press +Add QnA pair. My bot will send a link to Microsoft docs when user ask about Business central functionality. Press Save and Train button and after that Publish again.
17. Now ask bot about Customer card:
And you'll get an answer:
18. Hope you'll get a lot of fun playing with bot. Also it could be used for answering on end users simple questions and not disturbing you. Happy coding!
*This post is locked for comments