Skip to content

Commit

Permalink
misc: support express error handle
Browse files Browse the repository at this point in the history
  • Loading branch information
embbnux committed Jun 26, 2024
1 parent 83a8f3b commit 0719c77
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 51 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ jobs:
uses: actions/checkout@v2
with:
persist-credentials: false
- name: Use Node.js 14.x
- name: Use Node.js 16.x
uses: actions/setup-node@v1
with:
node-version: 14.x
node-version: 16.x
- run: yarn
- run: yarn build
106 changes: 57 additions & 49 deletions src/apps/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,73 @@ const createApp = (handle: Function, conf: BotConfig) => {
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
const {Bot} = conf.models || {}
app.all('/oauth', async (req, res) => {
const bot = (await (Bot as any).init({
code: req.query.code,
token: req.body,
creator_extension_id: req.query.creator_extension_id,
creator_account_id: req.query.creator_account_id,
})) as BotType;
await bot.setupWebHook(); // this might take a while, depends on when the bot user is ready
await handle({type: 'BotAdded', bot});
res.status(200);
res.send('ok'); // return string to fix issue for serverless-offline
app.all('/oauth', async (req, res, next) => {
try {
const bot = (await (Bot as any).init({
code: req.query.code,
token: req.body,
creator_extension_id: req.query.creator_extension_id,
creator_account_id: req.query.creator_account_id,
})) as BotType;
await bot.setupWebHook(); // this might take a while, depends on when the bot user is ready
await handle({type: 'BotAdded', bot});
res.status(200);
res.send('ok'); // return string to fix issue for serverless-offline
} catch (e) {
next(e);
}
});

app.post('/webhook', async (req, res) => {
app.post('/webhook', async (req, res, next) => {
const message = req.body;
if (process.env.LOG_LEVEL === 'DEBUG') {
console.log('WebHook payload:', JSON.stringify(message, null, 2));
}
const body = message.body;
if (body) {
switch (body.eventType) {
case 'Delete': {
const deleteBot = await botDeleted(Bot, message);
await handle({type: 'BotRemoved', bot: deleteBot});
break;
}
case 'PostAdded': {
const result = await postAdded(Bot, message, conf);
if (result) {
await handle({type: 'Message4Bot', ...result});
try {
const body = message.body;
if (body) {
switch (body.eventType) {
case 'Delete': {
const deleteBot = await botDeleted(Bot, message);
await handle({type: 'BotRemoved', bot: deleteBot});
break;
}
break;
}
case 'GroupLeft':
const info = await groupLeft(message);
await handle({
type: 'BotGroupLeft',
...info
});
break;
case 'GroupJoined': {
const botId = message.ownerId;
const joinGroupBot = await Bot.findByPk(botId);
const groupId = message.body.id;
const groupType = message.body.type;
await handle({
type: 'BotJoinGroup',
bot: joinGroupBot,
group: {id: groupId, type: groupType },
});
break;
case 'PostAdded': {
const result = await postAdded(Bot, message, conf);
if (result) {
await handle({type: 'Message4Bot', ...result});
}
break;
}
case 'GroupLeft':
const info = await groupLeft(message);
await handle({
type: 'BotGroupLeft',
...info
});
break;
case 'GroupJoined': {
const botId = message.ownerId;
const joinGroupBot = await Bot.findByPk(botId);
const groupId = message.body.id;
const groupType = message.body.type;
await handle({
type: 'BotJoinGroup',
bot: joinGroupBot,
group: {id: groupId, type: groupType },
});
break;
}
default:
break;
}
default:
break;
await handle({type: body.eventType, message});
}
await handle({type: body.eventType, message});
res.header('Validation-Token', req.header('Validation-Token'));
res.json({ result: 'ok' });
} catch (e) {
next(e);
}
res.header('Validation-Token', req.header('Validation-Token'));
res.json({ result: 'ok' });
});

return app;
Expand Down

0 comments on commit 0719c77

Please sign in to comment.