Guest User

Untitled

a guest
Mar 19th, 2018
1,184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.75 KB | None | 0 0
  1. <index.js>
  2.  
  3. 'use strict';
  4.  
  5. var express = require('express');
  6. var app = express();
  7. var bodyParser = require('body-parser');
  8. var request = require('request');
  9. var async = require('async');
  10. var crypto = require('crypto');
  11. var https = require('https');
  12. var qs = require('qs');
  13. const line = require('@line/bot-sdk');
  14.  
  15. const client = new line.Client({
  16. channelAccessToken: '---my Channel Access Token---'
  17. });
  18.  
  19. app.use(express.static('public'));
  20.  
  21. app.set('port', (process.env.PORT || 8000));
  22. app.use(bodyParser.urlencoded({extended: true})); // JSONの送信を許可
  23. app.use(bodyParser.json()); // JSONのパースを楽に(受信時)
  24.  
  25. app.post('/callback', function(req, res) {
  26.  
  27. async.waterfall([
  28. function(callback) {
  29.  
  30. console.log('req.headers is: ' + req.headers['x-line-signature']);
  31. console.log('req.body is: ' + req.body);
  32. console.log('validating result: ' + validate_signature(req.headers['x-line-signature'], req.body));
  33.  
  34. // 捕捉されない例外のスタックトレースを表示
  35. process.on('unhandledRejection', console.dir);
  36.  
  37. // リクエストがLINE Platformから送られてきたか確認する
  38. if (!validate_signature(req.headers['x-line-signature'], req.body)) {
  39. return;
  40. }
  41.  
  42. // テキストが送られてきた場合のみ返事をする
  43. if ((req.body['events'][0]['type'] != 'message') || (req.body['events'][0]['message']['type'] != 'text')) {
  44. return;
  45. }
  46.  
  47.  
  48.  
  49. //署名検証
  50. function validate_signature(signature, body) {
  51. // return signature == crypto.createHmac('sha256', process.env.LINE_CHANNEL_SECRET).update(Buffer.from(JSON.stringify(body), 'utf8')).digest('base64');
  52. return signature == crypto.createHmac('sha256', '3126eead2c2301a08ff7113b4a4c1347').update(Buffer.from(JSON.stringify(body), 'utf8')).digest('base64');
  53. }
  54.  
  55. const client = new line.Client({
  56. channelAccessToken: '---my Channel Access Token---'
  57. });
  58.  
  59. // 送信データ作成
  60. // テンプレートの内容
  61. let pushSendMessageObject = [{
  62. 'replyToken': req.body['events'][0]['replyToken'],
  63. "type": "template",
  64. "altText": "this is a buttons template",
  65. "template": {
  66. "type": "buttons",
  67. "title": "Spotifyにログインをお願いします。",
  68. "text": "login",
  69. "actions": [
  70. {
  71. "type": "uri",
  72. "label": "はーい",
  73. "uri": "https://accounts.spotify.com/authorize"
  74. },
  75. {
  76. "type": "postback",
  77. "label": "やめとく",
  78. "data": "action=cancel&selectId=2"
  79. },
  80. ]
  81. }
  82. }];
  83.  
  84. console.log('2 done');
  85. callback(null, req, pushSendMessageObject);
  86. },
  87. function (req, pushSendMessageObject, callback){
  88. client.replyMessage(req.body['events'][0]['replyToken'], pushSendMessageObject);
  89. console.log('1 done');
  90.  
  91. setTimeout(function(){
  92. callback(null);
  93. }, 50);
  94. }
  95. ],function(err){
  96. if (err){
  97. throw err;
  98. console.log('This is an error message on the line 160');
  99. }
  100. }
  101. );
  102. });
  103.  
  104. app.listen(app.get('port'), function() {
  105. console.log('Node app is running');
  106. });
  107.  
  108. [$ heroku logs
  109. 2018-03-19T11:40:48.090391+00:00 app[web.1]: headers: [Object],
  110. 2018-03-19T11:40:48.090392+00:00 app[web.1]: config: [Object],
  111. 2018-03-19T11:40:48.090394+00:00 app[web.1]: request: [Object],
  112. 2018-03-19T11:40:48.090396+00:00 app[web.1]: data: [Object] } } }
  113. 2018-03-19T11:41:17.678669+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/callback" host=ciecho.herokuapp.com request_id=4ff26185-f921-4af8-928c-53a9c05a1142 fwd="203.104.146.153" dyno=web.1 connect=0ms service=30001ms status=503 bytes=0 protocol=https
  114. 2018-03-19T11:42:22.000000+00:00 app[api]: Build started by user chikai098353@gmail.com
  115. 2018-03-19T11:42:48.333058+00:00 app[api]: Deploy 096014ce by user chikai098353@gmail.com
  116. 2018-03-19T11:42:48.333058+00:00 app[api]: Release v152 created by user chikai098353@gmail.com
  117. 2018-03-19T11:42:22.000000+00:00 app[api]: Build succeeded
  118. 2018-03-19T11:42:49.235277+00:00 heroku[web.1]: Restarting
  119. 2018-03-19T11:42:49.236384+00:00 heroku[web.1]: State changed from up to starting
  120. 2018-03-19T11:42:50.364974+00:00 heroku[web.1]: Stopping all processes with SIGTERM
  121. 2018-03-19T11:42:50.435324+00:00 heroku[web.1]: Process exited with status 143
  122. 2018-03-19T11:42:54.908997+00:00 heroku[web.1]: Starting process with command `node index.js`
  123. 2018-03-19T11:42:57.963816+00:00 app[web.1]: Node app is running
  124. 2018-03-19T11:42:58.711373+00:00 heroku[web.1]: State changed from starting to up
  125. 2018-03-19T11:43:15.632079+00:00 app[web.1]: req.headers is: SB9MLEAATN0YtWS/Q0tVTWb1WagzlQcqKWVwXGzxgKQ=
  126. 2018-03-19T11:43:15.633614+00:00 app[web.1]: 2 done
  127. 2018-03-19T11:43:15.635680+00:00 app[web.1]: 1 done
  128. 2018-03-19T11:43:15.633226+00:00 app[web.1]: validating result: true
  129. 2018-03-19T11:43:15.632340+00:00 app[web.1]: req.body is: [object Object]
  130. 2018-03-19T11:43:16.227644+00:00 app[web.1]: { Error: Request failed with status code 403
  131. 2018-03-19T11:43:16.227656+00:00 app[web.1]: at wrapError (/app/node_modules/@line/bot-sdk/dist/http.js:10:15)
  132. 2018-03-19T11:43:16.227659+00:00 app[web.1]: at <anonymous>
  133. 2018-03-19T11:43:16.227660+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:188:7)
  134. 2018-03-19T11:43:16.227662+00:00 app[web.1]: statusCode: 403,
  135. 2018-03-19T11:43:16.227664+00:00 app[web.1]: statusMessage: 'Forbidden',
  136. 2018-03-19T11:43:16.227665+00:00 app[web.1]: originalError:
  137. 2018-03-19T11:43:16.227667+00:00 app[web.1]: { Error: Request failed with status code 403
  138. 2018-03-19T11:43:16.227668+00:00 app[web.1]: at createError (/app/node_modules/axios/lib/core/createError.js:16:15)
  139. 2018-03-19T11:43:16.227670+00:00 app[web.1]: at settle (/app/node_modules/axios/lib/core/settle.js:18:12)
  140. 2018-03-19T11:43:16.227690+00:00 app[web.1]: at IncomingMessage.handleStreamEnd (/app/node_modules/axios/lib/adapters/http.js:191:11)
  141. 2018-03-19T11:43:16.227692+00:00 app[web.1]: at emitNone (events.js:111:20)
  142. 2018-03-19T11:43:16.227694+00:00 app[web.1]: at IncomingMessage.emit (events.js:208:7)
  143. 2018-03-19T11:43:16.227698+00:00 app[web.1]: at _combinedTickCallback (internal/process/next_tick.js:138:11)
  144. 2018-03-19T11:43:16.227696+00:00 app[web.1]: at endReadableNT (_stream_readable.js:1056:12)
  145. 2018-03-19T11:43:16.227700+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:180:9)
  146. 2018-03-19T11:43:16.227703+00:00 app[web.1]: config:
  147. 2018-03-19T11:43:16.227704+00:00 app[web.1]: { adapter: [Function: httpAdapter],
  148. 2018-03-19T11:43:16.227706+00:00 app[web.1]: transformRequest: [Object],
  149. 2018-03-19T11:43:16.227708+00:00 app[web.1]: transformResponse: [Object],
  150. 2018-03-19T11:43:16.227709+00:00 app[web.1]: timeout: 0,
  151. 2018-03-19T11:43:16.227711+00:00 app[web.1]: xsrfCookieName: 'XSRF-TOKEN',
  152. 2018-03-19T11:43:16.227712+00:00 app[web.1]: xsrfHeaderName: 'X-XSRF-TOKEN',
  153. 2018-03-19T11:43:16.227714+00:00 app[web.1]: maxContentLength: -1,
  154. 2018-03-19T11:43:16.227715+00:00 app[web.1]: validateStatus: [Function: validateStatus],
  155. 2018-03-19T11:43:16.227717+00:00 app[web.1]: headers: [Object],
  156. 2018-03-19T11:43:16.227719+00:00 app[web.1]: method: 'post',
  157. 2018-03-19T11:43:16.227720+00:00 app[web.1]: url: 'https://api.line.me/v2/bot/message/reply',
  158. 2018-03-19T11:43:16.227729+00:00 app[web.1]: data: '{"messages":[{"replyToken":"f0e97d20499e4520be90046fb6541d8c","type":"template","altText":"this is a buttons template","template":{"type":"buttons","title":"Spotifyにログインをお願いします。","text":"login","actions":[{"type":"uri","label":"はーい","uri":"https://accounts.spotify.com/authorize"},{"type":"postback","label":"やめとく","data":"action=cancel&selectId=2"}]}}],"replyToken":"f0e97d20499e4520be90046fb6541d8c"}' },
  159. 2018-03-19T11:43:16.227731+00:00 app[web.1]: request:
  160. 2018-03-19T11:43:16.227733+00:00 app[web.1]: ClientRequest {
  161. 2018-03-19T11:43:16.227734+00:00 app[web.1]: domain: null,
  162. 2018-03-19T11:43:16.227736+00:00 app[web.1]: _events: [Object],
  163. 2018-03-19T11:43:16.227738+00:00 app[web.1]: _eventsCount: 6,
  164. 2018-03-19T11:43:16.227740+00:00 app[web.1]: _maxListeners: undefined,
  165. 2018-03-19T11:43:16.227741+00:00 app[web.1]: output: [],
  166. 2018-03-19T11:43:16.227743+00:00 app[web.1]: outputEncodings: [],
  167. 2018-03-19T11:43:16.227744+00:00 app[web.1]: outputCallbacks: [],
  168. 2018-03-19T11:43:16.227746+00:00 app[web.1]: outputSize: 0,
  169. 2018-03-19T11:43:16.227748+00:00 app[web.1]: writable: true,
  170. 2018-03-19T11:43:16.227749+00:00 app[web.1]: _last: true,
  171. 2018-03-19T11:43:16.227751+00:00 app[web.1]: upgrading: false,
  172. 2018-03-19T11:43:16.227752+00:00 app[web.1]: chunkedEncoding: false,
  173. 2018-03-19T11:43:16.227754+00:00 app[web.1]: shouldKeepAlive: false,
  174. 2018-03-19T11:43:16.227755+00:00 app[web.1]: useChunkedEncodingByDefault: true,
  175. 2018-03-19T11:43:16.227757+00:00 app[web.1]: sendDate: false,
  176. 2018-03-19T11:43:16.227759+00:00 app[web.1]: _removedConnection: false,
  177. 2018-03-19T11:43:16.227760+00:00 app[web.1]: _removedContLen: false,
  178. 2018-03-19T11:43:16.227762+00:00 app[web.1]: _removedTE: false,
  179. 2018-03-19T11:43:16.227764+00:00 app[web.1]: _contentLength: null,
  180. 2018-03-19T11:43:16.227765+00:00 app[web.1]: _hasBody: true,
  181. 2018-03-19T11:43:16.227767+00:00 app[web.1]: _trailer: '',
  182. 2018-03-19T11:43:16.227768+00:00 app[web.1]: finished: true,
  183. 2018-03-19T11:43:16.227770+00:00 app[web.1]: _headerSent: true,
  184. 2018-03-19T11:43:16.227771+00:00 app[web.1]: socket: [Object],
  185. 2018-03-19T11:43:16.227773+00:00 app[web.1]: connection: [Object],
  186. 2018-03-19T11:43:16.227776+00:00 app[web.1]: _header: 'POST /v2/bot/message/reply HTTP/1.1rnAccept: application/json, text/plain, */*rnContent-Type: application/jsonrnAuthorization: Bearer mGKjMIpknOpyCQ7VdpNWjOw1jjwCJJxM0qRuojibKdChbfusZlUl/2DcCbS3A6cX4QWQ58yGZrKcH8pxXjmPwHMTKavxKhXGjIT3vFh5Z1NL8kUGnznrEM7QPCSQrPq+17OQgzIDWnUW/mP5abzpSgdB04t89/1O/w1cDnyilFU=rnUser-Agent: @line/bot-sdk/5.2.0rnContent-Length: 442rnHost: api.line.mernConnection: closernrn',
  187. 2018-03-19T11:43:16.227778+00:00 app[web.1]: _onPendingData: [Function: noopPendingOutput],
  188. 2018-03-19T11:43:16.227779+00:00 app[web.1]: agent: [Object],
  189. 2018-03-19T11:43:16.227781+00:00 app[web.1]: socketPath: undefined,
  190. 2018-03-19T11:43:16.227782+00:00 app[web.1]: timeout: undefined,
  191. 2018-03-19T11:43:16.227784+00:00 app[web.1]: method: 'POST',
  192. 2018-03-19T11:43:16.227786+00:00 app[web.1]: path: '/v2/bot/message/reply',
  193. 2018-03-19T11:43:16.227787+00:00 app[web.1]: _ended: true,
  194. 2018-03-19T11:43:16.227789+00:00 app[web.1]: res: [Object],
  195. 2018-03-19T11:43:16.227790+00:00 app[web.1]: aborted: undefined,
  196. 2018-03-19T11:43:16.227792+00:00 app[web.1]: timeoutCb: null,
  197. 2018-03-19T11:43:16.227794+00:00 app[web.1]: upgradeOrConnect: false,
  198. 2018-03-19T11:43:16.227795+00:00 app[web.1]: parser: null,
  199. 2018-03-19T11:43:16.227798+00:00 app[web.1]: _redirectable: [Object],
  200. 2018-03-19T11:43:16.227797+00:00 app[web.1]: maxHeadersCount: null,
  201. 2018-03-19T11:43:16.227800+00:00 app[web.1]: [Symbol(outHeadersKey)]: [Object] },
  202. 2018-03-19T11:43:16.227802+00:00 app[web.1]: response:
  203. 2018-03-19T11:43:16.227803+00:00 app[web.1]: { status: 403,
  204. 2018-03-19T11:43:16.227805+00:00 app[web.1]: statusText: 'Forbidden',
  205. 2018-03-19T11:43:16.227806+00:00 app[web.1]: headers: [Object],
  206. 2018-03-19T11:43:16.227808+00:00 app[web.1]: config: [Object],
  207. 2018-03-19T11:43:16.227810+00:00 app[web.1]: request: [Object],
  208. 2018-03-19T11:43:16.227811+00:00 app[web.1]: data: [Object] } } }
Add Comment
Please, Sign In to add comment