Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: support express error handle #42

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ringcentral-chatbot-core",
"version": "1.6.0",
"version": "1.6.1",
"license": "MIT",
"scripts": {
"test": "jest -c tests/jest.config.ts",
Expand Down
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
Loading