2

I'm working on a V4 MS Bot framework, I want to build a functionality such as, when user starts typing the question, the bot should populate and give the exact question phrases, so that the user can click on the suggestions given by the bot. Hence the user's effort is reduced and chances of errors is reduced.

So I'm writing a function in client side (using plain JavaScript), which get called on key press.

I used the following code to build that function.

            $( "[aria-label='Sendbox']" ).keypress(function() {
        
            if($( "[aria-label='Sendbox']" )[0].defaultValue.length >3){
    
                getquestion(this.value);
            }
        });

Inside the function i want to create a REST API call and get the questions related to the keyword entered by the user.

function getquestion(value)
{
    var params = value;

        $.ajax({
            url: "https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/knowledgebases/create?" + $.param(params),
            beforeSend: function(xhrObj){
            // Request headers
            xhrObj.setRequestHeader("Content-Type","application/json");
            xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","<key>");
        },
        type: "GET",
        // Request body
        //data: "{body}",
    })
    .done(function(data) {
        alert("success");
    })
    .fail(function(data) {
        alert("error");
    });
}

Using this function i can only reach to my Knowledgebase, but unable to narrow to query the questions related to the keywords.

can anyone please help me to achieve my requirement.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Jegan Baskaran
  • 337
  • 2
  • 16
  • Try to request like given format hope that will helps. – Md Farid Uddin Kiron Jan 22 '20 at 11:53
  • @MdFaridUddinKiron, thanks for helping me, you solution worked for me. Also, i want to know how to add filter on the query, suppose i want to view only the answers in the editorial source, so where should i put the filter ? – Jegan Baskaran Jan 23 '20 at 09:17

1 Answers1

2

You could try following code snippet to request QnA maker endpoint and get the answer from there.

Correct Request Format In Jquery:

  $("#btnQnAMakerAnswer").click(function () {
        var question = {
            question: "will you marry me"
        }
        $.ajax({
            type: "POST",
            url: "https://YourEndPointURL/qnamaker/knowledgebases/eb895acb-e034-4f7c-asda7c-1955458ecec6/generateAnswer",
            data: JSON.stringify(question),
            beforeSend: function (xhr) {
                xhr.setRequestHeader('Authorization','EndpointKey c44444_Your_Endpoint_Key_4556');
            },
            dataType: "json",
            contentType: "application/json",
            success: function (data) {
                console.log(data);
                console.log(data.answers[0].answer);   
            }
        });


    });

Response From QnA Endpoint:

enter image description here

Hope this will resolve your problem.

For more details you could refer this official docs

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
  • Awesome, thank you so much. this what i was expected – Jegan Baskaran Jan 23 '20 at 04:42
  • Also, Please let me know how to filter my answer with respect to the source ?, i mean if i want the answers only form the editorial source, where should i put a filter ? – Jegan Baskaran Jan 23 '20 at 04:44
  • I am not clear what do you exactly asking for. Would you kindly add sample with expectations in a different question that would be good for community as we want to make stackoverflow as a specific solution repository. Give me a link with new question will try to solve your problem. Thank you. – Md Farid Uddin Kiron Jan 23 '20 at 08:31
  • i've asked a new question here : https://stackoverflow.com/questions/59875337/how-to-add-filter-on-the-rest-api-query-to-view-the-answers-from-qnamaker – Jegan Baskaran Jan 23 '20 at 09:36
  • hope this new question will make you to understand my requirement and help me to fix it – Jegan Baskaran Jan 23 '20 at 09:37