File tree Expand file tree Collapse file tree 3 files changed +28
-22
lines changed Expand file tree Collapse file tree 3 files changed +28
-22
lines changed Original file line number Diff line number Diff line change @@ -19,19 +19,22 @@ export class FacebookApi {
19
19
20
20
constructor ( private settings : IBotSettings ) { }
21
21
22
- public sendMessage ( msg : IFbResponse ) {
23
- request . post ( {
24
- url : `${ BASE_API } /me/messages?access_token=${ this . settings . fb . access_token } ` ,
25
- body : msg , json : true
26
- } , ( err , res , body ) => {
27
- if ( err ) {
28
- console . log ( 'facebook: could not send msg to fb' , JSON . stringify ( err , null , 2 ) ) ;
29
- throw err ;
30
- }
31
- if ( res . statusCode >= 400 ) {
32
- throw new Error ( `facebook: Error sending to fb ${ JSON . stringify ( res ) } ${ JSON . stringify ( msg ) } ` ) ;
33
- }
34
- console . log ( 'facebook: reply sent' ) ;
22
+ public sendMessage ( msg : IFbResponse ) : Promise < any > {
23
+ return new Promise ( ( resolve : Function , reject : Function ) => {
24
+ request . post ( {
25
+ url : `${ BASE_API } /me/messages?access_token=${ this . settings . fb . access_token } ` ,
26
+ body : msg , json : true
27
+ } , ( err , res , body ) => {
28
+ if ( err ) {
29
+ console . log ( 'facebook: could not send msg to fb' , JSON . stringify ( err , null , 2 ) ) ;
30
+ return reject ( err ) ;
31
+ }
32
+ if ( res . statusCode >= 400 ) {
33
+ return reject ( new Error ( `facebook: Error sending to fb ${ JSON . stringify ( res ) } ${ JSON . stringify ( msg ) } ` ) ) ;
34
+ }
35
+ if ( this . settings . debug ) console . log ( `facebook: reply sent ${ JSON . stringify ( res ) } ` ) ;
36
+ return resolve ( res ) ;
37
+ } ) ;
35
38
} ) ;
36
39
}
37
40
Original file line number Diff line number Diff line change @@ -5,16 +5,18 @@ import * as Promise from 'bluebird';
5
5
6
6
export class FacebookReply implements IBotReply {
7
7
constructor ( private recipientId : number , private fbApi : FacebookApi ) { }
8
- text ( text : string ) {
8
+
9
+ text ( text : string ) : Promise < any > {
9
10
let response = {
10
11
recipient : {
11
12
id : this . recipientId
12
13
} ,
13
14
message : { text}
14
15
} ;
15
- this . fbApi . sendMessage ( response ) ;
16
+ return this . fbApi . sendMessage ( response ) ;
16
17
}
17
- list ( elements : Array < IBotReplyListItem > ) {
18
+
19
+ list ( elements : Array < IBotReplyListItem > ) : Promise < any > {
18
20
let response : IFbResponse = {
19
21
recipient : {
20
22
id : this . recipientId
@@ -29,10 +31,10 @@ export class FacebookReply implements IBotReply {
29
31
}
30
32
}
31
33
} ;
32
- this . fbApi . sendMessage ( response ) ;
34
+ return this . fbApi . sendMessage ( response ) ;
33
35
}
34
36
35
- buttons ( text : string , buttons : IBotReplyListItemAction [ ] ) : void {
37
+ buttons ( text : string , buttons : IBotReplyListItemAction [ ] ) : Promise < any > {
36
38
let response : IFbResponse = {
37
39
recipient : {
38
40
id : this . recipientId
@@ -48,7 +50,7 @@ export class FacebookReply implements IBotReply {
48
50
}
49
51
}
50
52
} ;
51
- this . fbApi . sendMessage ( response ) ;
53
+ return this . fbApi . sendMessage ( response ) ;
52
54
}
53
55
}
54
56
Original file line number Diff line number Diff line change
1
+ import * as Promise from 'bluebird' ;
1
2
2
3
export interface IBotUser {
3
4
id : string ;
@@ -70,9 +71,9 @@ export interface IBotRequest {
70
71
}
71
72
72
73
export interface IBotReply {
73
- text ( text : string ) : void ;
74
- buttons ( text : string , buttons : IBotReplyListItemAction [ ] ) : void ;
75
- list ( list : Array < IBotReplyListItem > ) : void ;
74
+ text ( text : string ) : Promise < any > ;
75
+ buttons ( text : string , buttons : IBotReplyListItemAction [ ] ) : Promise < any > ;
76
+ list ( list : Array < IBotReplyListItem > ) : Promise < any > ;
76
77
}
77
78
78
79
export interface IDeliveryMessage {
You can’t perform that action at this time.
0 commit comments