forked from febritecno/hasana-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
264 lines (241 loc) · 9.36 KB
/
index.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
'use strict';
const line = require('@line/bot-sdk');
const express = require('express');
var request = require('request');
var http = require('http');
// create LINE SDK config from env variables
const config = {
channelAccessToken: process.env.CAT,
channelSecret: process.env.CS,
};
// create LINE SDK client
const client = new line.Client(config);
// create Express app
// about Express itself: https://expressjs.com/
const app = express();
// register a webhook handler with middleware
app.post('/webhook', line.middleware(config), (req, res) => {
Promise
.all(req.body.events.map(handleEvent))
.then((result) => res.json(result))
.catch((err) => {
console.error(err);
res.status(500).end();
});
});
setInterval(function() {
http.get(process.env.URL);
}, 600000); // every 10 minutes
// event handler
function handleEvent(event) {
if (event.type !== 'message' || event.message.type !== 'text') {
// ignore non-text-message event
return Promise.resolve(null);
}
var options1 = {
method: 'GET',
url: 'http://api.susi.ai/susi/chat.json',
qs: {
timezoneOffset: '-330',
q: event.message.text
}
};
if (event.message.text.toLowerCase() === "who febrian?" || event.message.text.toLowerCase() === "Who febrian?"){
const answer = {
"type": "template",
"altText": "Febrian Dwi Putra is humble people with skill programing and design",
"template": {
"type": "buttons",
"actions": [
{
"type": "uri",
"label": "Facebook",
"uri": "https://www.facebook.com/febri.krn"
},
{
"type": "uri",
"label": "Linkedin",
"uri": "https://www.linkedin.com/in/febrian-dwi-putra-026446163"
},
{ "type": "uri",
"label": "Github",
"uri": "https://github.com/febritecno"
}
],
"thumbnailImageUrl": "https://avatars2.githubusercontent.com/u/9696688?s=460&v=4",
"title": "Febrian Dwi Putra",
"text": "People can solve and make your dream realise"
}
};
return client.replyMessage(event.replyToken, answer);
}else if (event.message.text.toLowerCase() === "help"){
const answer = {
"type": "template",
"altText": "help about bot",
"template": {
"type": "buttons",
"actions": [
{
"type": "message",
"label": "Show Skill",
"text": "what can you do ?"
}
],
"thumbnailImageUrl": "https://dkru86weszx9t.cloudfront.net/blog/wp-content/uploads/2018/05/how-to-ask-for-help-760x400.jpg",
"title": "Show help",
"text": "You can get help now"
}
};
return client.replyMessage(event.replyToken, answer);
}else if (event.message.text.toLowerCase() === "what can you do ?"){
const answer = {
"type": "text",
"text": "you can visit https://skills.susi.ai/ , then you look example reference my skill. happy chatting :)"
};
const answer1 = {
"type": "sticker",
"packageId": "1",
"stickerId": "4"
};
return client.replyMessage(event.replyToken, [answer,answer1]);
} else if (event.message.text.toLowerCase() === "") {
request(options1, function(error1, response1, body1) {
if (error1) throw new Error(error1);
// answer fetched from api susi
var ans = (JSON.parse(body1)).answers[0].actions[0].expression;
const sampleQ = [{
type: 'text',
text: ans
}
]
return client.replyMessage(event.replyToken, sampleQ);
});
} else {
request(options1, function(error1, response1, body1) {
if (error1) throw new Error(error1);
// answer fetched from api susi
var type = (JSON.parse(body1)).answers[0].actions;
var ans = (JSON.parse(body1)).answers[0].actions[0].expression;
if ( ((JSON.parse(body1)).answers[0].data[0].lon) || ((JSON.parse(body1)).answers[0].data[0].lat) ) {
var lat = JSON.parse(body1).answers[0].data[0].lat;
var lon = JSON.parse(body1).answers[0].data[0].lon;
var address = JSON.parse(body1).answers[0].data[0].locationInfo;
var title = JSON.parse(body1).answers[0].data[0][1];
const answer = {
type: "location",
title: title,
address: title,
latitude: lat,
longitude: lon
};
// use reply API
return client.replyMessage(event.replyToken, answer)
.catch((err) => {
console.log('Error - '+err);
});
} else if (JSON.parse(body1).answers[0].data[0].type === 'gif') {
let videoUrl = JSON.parse(body1).answers[0].data[0].v1.original.mp4;
let previewUrl = JSON.parse(body1).answers[0].data[0].images["480w_still"].url;
const answer = {
type: 'video',
originalContentUrl: videoUrl,
previewImageUrl: previewUrl
};
// use reply API
return client.replyMessage(event.replyToken, answer)
.catch((err) => {
console.log('Error - '+err);
});
} else if (type.length == 1 && type[0].type == "answer") {
let answer;
if((JSON.parse(body1)).answers[0].data[0].type === 'photo'){
answer = {
type: 'image',
originalContentUrl: ans,
previewImageUrl: ans
};
} else {
answer = {
type: 'text',
text: ans
};
}
// use reply API
return client.replyMessage(event.replyToken, answer);
} else if (type[0].type == "table") {
var data = JSON.parse(body1).answers[0].data;
var columns = type[0].columns;
var key = Object.keys(columns);
var msg = [];
console.log(key);
for (var i = 0; i < 5; i++) {
msg[i] = "";
msg[i] = {
type: 'text',
text: key[0].toUpperCase() + ": " + data[i][key[0]] + "\n" + key[1].toUpperCase() + ": " + data[i][key[1]] + "\n" + key[2].toUpperCase() + ": " + data[i][key[2]]
}
}
return client.replyMessage(event.replyToken, msg);
} else if (type.length == 2 && type[1].type == "rss") {
var data = JSON.parse(body1).answers[0].data;
var columns = type[1];
var key = Object.keys(columns);
var msg, title, link, query;
var carousel = [];
console.log(key);
for (var i = 1; i < 4; i++) {
title = key[1].toUpperCase() + ": " + data[i][key[1]];
query = title;
msg = key[2].toUpperCase() + ": " + data[i][key[2]];
link = data[i][key[3]]
if (title.length >= 40) {
title = title.substring(0, 36);
title = title + "...";
}
if (msg.length >= 60) {
msg = msg.substring(0, 56);
msg = msg + "...";
}
carousel[i] = {
"title": title,
"text": msg,
"actions": [{
"type": "uri",
"label": "View detail",
"uri": link
},
{
"type": "message",
"label": "Ask Hasana again",
"text": query
}
]
};
}
const answer = [{
type: 'text',
text: ans
},
{
"type": "template",
"altText": "Web Search",
"template": {
"type": "carousel",
"columns": [
carousel[1],
carousel[2],
carousel[3]
]
}
}
]
return client.replyMessage(event.replyToken, answer);
}
})
}
}
// listen on port
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`listening on ${port}`);
});