This repository has been archived by the owner on Jun 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqna.js
92 lines (75 loc) · 3.07 KB
/
qna.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';
let fs = require ('fs');
let https = require ('https');
// **********************************************
// *** Update or verify the following values. ***
// **********************************************
// NOTE: Replace this with a valid host name.
// let host = "askaboutblockchain.azurewebsites.net";
let host = "ehubbot.azurewebsites.net";
// NOTE: Replace this with a valid endpoint key.
// This is not your subscription key.
// To get your endpoint keys, call the GET /endpointkeys method.
// let endpoint_key = "ba5eac5c-0ec8-479a-80bd-8c527ae060d7";
let endpoint_key = "189f6541-fbcc-449a-a035-9f3729a27f19";
//
// NOTE: Replace this with a valid knowledge base ID.
// Make sure you have published the knowledge base with the
// POST /knowledgebases/{knowledge base ID} method.
// let kb = "275be859-8d17-4817-876f-2e4cbaa369d3";
let kb = "41556151-d5f0-4d19-a1d1-ac399f8b671a";
let method = "/qnamaker/knowledgebases/" + kb + "/generateAnswer";
let question = {"question":"what is this?"};
let pretty_print = function (s) {
return JSON.stringify(JSON.parse(s), null, 4);
}
// callback is the function to call when we have the entire response.
let response_handler = function (callback, response) {
let body = '';
response.on ('data', function (d) {
body += d;
});
response.on ('end', function () {
// Call the callback function with the status code, headers, and body of the response.
callback ({ status : response.statusCode, headers : response.headers, body : body });
});
response.on ('error', function (e) {
console.log ('Error: ' + e.message);
});
};
// Get an HTTP response handler that calls the specified callback function when we have the entire response.
let get_response_handler = function (callback) {
// Return a function that takes an HTTP response, and is closed over the specified callback.
// This function signature is required by https.request, hence the need for the closure.
return function (response) {
response_handler (callback, response);
}
}
// callback is the function to call when we have the entire response from the POST request.
let post = function (path, content, callback) {
let request_params = {
method : 'POST',
hostname : host,
path : path,
headers : {
'Content-Type' : 'application/json',
'Content-Length' : content.length,
'Authorization' : 'EndpointKey ' + endpoint_key,
}
};
// Pass the callback function to the response handler.
let req = https.request (request_params, get_response_handler (callback));
req.write (content);
req.end ();
}
// callback is the function to call when we have the response from the /knowledgebases POST method.
let get_answers = function (path, req, callback) {
console.log ('Calling ' + host + path + '.');
// Send the POST request.
post (path, req, function (response) {
callback (response.body);
});
}
// Convert the request to a string.
let content = JSON.stringify(question);
module.exports = {get_answers, method, content, pretty_print};