Showing
1 changed file
with
11 additions
and
216 deletions
1 | -var express = require('express'); | 1 | +var linebot = require('linebot'); |
2 | -var app = express(); | ||
3 | -const line = require('@line/bot-sdk'); | ||
4 | 2 | ||
5 | -const config = { | 3 | +var bot = linebot({ |
6 | - channelAccessToken: 'L+RJ15lZgeSoWgeHf9BqgEhm8lYh11qALzPlDV85VGXHfmiKqj3CX1V9DvqakFeJqwWgfQRejsUIWqf/kJuTLQcRC5ws2pyJJbr0VFEXptoYicgiRDzuA51W91dTcFL6/olvLNS1zAf1xO2wxpTGvQdB04t89/1O/w1cDnyilFU=', | 4 | + channelId: '1653555207', |
7 | channelSecret: 'e8e139b4cf31d22ed234d62a9b336e74', | 5 | channelSecret: 'e8e139b4cf31d22ed234d62a9b336e74', |
8 | -}; | 6 | + channelAccessToken: 'L+RJ15lZgeSoWgeHf9BqgEhm8lYh11qALzPlDV85VGXHfmiKqj3CX1V9DvqakFeJqwWgfQRejsUIWqf/kJuTLQcRC5ws2pyJJbr0VFEXptoYicgiRDzuA51W91dTcFL6/olvLNS1zAf1xO2wxpTGvQdB04t89/1O/w1cDnyilFU=' |
9 | - | ||
10 | - | ||
11 | -// create LINE SDK client | ||
12 | -const client = new line.Client(config); | ||
13 | - | ||
14 | -// create Express app | ||
15 | -// about Express itself: https://expressjs.com/ | ||
16 | - | ||
17 | -// register a webhook handler with middleware | ||
18 | -// about the middleware, please refer to doc | ||
19 | -app.post('https://bots.dialogflow.com/line/d1584d25-0e52-48da-b6cc-20e021dc5e70/webhook', line.middleware(config), (req, res) => { | ||
20 | - Promise | ||
21 | - .all(req.body.events.map(handleEvent)) | ||
22 | - .then((result) => res.json(result)) | ||
23 | - .catch((err) => { | ||
24 | - console.error(err); | ||
25 | - res.status(200).end(); | ||
26 | - }); | ||
27 | -}); | ||
28 | - | ||
29 | -// event handler | ||
30 | -function handleEvent(event) { | ||
31 | - if (event.type !== 'message' || event.message.type !== 'text') { | ||
32 | - // ignore non-text-message event | ||
33 | - return Promise.resolve(null); | ||
34 | - } | ||
35 | - return new Promise(function (resolve, reject) { | ||
36 | - //내가 짠 것 | ||
37 | - function getup(shour, smin, ampm) {//몇시에 자려고 할때 언제 일어나면 좋을지 | ||
38 | - if (shour != '(hour)' && smin != '(minute)') { | ||
39 | - ; //탈출 | ||
40 | - var setTime = new Date(); | ||
41 | - | ||
42 | - if (shour == 12) { | ||
43 | - shour = 0; | ||
44 | - } | ||
45 | - | ||
46 | - if (ampm == "AM") { | ||
47 | - setTime.setHours(shour); | ||
48 | - } | ||
49 | - else if (ampm == "PM") { | ||
50 | - setTime.setHours(shour + 12); | ||
51 | - } | ||
52 | - | ||
53 | - setTime.setMinutes(smin); | ||
54 | - | ||
55 | - var res1 = new Date(setTime.getTime() - 270 * 60000); | ||
56 | - var res2 = new Date(res1.getTime() - 90 * 60000); | ||
57 | - var res3 = new Date(res2.getTime() - 90 * 60000); | ||
58 | - var res4 = new Date(res3.getTime() - 90 * 60000); | ||
59 | - | ||
60 | - function retDate(dateObj) { | ||
61 | - var formatted = ''; | ||
62 | - var pm = false; | ||
63 | - if (dateObj.getHours() > 12) { | ||
64 | - formatted = dateObj.getHours() - 12; | ||
65 | - pm = true; | ||
66 | - } | ||
67 | - else if (dateObj.getHours() < 12 && dateObj.getHours() != 0) { | ||
68 | - formatted = dateObj.getHours(); | ||
69 | - } | ||
70 | - else if (dateObj.getHours() == 0) { | ||
71 | - formatted = "12"; | ||
72 | - } | ||
73 | - else if (dateObj.getHours() == 12) { | ||
74 | - formatted = "12"; | ||
75 | - pm = true; | ||
76 | - } | ||
77 | - | ||
78 | - if (dateObj.getMinutes() < 10) { | ||
79 | - formatted = formatted + ":0" + dateObj.getMinutes(); | ||
80 | - } | ||
81 | - else { | ||
82 | - formatted = formatted + ":" + dateObj.getMinutes(); | ||
83 | - } | ||
84 | - | ||
85 | - if (pm == true) { | ||
86 | - formatted = formatted + " PM"; | ||
87 | - } | ||
88 | - else { | ||
89 | - formatted = formatted + " AM"; | ||
90 | - } | ||
91 | - return formatted; | ||
92 | - } | ||
93 | - | ||
94 | - console.log(String(retDate(res1))); | ||
95 | - console.log(String(retDate(res2))); | ||
96 | - console.log(String(retDate(res3))); | ||
97 | - console.log(String(retDate(res4))); | ||
98 | - | ||
99 | - $('#results').fadeIn(); | ||
100 | - $('#feedback').fadeIn(); | ||
101 | - $('#ad').fadeIn(); | ||
102 | - } // end hour/minute check if | ||
103 | - else { | ||
104 | - alert("Please select an hour and a minute before trying to calculate!"); | ||
105 | - } // end not-filled check | ||
106 | - };// end calculate | ||
107 | -})}; | ||
108 | - | ||
109 | - | ||
110 | -function sleep(ghour, gmin, ampm) { | ||
111 | - //$('#start').hide(); | ||
112 | - var zDate = new Date(); | ||
113 | - | ||
114 | - var res1 = new Date(zDate.getTime() + 104*60000); | ||
115 | - var res2 = new Date(res1.getTime() + 90*60000); | ||
116 | - var res3 = new Date(res2.getTime() + 90*60000); | ||
117 | - var res4 = new Date(res3.getTime() + 90*60000); | ||
118 | - var res5 = new Date(res4.getTime() + 90*60000); | ||
119 | - var res6 = new Date(res5.getTime() + 90*60000); | ||
120 | - | ||
121 | - function retDate(dateObj) { | ||
122 | - var formatted = ''; | ||
123 | - var pm = false; | ||
124 | - if(dateObj.getHours() > 12) { | ||
125 | - formatted = dateObj.getHours() - 12; | ||
126 | - pm = true; | ||
127 | - } else if(dateObj.getHours() < 12 && dateObj.getHours() != 0) { | ||
128 | - formatted = dateObj.getHours(); | ||
129 | - } else if(dateObj.getHours() == 0) { | ||
130 | - formatted = "12"; | ||
131 | - } else if(dateObj.getHours() == 12) { | ||
132 | - formatted = "12"; | ||
133 | - pm = true; | ||
134 | - } | ||
135 | - if(dateObj.getMinutes() < 10) { | ||
136 | - formatted = formatted + ":0" + dateObj.getMinutes(); | ||
137 | - } else { | ||
138 | - formatted = formatted + ":" + dateObj.getMinutes(); | ||
139 | - } | ||
140 | - if(pm == true) { | ||
141 | - formatted = formatted + " PM"; | ||
142 | - } else { | ||
143 | - formatted = formatted + " AM"; | ||
144 | - } | ||
145 | - return formatted; | ||
146 | - } | ||
147 | - | ||
148 | - console.log(String(retDate(res1))); | ||
149 | - console.log(String(retDate(res2))); | ||
150 | - console.log(String(retDate(res3))); | ||
151 | - console.log(String(retDate(res4))); | ||
152 | - console.log(String(retDate(res5))); | ||
153 | - console.log(String(retDate(res6))); | ||
154 | - | ||
155 | - $('#resultsNow').fadeIn(); | ||
156 | - $('#feedback').fadeIn(); | ||
157 | - $('#ad').fadeIn(); | ||
158 | }); | 7 | }); |
159 | -}); | ||
160 | -/* | ||
161 | - //언어 감지 option | ||
162 | - var detect_options = { | ||
163 | - url : languagedetect_api_url, | ||
164 | - form : {'query': event.message.text}, | ||
165 | - headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} | ||
166 | - }; | ||
167 | - | ||
168 | - //papago 언어 감지 | ||
169 | - request.post(detect_options,function(error,response,body){ | ||
170 | - console.log(response.statusCode); | ||
171 | - if(!error && response.statusCode == 200){ | ||
172 | - var detect_body = JSON.parse(response.body); | ||
173 | - var source = ''; | ||
174 | - var target = ''; | ||
175 | - var result = { type: 'text', text:''}; | ||
176 | - | ||
177 | - //언어 감지가 제대로 됐는지 확인 | ||
178 | - console.log(detect_body.langCode); | ||
179 | - | ||
180 | - | ||
181 | - //번역은 한국어->영어 / 영어->한국어만 지원 | ||
182 | - if(detect_body.langCode == 'ko'||detect_body.langCode == 'en'){ | ||
183 | - source = detect_body.langCode == 'ko' ? 'ko':'en'; | ||
184 | - target = source == 'ko' ? 'en':'ko'; | ||
185 | - //papago 번역 option | ||
186 | - var options = { | ||
187 | - url: translate_api_url, | ||
188 | - // 한국어(source : ko), 영어(target: en), 카톡에서 받는 메시지(text) | ||
189 | - form: {'source':source, 'target':target, 'text':event.message.text}, | ||
190 | - headers: {'X-Naver-Client-Id': client_id, 'X-Naver-Client-Secret': client_secret} | ||
191 | - }; | ||
192 | - | ||
193 | - // Naver Post API | ||
194 | - request.post(options, function(error, response, body){ | ||
195 | - // Translate API Sucess | ||
196 | - if(!error && response.statusCode == 200){ | ||
197 | - // JSON | ||
198 | - var objBody = JSON.parse(response.body); | ||
199 | - // Message 잘 찍히는지 확인 | ||
200 | - | ||
201 | - result.text = objBody.message.result.translatedText; | ||
202 | - console.log(result.text); | ||
203 | - //번역된 문장 보내기 | ||
204 | - client.replyMessage(event.replyToken,result).then(resolve).catch(reject); | ||
205 | - } | ||
206 | - }); | ||
207 | - } | ||
208 | - // 메시지의 언어가 영어 또는 한국어가 아닐 경우 | ||
209 | - else{ | ||
210 | - result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.'; | ||
211 | - client.replyMessage(event.replyToken,result).then(resolve).catch(reject); | ||
212 | - } | ||
213 | - | ||
214 | - } | ||
215 | 8 | ||
9 | +bot.on('message', function (event) { | ||
10 | + event.reply(event.message.text).then(function (data) { | ||
11 | + // success | ||
12 | + }).catch(function (error) { | ||
13 | + // error | ||
216 | }); | 14 | }); |
217 | -*/ | ||
218 | - | ||
219 | - | ||
220 | -app.listen(3000, function () { | ||
221 | - console.log('Linebot listening on port 3000!'); | ||
222 | }); | 15 | }); |
16 | + | ||
17 | +bot.listen('/linewebhook', 3000); | ... | ... |
-
Please register or login to post a comment