前提・実現したいこと
AWS lambdaを使用してAmazonLex(Bot)を呼び出し、レスポンスをLINEへ返信
発生している問題・エラーメッセージ
AWS lambdaからAmazonLexを呼び出し時に 401エラーが発生する。
2021-05-20T10:20:00.399Z a6937f1c-2c3f-4cfd-9fb8-8a41ed893c23 ERROR Invoke Error { "errorType": "Error", "errorMessage": "Request failed with status code 401", "statusCode": 401, "statusMessage": "Unauthorized", "originalError": { "message": "Request failed with status code 401", "name": "Error", "stack": "Error: Request failed with status code 401\n at createError (/var/task/node_modules/axios/lib/core/createError.js:16:15)\n at settle (/var/task/node_modules/axios/lib/core/settle.js:17:12)\n at IncomingMessage.handleStreamEnd (/var/task/node_modules/axios/lib/adapters/http.js:236:11)\n at IncomingMessage.emit (events.js:326:22)\n at IncomingMessage.EventEmitter.emit (domain.js:483:12)\n at endReadableNT (_stream_readable.js:1241:12)\n at processTicksAndRejections (internal/process/task_queues.js:84:21)", "config": { "url": "https://api.line.me/v2/bot/message/reply", "method": "post", "data": "{\"messages\":[null],\"replyToken\":\"16264188ddd94dbd8b6af8db475f50d8\",\"notificationDisabled\":false}", "headers": { "Accept": "application/json, text/plain, */*", "Content-Type": "application/json", "Authorization": "Bearer チャネルアクセストークン", "User-Agent": "@line/bot-sdk/6.8.4", "Content-Length": 96 }, "transformRequest": [ null ], "transformResponse": [ null ], "timeout": 0, "xsrfCookieName": "XSRF-TOKEN", "xsrfHeaderName": "X-XSRF-TOKEN", "maxContentLength": -1 } }, "stack": [ "Error: Request failed with status code 401", " at HTTPClient.wrapError (/var/task/node_modules/@line/bot-sdk/dist/http.js:83:20)", " at /var/task/node_modules/@line/bot-sdk/dist/http.js:19:88", " at processTicksAndRejections (internal/process/task_queues.js:97:5)", " at async HTTPClient.post (/var/task/node_modules/@line/bot-sdk/dist/http.js:33:21)", " at async Runtime.exports.handler (/var/task/index.js:80:13)" ] }
該当のソースコード
node.js
1const Line = require('@line/bot-sdk'); 2const Aws = require('aws-sdk'); 3const channelAccessToken = process.env.CHANNEL_ACCESS_TOKEN; 4const channelSecret = process.env.CHANNEL_SECRET; 5 6const client = new Line.Client({channelAccessToken: channelAccessToken}); 7const s3 = new Aws.S3(); 8 9const lex = new Aws.LexRuntime({region: 'ap-northeast-1'}); 10 11const getContent = (messageId) => { 12 return new Promise((resolve, reject) => { 13 client.getMessageContent(messageId).then((stream) => { 14 let content = []; 15 stream.on('data', (chunk) => { 16 console.log('data'); 17 console.log(chunk); 18 content.push(new Buffer(chunk)); 19 }).on('error', (err) => { 20 reject(err); 21 }).on('end', function(){ 22 console.log('end'); 23 console.log(content); 24 resolve(Buffer.concat(content)); 25 }); 26 }); 27 }); 28}; 29 30 31exports.handler = async(event,text,callback) => { 32 console.log(JSON.stringify(event)); 33 34 //Validate signature 35 if (!Line.validateSignature(event.body, channelSecret, event.headers['x-line-signature'])) return { statusCode: 400 }; 36 37 const messages = JSON.parse(event.body); 38 39 //Validate webhook from LINE Developers 40 if (messages.events.length === 0) { 41 return { statusCode: 200, body: 'OK'}; 42 } 43 44 for (let i = 0; i<messages.events.length; i++) { 45 if (messages.events[i].type === 'message') { 46 let text; 47 if (messages.events[i].message.type === 'text') { 48 const params = { 49 botAlias: 'PRD', 50 botName: 'TESTBot', 51 inputText:messages.events[i].message.text, 52 userId:messages.events[i].source.userId, 53 //sessionAttributes:{}, 54 }; 55 console.log(params); 56 57 lex.postText(params,function(err,data) { ←LEX呼び出しでエラー 58 console.log("????"); 59・・・以降省略 60
補足情報(FW/ツールのバージョンなど)
Node.js 12.x
あなたの回答
tips
プレビュー