@@ -8,65 +8,73 @@ const createApp = (handle: Function, conf: BotConfig) => {
8
8
app . use ( express . json ( ) )
9
9
app . use ( express . urlencoded ( { extended : true } ) )
10
10
const { Bot} = conf . models || { }
11
- app . all ( '/oauth' , async ( req , res ) => {
12
- const bot = ( await ( Bot as any ) . init ( {
13
- code : req . query . code ,
14
- token : req . body ,
15
- creator_extension_id : req . query . creator_extension_id ,
16
- creator_account_id : req . query . creator_account_id ,
17
- } ) ) as BotType ;
18
- await bot . setupWebHook ( ) ; // this might take a while, depends on when the bot user is ready
19
- await handle ( { type : 'BotAdded' , bot} ) ;
20
- res . status ( 200 ) ;
21
- res . send ( 'ok' ) ; // return string to fix issue for serverless-offline
11
+ app . all ( '/oauth' , async ( req , res , next ) => {
12
+ try {
13
+ const bot = ( await ( Bot as any ) . init ( {
14
+ code : req . query . code ,
15
+ token : req . body ,
16
+ creator_extension_id : req . query . creator_extension_id ,
17
+ creator_account_id : req . query . creator_account_id ,
18
+ } ) ) as BotType ;
19
+ await bot . setupWebHook ( ) ; // this might take a while, depends on when the bot user is ready
20
+ await handle ( { type : 'BotAdded' , bot} ) ;
21
+ res . status ( 200 ) ;
22
+ res . send ( 'ok' ) ; // return string to fix issue for serverless-offline
23
+ } catch ( e ) {
24
+ next ( e ) ;
25
+ }
22
26
} ) ;
23
27
24
- app . post ( '/webhook' , async ( req , res ) => {
28
+ app . post ( '/webhook' , async ( req , res , next ) => {
25
29
const message = req . body ;
26
30
if ( process . env . LOG_LEVEL === 'DEBUG' ) {
27
31
console . log ( 'WebHook payload:' , JSON . stringify ( message , null , 2 ) ) ;
28
32
}
29
- const body = message . body ;
30
- if ( body ) {
31
- switch ( body . eventType ) {
32
- case 'Delete' : {
33
- const deleteBot = await botDeleted ( Bot , message ) ;
34
- await handle ( { type : 'BotRemoved' , bot : deleteBot } ) ;
35
- break ;
36
- }
37
- case 'PostAdded' : {
38
- const result = await postAdded ( Bot , message , conf ) ;
39
- if ( result ) {
40
- await handle ( { type : 'Message4Bot' , ...result } ) ;
33
+ try {
34
+ const body = message . body ;
35
+ if ( body ) {
36
+ switch ( body . eventType ) {
37
+ case 'Delete' : {
38
+ const deleteBot = await botDeleted ( Bot , message ) ;
39
+ await handle ( { type : 'BotRemoved' , bot : deleteBot } ) ;
40
+ break ;
41
41
}
42
- break ;
43
- }
44
- case 'GroupLeft' :
45
- const info = await groupLeft ( message ) ;
46
- await handle ( {
47
- type : 'BotGroupLeft' ,
48
- ...info
49
- } ) ;
50
- break ;
51
- case 'GroupJoined' : {
52
- const botId = message . ownerId ;
53
- const joinGroupBot = await Bot . findByPk ( botId ) ;
54
- const groupId = message . body . id ;
55
- const groupType = message . body . type ;
56
- await handle ( {
57
- type : 'BotJoinGroup' ,
58
- bot : joinGroupBot ,
59
- group : { id : groupId , type : groupType } ,
60
- } ) ;
61
- break ;
42
+ case 'PostAdded' : {
43
+ const result = await postAdded ( Bot , message , conf ) ;
44
+ if ( result ) {
45
+ await handle ( { type : 'Message4Bot' , ...result } ) ;
46
+ }
47
+ break ;
48
+ }
49
+ case 'GroupLeft' :
50
+ const info = await groupLeft ( message ) ;
51
+ await handle ( {
52
+ type : 'BotGroupLeft' ,
53
+ ...info
54
+ } ) ;
55
+ break ;
56
+ case 'GroupJoined' : {
57
+ const botId = message . ownerId ;
58
+ const joinGroupBot = await Bot . findByPk ( botId ) ;
59
+ const groupId = message . body . id ;
60
+ const groupType = message . body . type ;
61
+ await handle ( {
62
+ type : 'BotJoinGroup' ,
63
+ bot : joinGroupBot ,
64
+ group : { id : groupId , type : groupType } ,
65
+ } ) ;
66
+ break ;
67
+ }
68
+ default :
69
+ break ;
62
70
}
63
- default :
64
- break ;
71
+ await handle ( { type : body . eventType , message} ) ;
65
72
}
66
- await handle ( { type : body . eventType , message} ) ;
73
+ res . header ( 'Validation-Token' , req . header ( 'Validation-Token' ) ) ;
74
+ res . json ( { result : 'ok' } ) ;
75
+ } catch ( e ) {
76
+ next ( e ) ;
67
77
}
68
- res . header ( 'Validation-Token' , req . header ( 'Validation-Token' ) ) ;
69
- res . json ( { result : 'ok' } ) ;
70
78
} ) ;
71
79
72
80
return app ;
0 commit comments