Skip to content
This repository has been archived by the owner on Jul 14, 2020. It is now read-only.

Commit

Permalink
Add an error handler in backend
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Jul 23, 2019
1 parent 8abda47 commit 2cb8701
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
38 changes: 25 additions & 13 deletions serverless-aws/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

import { App, LogLevel, ExpressReceiver } from '@slack/bolt';
import { IncomingWebhook } from '@slack/webhook';
import * as moment from 'moment-timezone';
import AWS = require('aws-sdk');

Expand Down Expand Up @@ -41,8 +42,9 @@ app.command(`/${slashCommandName}`, async ({ command, ack, context }) => {

const m = moment().tz('Asia/Tokyo');
const asyncOpArgs: AsyncOperationArgs = {
url: buildYahooImageUrl({ lat, lon, width, height, m }),
token: context.botToken,
yahooImageUrl: buildYahooImageUrl({ lat, lon, width, height, m }),
responseUrl: command.response_url,
channelId: command.channel_id,
prefName: prefectureName,
prefKanjiName: prefecture.kanjiName,
Expand All @@ -52,7 +54,6 @@ app.command(`/${slashCommandName}`, async ({ command, ack, context }) => {
if (process.env.IS_OFFLINE === 'true') { // serverless-offline
ack();
fetchImageAndUpload(asyncOpArgs);

} else {
// on AWS
const lambda = new AWS.Lambda();
Expand Down Expand Up @@ -96,14 +97,22 @@ export const backendOperation = async function (event, _context) {
};

type AsyncOperationArgs = UploadImageArgs & {
url: string;
yahooImageUrl: string;
responseUrl: string;
};
function fetchImageAndUpload(args: AsyncOperationArgs): Promise<void> {
const req: Promise<Buffer> = request.get({ url: args.url, encoding: null });
const req: Promise<Buffer> = request.get({ url: args.yahooImageUrl, encoding: null });
return req.then(image => {
args.file = image;
return uploadImage(args);
}).catch(printCompleteJSON);
}).catch(err => {
const jsonData = JSON.stringify(err);
console.log(jsonData);
return new IncomingWebhook(args.responseUrl)
.send(`Failed to post an image file - ${jsonData}`)
.then(printCompleteJSON)
.catch(printCompleteJSON);
});
}

type UploadImageArgs = {
Expand All @@ -114,14 +123,17 @@ type UploadImageArgs = {
file: Buffer;
}
function uploadImage(args: UploadImageArgs): Promise<void> {
return app.client.files.upload({
token: args.token,
title: `${args.prefKanjiName}付近の現在の雨雲レーダーを表示しています`,
file: args.file,
filename: `amesh_${args.prefName}.png`,
filetype: `image/png`,
channels: args.channelId
}).then(console.log);
return app.client.files
.upload({
token: args.token,
title: `${args.prefKanjiName}付近の現在の雨雲レーダーを表示しています`,
file: args.file,
filename: `amesh_${args.prefName}.png`,
filetype: `image/png`,
channels: args.channelId
})
.then(printCompleteJSON)
.catch(printCompleteJSON);
}

// --------------------------------------
Expand Down
13 changes: 11 additions & 2 deletions serverless-aws/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions serverless-aws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"dependencies": {
"@slack/bolt": "^1.2.0",
"@slack/webhook": "^5.0.0",
"aws-sdk": "^2.497.0",
"aws-serverless-express": "^3.3.6",
"moment-timezone": "^0.5.26",
Expand Down

0 comments on commit 2cb8701

Please sign in to comment.