This repository has been archived by the owner on May 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
conversation.js
55 lines (52 loc) · 1.59 KB
/
conversation.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
/**
*
* main() will be invoked when you Run This Action.
*
* @param OpenWhisk actions accept a single parameter,
* which must be a JSON object.
*
* In this case, the params variable will look like:
* { "message": "xxxx" }
*
* @return which must be a JSON object.
* It will be the output of this action.
*
*/
var request = require('request');
function main(params) {
var username = params.username
var password = params.password
var iamApiKey = params.iamApiKey
var workspace_id = params.workspace_id
var auth = {"user": username,"pass":password}
var url = "https://gateway.watsonplatform.net/assistant/api/v1/workspaces/" + workspace_id + "/message?version=2018-07-10"
if(iamApiKey){
auth = {"user": "apikey","pass":iamApiKey}
url = "https://gateway-wdc.watsonplatform.net/assistant/api/v1/workspaces/" + workspace_id + "/message?version=2018-07-10"
}
var input_text = params.data
var body = {"input": {"text": input_text}}
return new Promise(function(resolve, reject) {
request( {
url: url,
method: 'POST',
auth: auth,
headers: {
"content-type": "application/json",
},
body: JSON.stringify({"input": {
"text": input_text
}})
},
function(error, response, body) {
if (error) {
reject(error);
}
else {
var output = JSON.parse(body)
resolve({msg: output});
}
});
}
);
}