Skip to content

Commit 3aecbaa

Browse files
committed
Revert "Structure refactor"
This reverts commit 69afb3d.
1 parent 4f60d15 commit 3aecbaa

File tree

3 files changed

+30
-45
lines changed

3 files changed

+30
-45
lines changed

README.md

+9-19
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This is a fork of [https://github.com/ringcentral/ringcentral-chatbot-js](https:
88
import { extendApp } from 'ringcentral-chatbot-core'
99
import express from 'express'
1010

11-
function botCommandHandler ({
11+
function eventHandler ({
1212
type, // could be 'BotRemoved', 'Message4Bot', 'BotGroupLeft', 'BotJoinGroup', 'Maintain', 'SetupDatabase'
1313
bot, // the bot instance, check src/models/Bot.ts for instance methods
1414
text, // the text message user posted in chatgroup
@@ -65,27 +65,17 @@ function botCommandHandler ({
6565
}
6666
}
6767

68-
const config = {
69-
admin : {
70-
route: '/admin',
71-
handler: null
72-
},
73-
bot:{
74-
route: '/bot',
75-
handler: botCommandHandler
76-
},
77-
card: {
78-
route: '/interactive-messages',
79-
handler: null
80-
},
81-
models: { // optional
82-
Bot: 'your bot data model defination' // check src/models/Bot.ts as a example, optional
83-
}
68+
const botConfig = {
69+
adminRoute: '/admin', // optional
70+
botRoute: '/bot', // optional
71+
models: { // optional
72+
Bot: 'your bot data model defination' // check src/models/Bot.ts as a example, optional
73+
}
8474
}
8575

86-
let app = express();
76+
let app = express()
8777
const skills = []
88-
app = extendApp(app, skills, config)
78+
app = extendApp(app, skills, eventHandler, botConfig)
8979
app.listen(3000, () => {
9080
console.log('server running')
9181
})

src/index.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,30 @@ import adminApp from './apps/admin';
44
import { BotConfig } from './types';
55
import defaultModels from './models';
66
import express from 'express';
7+
78
export const extendApp = (
89
app: any,
910
skills: { handle: Function; app?: any }[] = [],
10-
config: BotConfig
11+
handle?: Function,
12+
config: BotConfig = {
13+
adminRoute: '/admin',
14+
botRoute: '/bot',
15+
models: {}
16+
}
1117
) => {
1218
const conf = {
1319
...config,
1420
models: Object.assign({}, defaultModels, config.models || {})
1521
}
16-
1722
conf.setupDatabase = async (force = false) => {
1823
for (const modelName in conf.models) {
1924
await conf.models[modelName].sync({ force });
2025
}
2126
};
2227
const mergedHandle = async (event: any) => {
2328
let handled = false;
24-
if (config.bot.handler) {
25-
handled = await config.bot.handler(event, handled);
29+
if (handle) {
30+
handled = await handle(event, handled);
2631
}
2732
for (const skill of skills) {
2833
if (skill.handle) {
@@ -31,16 +36,16 @@ export const extendApp = (
3136
}
3237
}
3338
};
34-
39+
3540
app.use(express.json());
36-
app.use(express.urlencoded({ extended: true }));
37-
41+
app.use(express.urlencoded({extended: true}));
42+
3843
app.use(
39-
config.admin.route,
44+
config.adminRoute,
4045
adminApp(mergedHandle, conf)
4146
);
4247
app.use(
43-
config.bot.route,
48+
config.botRoute,
4449
botApp(mergedHandle, conf)
4550
);
4651
for (const skill of skills) {
@@ -49,10 +54,6 @@ export const extendApp = (
4954
}
5055
}
5156
(app as any).mergedHandle = mergedHandle; // for unit testing
52-
console.log('server running...');
53-
console.log(`- bot oauth uri:\n${process.env.RINGCENTRAL_CHATBOT_SERVER}${config.bot.route}/oauth`);
54-
console.log(`- interactive message uri:\n${process.env.RINGCENTRAL_CHATBOT_SERVER}${config.card.route}`);
55-
console.log(`- admin uri:\n${process.env.RINGCENTRAL_CHATBOT_SERVER}${config.admin.route}`);
5657
return app;
5758
};
5859

src/types.ts

+7-13
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ export type Message = {
2727
creatorId: string;
2828
groupId: string;
2929
mentions:
30-
| null
31-
| {
32-
id: string;
33-
type: string;
34-
}[];
30+
| null
31+
| {
32+
id: string;
33+
type: string;
34+
}[];
3535
attachments?: AttachmentType[];
3636
};
3737
ownerId: string;
@@ -43,15 +43,9 @@ export type AttachmentType = {
4343
name: string;
4444
};
4545

46-
export interface EndPoint {
47-
route: string;
48-
handler?: Function;
49-
}
50-
5146
export interface BotConfig {
52-
admin: EndPoint,
53-
bot: EndPoint,
54-
card: EndPoint,
47+
adminRoute: string,
48+
botRoute: string,
5549
models?: any,
5650
setupDatabase?: Function
5751
}

0 commit comments

Comments
 (0)