8000 feat(facebook bot): implement postback handler · amitevski/botframework@ca7585d · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit ca7585d

Browse files
committed
feat(facebook bot): implement postback handler
1 parent 5f5de93 commit ca7585d

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

src/facebook/bot.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ export class FacebookBot {
9898
type: BOT_REQUEST_TYPE.FACEBOOK
9999
}, reply) : null;
100100
}
101+
if (messaging.postback) {
102+
return (this.botController.postback) ? this.botController.postback({
103+
user, payload: messaging.postback.payload,
104+
type: BOT_REQUEST_TYPE.FACEBOOK
105+
}, reply) : null;
106+
}
101107
if (messaging.message) {
102108
if (messaging.message.text) {
103109
let textMessage: IBotRequest = {

src/facebook/interfaces.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,18 @@ export interface IFbMessageOptin {
4141
ref: string; // pass through param
4242
}
4343

44+
export interface IFbPostbackPayload {
45+
payload: string;
46+
}
47+
4448
export interface IFbMessaging {
4549
sender: IFbMessageUser;
4650
recipient: IFbMessageUser;
4751
timestamp: number; //timestamp
4852
message?: IFbMessage;
4953
optin?: IFbMessageOptin;
5054
delivery?: Object; // delivery
51-
postback?: Object; // postback with custom val
55+
postback?: IFbPostbackPayload; // postback with custom val
5256
}
5357

5458
export interface IFbMessageEntry {

src/interfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export interface IBotRequest {
6262
location?: ILocation;
6363
link?: ILink;
6464
ref?: string;
65+
payload?: string;
6566
image?: IImage;
6667
text?: string;
6768
raw?: any;
@@ -87,6 +88,7 @@ export interface IUnknownMessage {
8788

8889
export interface IBotController {
8990
newUser?(request: IBotRequest, reply: IBotReply): void;
91+
postback?(request: IBotRequest, reply: IBotReply): void;
9092
textMessage?(request: IBotRequest, reply: IBotReply): void;
9193
imageMessage?(request: IBotRequest, reply: IBotReply): void;
9294
linkMessage?(request: IBotRequest, reply: IBotReply): void;

tests/bot.spec.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {IBotSettings, IBotRequest, IBotReply, IBotUser, IBotController} from '..
77
class DummyController implements IBotController {
88
cc = {
99
newUser: 0,
10+
postback: 0,
1011
textMessage: 0,
1112
imageMessage: 0,
1213
linkMessage: 0,
@@ -15,6 +16,7 @@ class DummyController implements IBotController {
1516
catchAll: 0,
1617
}
1718
newUser(msg: IBotRequest, reply: IBotReply) {console.log('got new user'); this.cc.newUser++;}
19+
postback(msg: IBotRequest, reply: IBotReply) {console.log('got new postback'); this.cc.postback++;}
1820
textMessage(msg: IBotRequest, reply: IBotReply) {console.log('got new textMessage'); this.cc.textMessage++;}
1921
imageMessage(image: IBotRequest, reply: IBotReply) {console.log('got new imageMessage'); this.cc.imageMessage++;}
2022
linkMessage(msg: IBotRequest, reply: IBotReply) {console.log('got linkMessage'); this.cc.linkMessage++;}
@@ -54,16 +56,16 @@ describe('FacebookBot', () => {
5456
it('should dispatch each message', (done) => {
5557
bot.receiveMessage(fakeMessageMixed())
5658
.then( dispatchResults => {
57-
expect(dispatchResults.length).to.eql(4);
59+
expect(dispatchResults.length).to.eql(5);
5860
expect(ctrl.cc.delivered).to.eql(1);
61+
expect(ctrl.cc.postback).to.eql(1);
5962
expect(ctrl.cc.textMessage).to.eql(1);
6063
expect(ctrl.cc.imageMessage).to.eql(1);
6164
expect(ctrl.cc.locationMessage).to.eql(1);
6265
done();
6366
})
6467
// just console.log testing for now :(
6568
// if anyone knows a good mocking library for typescript youre welcome :)
66-
6769
});
6870

6971
});
@@ -177,6 +179,24 @@ function fakeMessageMixed() {
177179
}
178180
}
179181
]
182+
},
183+
{
184+
"id":234,
185+
"time":1458692752478,
186+
"messaging":[
187+
{
188+
"sender":{
189+
"id":123
190+
},
191+
"recipient":{
192+
"id":234
193+
},
194+
"timestamp":1458692752478,
195+
"postback":{
196+
"payload":"USER_DEFINED_PAYLOAD"
197+
}
198+
}
199+
]
180200
}
181201
]
182202
};

0 commit comments

Comments
 (0)
0