Showing
4 changed files
with
67 additions
and
1 deletions
app.js
0 → 100644
1 | +const express = require('express'); | ||
2 | +const app = express(); | ||
3 | +const mysql = require('mysql2/promise'); | ||
4 | +const axios = require('axios').default; | ||
5 | +const bodyParser = require('body-parser'); | ||
6 | + | ||
7 | +app.use(bodyParser.json()); | ||
8 | + | ||
9 | +const channelToken = 'Bearer ZVI571ynUAVT0U44734ziyjVtStYiUi2UA4txCgVUvugv5YQwToMDgE991Sd4UbsZtJpBhXEtQKX/YkvQVx0PgH9F7qy75eUR0iKMwz33RXD5BG8JuFZ67FqR/GXZQNSk+kXHMBVnCQPeeNqTNn5MgdB04t89/1O/w1cDnyilFU='; | ||
10 | + | ||
11 | +const pool = mysql.createPool({ | ||
12 | + host: 'localhost', | ||
13 | + user: 'root', | ||
14 | + password: '0712', | ||
15 | + database: 'tft' | ||
16 | +}); | ||
17 | + | ||
18 | +app.post('/messages', function(req, res) { | ||
19 | + for(let event of req.body.events) { | ||
20 | + if(event.type == 'message') | ||
21 | + onMessage(event.message, event.replyToken); | ||
22 | + } | ||
23 | + | ||
24 | + res.sendStatus(200); | ||
25 | +}); | ||
26 | + | ||
27 | +function onMessage(message, replyToken) { | ||
28 | + if(message.type != 'text') | ||
29 | + return; | ||
30 | + | ||
31 | + 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'; | ||
32 | + pool.query(sql, [message.text]).then(function([results]) { | ||
33 | + let reply; | ||
34 | + if(results.length == 0) | ||
35 | + reply = '데이터가 없습니다.'; | ||
36 | + else | ||
37 | + reply = `${message.text}의 추천 아이템은 ${results.map(e => e.name).join(', ')} 입니다.`; | ||
38 | + | ||
39 | + axios.post('https://api.line.me/v2/bot/message/reply', { | ||
40 | + replyToken: replyToken, | ||
41 | + messages: [{type: 'text', text: reply}] | ||
42 | + }, { | ||
43 | + headers: {'Authorization': channelToken} | ||
44 | + }); | ||
45 | + }); | ||
46 | +} | ||
47 | + | ||
48 | +app.listen(3000, () => console.log('TFT Chatbot started')); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
package-lock.json
0 → 100644
This diff is collapsed. Click to expand it.
package.json
0 → 100644
1 | +{ | ||
2 | + "name": "tft-chatbot", | ||
3 | + "version": "1.0.0", | ||
4 | + "description": "", | ||
5 | + "main": "index.js", | ||
6 | + "scripts": { | ||
7 | + "test": "echo \"Error: no test specified\" && exit 1" | ||
8 | + }, | ||
9 | + "author": "", | ||
10 | + "license": "ISC", | ||
11 | + "dependencies": { | ||
12 | + "axios": "^0.21.0", | ||
13 | + "body-parser": "^1.19.0", | ||
14 | + "express": "^4.17.1", | ||
15 | + "mysql2": "^2.2.5" | ||
16 | + } | ||
17 | +} |
-
Please register or login to post a comment