8000 feat(IBotReply): reply interface now returns a promise, that is resol… · amitevski/botframework@414ae02 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 414ae02

Browse files
committed
feat(IBotReply): reply interface now returns a promise, that is resolved when facebook returns a res
reply methods now return a Promise instead of void
1 parent e853d7b commit 414ae02

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

src/facebook/api.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,22 @@ export class FacebookApi {
1919

2020
constructor(private settings: IBotSettings) {}
2121

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+
});
3538
});
3639
}
3740

src/facebook/bot.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ import * as Promise from 'bluebird';
55

66
export class FacebookReply implements IBotReply {
77
constructor(private recipientId: number, private fbApi: FacebookApi) {}
8-
text(text: string) {
8+
9+
text(text: string): Promise<any> {
910
let response = {
1011
recipient: {
1112
id: this.recipientId
1213
},
1314
message: {text}
1415
};
15-
this.fbApi.sendMessage(response);
16+
return this.fbApi.sendMessage(response);
1617
}
17-
list(elements: Array<IBotReplyListItem>) {
18+
19+
list(elements: Array<IBotReplyListItem>): Promise<any> {
1820
let response: IFbResponse = {
1921
recipient: {
2022
id: this.recipientId
@@ -29,10 +31,10 @@ export class FacebookReply implements IBotReply {
2931
}
3032
}
3133
};
32-
this.fbApi.sendMessage(response);
34+
return this.fbApi.sendMessage(response);
3335
}
3436

35-
buttons(text: string, buttons: IBotReplyListItemAction[]): void {
37+
buttons(text: string, buttons: IBotReplyListItemAction[]): Promise<any> {
3638
let response: IFbResponse = {
3739
recipient: {
3840
id: this.recipientId
@@ -48,7 +50,7 @@ export class FacebookReply implements IBotReply {
4850
}
4951
}
5052
};
51-
this.fbApi.sendMessage(response);
53+
return this.fbApi.sendMessage(response);
5254
}
5355
}
5456

src/interfaces.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as Promise from 'bluebird';
12

23
export interface IBotUser {
34
id: string;
@@ -70,9 +71,9 @@ export interface IBotRequest {
7071
}
7172

7273
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>;
7677
}
7778

7879
export interface IDeliveryMessage {

0 commit comments

Comments
 (0)
0