Seongwook Jin

Reply, Push, Trans Added

1 +const request = require('request');
2 +const TARGET_URL = 'https://api.line.me/v2/bot/message/push'
3 +const MULTI_TARGET_URL = 'https://api.line.me/v2/bot/message/multicast'
4 +const BROAD_TARGET_URL = 'https://api.line.me/v2/bot/message/broadcast'
5 +const TOKEN = '채널 토큰으로 교체'
6 +const USER_ID = '사용자의 ID, 메세지 수신 시에 확인할 수 있음'
7 +
8 +// Single User
9 +// request.post(
10 +// {
11 +// url: TARGET_URL,
12 +// headers: {
13 +// 'Authorization': `Bearer ${TOKEN}`
14 +// },
15 +// json: {
16 +// "to": `${USER_ID}`,
17 +// "messages":[
18 +// {
19 +// "type":"text",
20 +// "text":"Hello, user"
21 +// },
22 +// {
23 +// "type":"text",
24 +// "text":"May I help you?"
25 +// }
26 +// ]
27 +// }
28 +// },(error, response, body) => {
29 +// console.log(body)
30 +// });
31 +
32 +
33 +// Multicast User
34 +// request.post(
35 +// {
36 +// url: MULTI_TARGET_URL,
37 +// headers: {
38 +// 'Authorization': `Bearer ${TOKEN}`
39 +// },
40 +// json: {
41 +// "to": [`${USER_ID}`],
42 +// "messages":[
43 +// {
44 +// "type":"text",
45 +// "text":"Hello, user"
46 +// },
47 +// {
48 +// "type":"text",
49 +// "text":"May I help you?"
50 +// }
51 +// ]
52 +// }
53 +// },(error, response, body) => {
54 +// console.log(body)
55 +// });
56 +
57 +
58 +// Broadcast
59 + request.post(
60 + {
61 + url: BROAD_TARGET_URL,
62 + headers: {
63 + 'Authorization': `Bearer ${TOKEN}`
64 + },
65 + json: {
66 + "messages":[
67 + {
68 + "type":"text",
69 + "text":"Hello, user"
70 + },
71 + {
72 + "type":"text",
73 + "text":"May I help you?"
74 + }
75 + ]
76 + }
77 + },(error, response, body) => {
78 + console.log(body)
79 + });
...\ No newline at end of file ...\ No newline at end of file
This diff is collapsed. Click to expand it.
1 +{
2 + "name": "push",
3 + "version": "1.0.0",
4 + "description": "",
5 + "main": "app.js",
6 + "scripts": {
7 + "test": "echo \"Error: no test specified\" && exit 1"
8 + },
9 + "author": "",
10 + "license": "ISC",
11 + "dependencies": {
12 + "request": "^2.88.2"
13 + }
14 +}
1 +var express = require('express');
2 +const request = require('request');
3 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
4 +const TOKEN = '채널 토큰으로 교체 해야 함'
5 +
6 +const bodyParser = require('body-parser');
7 +var app = express();
8 +app.use(bodyParser.json());
9 +app.post('/hook', function (req, res) {
10 +
11 + var eventObj = req.body.events[0];
12 + var source = eventObj.source;
13 + var message = eventObj.message;
14 +
15 + // request log
16 + console.log('======================', new Date() ,'======================');
17 + console.log('[request]', req.body);
18 + console.log('[request source] ', eventObj.source);
19 + console.log('[request message]', eventObj.message);
20 +
21 + request.post(
22 + {
23 + url: TARGET_URL,
24 + headers: {
25 + 'Authorization': `Bearer ${TOKEN}`
26 + },
27 + json: {
28 + "replyToken":eventObj.replyToken,
29 + "messages":[
30 + {
31 + "type":"text",
32 + "text":"Hello, user"
33 + },
34 + {
35 + "type":"text",
36 + "text":"May I help you?"
37 + }
38 + ]
39 + }
40 + },(error, response, body) => {
41 + console.log(body)
42 + });
43 +
44 +
45 + res.sendStatus(200);
46 +});
47 +
48 +var server = app.listen(23023, function () {
49 + var host = server.address().address
50 + var port = server.address().port
51 + console.log("Example app listening at http://%s:%s", host, port)
52 +})
This diff is collapsed. Click to expand it.
1 +{
2 + "name": "reply",
3 + "version": "1.0.0",
4 + "description": "",
5 + "main": "app.js",
6 + "scripts": {
7 + "test": "echo \"Error: no test specified\" && exit 1"
8 + },
9 + "author": "",
10 + "license": "ISC",
11 + "dependencies": {
12 + "body-parser": "^1.19.0",
13 + "express": "^4.17.1",
14 + "request": "^2.88.2"
15 + }
16 +}
1 +var express = require('express');
2 +const request = require('request');
3 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
4 +const TOKEN = '채널 토큰으로 교체'
5 +const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
6 +const PAPAGO_ID = '네이버 클라이언트 ID'
7 +const PAPAGO_SECRET = '네이버 클라이언트 Secret'
8 +
9 +const bodyParser = require('body-parser');
10 +var app = express();
11 +app.use(bodyParser.json());
12 +app.post('/hook', function (req, res) {
13 +
14 + var eventObj = req.body.events[0];
15 + var source = eventObj.source;
16 + var message = eventObj.message;
17 +
18 + // request log
19 + console.log('======================', new Date() ,'======================');
20 + console.log('[request]', req.body);
21 + console.log('[request source] ', eventObj.source);
22 + console.log('[request message]', eventObj.message);
23 +
24 + trans(eventObj.replyToken, eventObj.message.text);
25 +
26 +
27 + res.sendStatus(200);
28 +});
29 +
30 +function trans(replyToken, message) {
31 +
32 + request.post(
33 + {
34 + url: PAPAGO_URL,
35 + headers: {
36 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
37 + 'X-Naver-Client-Id': `${PAPAGO_ID}`,
38 + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
39 + },
40 + body: 'source=ko&target=en&text=' + message,
41 + json:true
42 + },(error, response, body) => {
43 + if(!error && response.statusCode == 200) {
44 + console.log(body.message);
45 + var transMessage = body.message.result.translatedText;
46 + request.post(
47 + {
48 + url: TARGET_URL,
49 + headers: {
50 + 'Authorization': `Bearer ${TOKEN}`
51 + },
52 + json: {
53 + "replyToken":replyToken,
54 + "messages":[
55 + {
56 + "type":"text",
57 + "text":transMessage
58 + }
59 + ]
60 + }
61 + },(error, response, body) => {
62 + console.log(body)
63 + });
64 + }
65 + });
66 +
67 +}
68 +
69 +var server = app.listen(23023, function () {
70 + var host = server.address().address
71 + var port = server.address().port
72 + console.log("Example app listening at http://%s:%s", host, port)
73 +})
This diff is collapsed. Click to expand it.
1 +{
2 + "name": "trans",
3 + "version": "1.0.0",
4 + "description": "",
5 + "main": "app.js",
6 + "scripts": {
7 + "test": "echo \"Error: no test specified\" && exit 1"
8 + },
9 + "author": "",
10 + "license": "ISC",
11 + "dependencies": {
12 + "express": "^4.17.1",
13 + "request": "^2.88.2"
14 + }
15 +}