app.js
1.69 KB
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
const express = require('express');
const axios = require('axios').default;
const FormData = require('form-data');
const constants = require('./constants');
const app = express();
app.use(express.json());
app.get('/', (req, res) => {
res.send('Hello world!');
});
app.get('/notify', (req, res) => {
const url = 'https://notify-api.line.me/api/notify'
const form = new FormData();
form.append('message', 'Line Notify Testing...');
form.append('stickerPackageId', 1);
form.append('stickerId', 1);
axios.post(url, form, { headers: form.getHeaders({ authorization: `Bearer ${constants.LINE.TOKEN}` })})
.then(notifyResponse => {
console.log(notifyResponse);
res.status(200);
res.send('Request Success');
}).catch(err => {
console.log(err);
res.send('Request Error');
});
})
app.post('/webhook', (req, res) => {
const { body } = req;
const eventObj = body.events[0];
const { source, message, replyToken } = eventObj;
// request log
console.log('======================', new Date() ,'======================');
console.log('[request]', body);
console.log('[request source] ', source);
console.log('[request message]', message);
const url = 'https://api.line.me/v2/bot/message/reply'
axios.post(url, {
replyToken,
messages: [
{
type: 'text',
text: 'Hello!',
},
{
type: 'text',
text: 'May I Help U?',
},
],
}, {
headers: {
authorization: `Bearer ${constants.LINE.TOKEN}`,
'Content-Type': 'application/json',
},
}).then(notifyResponse => {
res.status(200);
res.send('Success');
}).catch(err => {
res.send('Failed');
});
});
module.exports = app;