Skip to content

Commit

Permalink
[Feat] Add line bot webhook. (#89)
Browse files Browse the repository at this point in the history
Add a web hook for the line bot, and pass testing data for now.


### Related Issue
- #87
  • Loading branch information
moojing authored Mar 4, 2023
1 parent feda024 commit 16f0aba
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 9 deletions.
6 changes: 5 additions & 1 deletion lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { process591QueryUrl } from "./591House/utils";

const apiSecret = process.env.ZUZUGO_API_SECRET;

const tokenLine = process.env.LINE_API_TOKEN;

// const isSubwayStationFilterEnabled = process.env.ENABLE_SUBWAY_STATION_FILTER === "true";
Expand Down Expand Up @@ -34,4 +33,9 @@ export const config = {
tokenLine,
production: process.env.NODE_ENV === "production",
cronEnabled: process.env.DISABLE_CRON !== "true",

line: {
LINE_ACCESS_TOKEN: process.env.LINE_ACCESS_TOKEN,
LINE_CHANNEL_SECRET: process.env.LINE_CHANNEL_SECRET
}
};
19 changes: 19 additions & 0 deletions lib/notification/lineBot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Client, TextMessage } from '@line/bot-sdk';

import { config } from 'lib/config'
// set Channel Access Token and Channel Secret for Line Bot
const client = new Client({
channelAccessToken: config.line.LINE_ACCESS_TOKEN || '',
channelSecret: config.line.LINE_CHANNEL_SECRET,
});

// @todo: pass rent data from api.
const message: TextMessage = {
type: 'text',
text: 'Zuzugo got a new house!',
};

// @todo: get groupId from database.
export default async function sendLineBotMessage(groupId: string) {
await client.pushMessage(groupId, message)
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"dependencies": {
"@next-auth/prisma-adapter": "^1.0.5",
"@line/bot-sdk": "^7.5.2",
"@next/font": "13.1.6",
"@prisma/client": "^4.10.1",
"@seratch_/bolt-http-runner": "^1.0.3",
Expand Down Expand Up @@ -66,4 +67,4 @@
"engines": {
"node": ">=18.12"
}
}
}
35 changes: 35 additions & 0 deletions pages/api/line/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { WebhookEvent, User, Group, Room } from "@line/bot-sdk";
import type { NextApiRequest, NextApiResponse } from "next";

import sendLineBotMessage from 'lib/notification/lineBot'


export default async function handler(req: NextApiRequest, res: NextApiResponse) {

if (req.method !== "POST") {
res.status(405).json({ error: "Sorry! This endpoint does not accept your requests." });
return;
}

const events: WebhookEvent[] = req.body.events;
const [firstEvent] = events;

if (firstEvent.type === 'join') {
const {
userId = '',
groupId = '',
roomId = '',
// @todo: save chatType to database.
// type: chatType,
} = firstEvent.source as User & Group & Room;

const chatId = userId || groupId || roomId;
try {
await sendLineBotMessage(chatId)
} catch (e) {
console.error(e)
}
}

res.send(200)
}
72 changes: 65 additions & 7 deletions pnpm-lock.yaml

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

1 comment on commit 16f0aba

@vercel
Copy link

@vercel vercel bot commented on 16f0aba Mar 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

zuzugo – ./

zuzugo.vercel.app
zuzugo-git-main-yukaihuangtw.vercel.app
zuzugo-yukaihuangtw.vercel.app

Please sign in to comment.