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.
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:
Hope this will resolve your problem.
For more details you could refer this official docs