Skip to content

Commit

Permalink
公式webhookを利用
Browse files Browse the repository at this point in the history
  • Loading branch information
gentksb committed Mar 9, 2024
1 parent 8b00f4b commit 71859af
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lambda/spilitwise-automation/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const handler: Handler = async (
throw new Error("環境変数が設定されていません");
}

splitRecent20Expenses({
const resuleMessage = splitRecent20Expenses({
SPLITWISE_API_KEY_PARAMETER_NAME,
SLACK_WEBHOOK_URL,
USER1_ID,
Expand All @@ -46,7 +46,7 @@ export const handler: Handler = async (
return {
statusCode: 200,
body: JSON.stringify({
message: "処理が完了しました",
message: resuleMessage,
}),
};
};
20 changes: 10 additions & 10 deletions lambda/spilitwise-automation/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
import { splitExpense } from "./src/logic/splitExpense";
import { isExpenseEligibleForSplitting } from "./src/validator/isExpenseEligibleForSplitting";
import { components, paths } from "../../@types/splitwise";
import { IncomingWebhook } from "@slack/webhook";

interface Props {
SPLITWISE_API_KEY_PARAMETER_NAME: string;
Expand All @@ -25,6 +26,7 @@ export const splitRecent20Expenses = async (props: Props) => {
USER2_RATE,
SPLITWISE_GROUP_ID,
} = props;
const webhook = new IncomingWebhook(SLACK_WEBHOOK_URL);

const axios_option: AxiosRequestConfig = {
headers: { Authorization: `Bearer ${SPLITWISE_API_KEY_PARAMETER_NAME}` },
Expand Down Expand Up @@ -112,10 +114,11 @@ export const splitRecent20Expenses = async (props: Props) => {
) => {
if (response.data.errors?.length !== 0) {
console.error(response.data.errors);
await axios.post(SLACK_WEBHOOK_URL, {
await webhook.send({
text: `割り勘処理でエラー発生\n ID:${response.data.expenses?.[0].id}\n${response.data.errors?.toString()}`,
});
throw new Error("Splitwise APIへのPOST内容に問題があります");
return;
// 失敗したものはエラーを出力して終了
} else {
console.log("update_expense成功", response.data);

Expand All @@ -127,8 +130,8 @@ export const splitRecent20Expenses = async (props: Props) => {
`●${response.data.expenses?.[0].users?.[1].user?.first_name}の負担: ${response.data.expenses?.[0].users?.[1].owed_share}円\`\`\``,
].join("\n");

await axios
.post(SLACK_WEBHOOK_URL, {
await webhook
.send({
blocks: [
{
type: "header",
Expand All @@ -147,12 +150,9 @@ export const splitRecent20Expenses = async (props: Props) => {
},
],
})
.then((res) =>
console.log(
`Slack post response: ${res.status}`,
res.data.toString()
)
);
.then((result) => {
console.log("Slackへの通知に成功しました", result.text);
});
}
}
);
Expand Down
47 changes: 41 additions & 6 deletions 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"typescript": "~5.3.3"
},
"dependencies": {
"@slack/webhook": "^7.0.2",
"aws-cdk-lib": "2.126.0",
"aws-lambda": "^1.0.7",
"axios": "^1.6.7",
Expand Down

0 comments on commit 71859af

Please sign in to comment.