app.js
1.55 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
const express = require('express');
const app = express();
const mysql = require('mysql2/promise');
const axios = require('axios').default;
const bodyParser = require('body-parser');
app.use(bodyParser.json());
const channelToken = 'Bearer ZVI571ynUAVT0U44734ziyjVtStYiUi2UA4txCgVUvugv5YQwToMDgE991Sd4UbsZtJpBhXEtQKX/YkvQVx0PgH9F7qy75eUR0iKMwz33RXD5BG8JuFZ67FqR/GXZQNSk+kXHMBVnCQPeeNqTNn5MgdB04t89/1O/w1cDnyilFU=';
const pool = mysql.createPool({
host: 'localhost',
user: 'root',
password: '0712',
database: 'tft'
});
app.post('/messages', function(req, res) {
for(let event of req.body.events) {
if(event.type == 'message')
onMessage(event.message, event.replyToken);
}
res.sendStatus(200);
});
function onMessage(message, replyToken) {
if(message.type != 'text')
return;
let sql = 'SELECT i.`name` FROM `character_items` c JOIN `items` i ON c.`item`=i.`id` WHERE c.`character_id`=? ORDER BY c.`count` DESC LIMIT 0,5';
pool.query(sql, [message.text]).then(function([results]) {
let reply;
if(results.length == 0)
reply = '데이터가 없습니다.';
else
reply = `${message.text}의 추천 아이템은 ${results.map(e => e.name).join(', ')} 입니다.`;
axios.post('https://api.line.me/v2/bot/message/reply', {
replyToken: replyToken,
messages: [{type: 'text', text: reply}]
}, {
headers: {'Authorization': channelToken}
});
});
}
app.listen(3000, () => console.log('TFT Chatbot started'));