-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebHook.js
54 lines (45 loc) · 1.25 KB
/
WebHook.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
// webhook
// 2019, 12/25, by Queenie
// 注入相關套件
var express = require('express');
var router = express.Router();
const request = require('request-promise');
const AssistantV1 = require('ibm-watson/assistant/v1');
const { IamAuthenticator } = require('ibm-watson/auth');
// var
// Object of IBM ChatBot
const assistant = new AssistantV1({
version: '2018-02-28',
authenticator: new IamAuthenticator({
apikey: ' ', // change
}),
url: 'https://gateway.watsonplatform.net/assistant/api', // change
});
// WebHook Block
// Line hooked on IBM
router.post('/linebot', async function(req, res, next) {
console.log(req.body)
res.sendStatus(200);
let words = req.body.events[0].message.text;
let assistantResult = await askAssistant(words); // call function
console.log(assistantResult)
//let lineMsg = assistantResult.lineMsg;
//let lineMsg = toLineFormat()
//callLineSendMsgAPI
});
// function defined
// call var
function askAssistant(words){
return assistant.message({
workspaceId: ' ',
input: {'text': words}
})
.then(res => {
//console.log(JSON.stringify(res.result, null, 2));
return res.result
})
.catch(err => {
console.log(err)
});
}
module.exports = router; // 實際命名由路由導向名稱的變數為主