Skip to content

Commit

Permalink
feat(facebook bot): implement postback handler
Browse files Browse the repository at this point in the history
  • Loading branch information
amitevski committed Jun 5, 2016
1 parent 5f5de93 commit ca7585d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/facebook/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ export class FacebookBot {
type: BOT_REQUEST_TYPE.FACEBOOK
}, reply) : null;
}
if (messaging.postback) {
return (this.botController.postback) ? this.botController.postback({
user, payload: messaging.postback.payload,
type: BOT_REQUEST_TYPE.FACEBOOK
}, reply) : null;
}
if (messaging.message) {
if (messaging.message.text) {
let textMessage: IBotRequest = {
Expand Down
6 changes: 5 additions & 1 deletion src/facebook/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ export interface IFbMessageOptin {
ref: string; // pass through param
}

export interface IFbPostbackPayload {
payload: string;
}

export interface IFbMessaging {
sender: IFbMessageUser;
recipient: IFbMessageUser;
timestamp: number; //timestamp
message?: IFbMessage;
optin?: IFbMessageOptin;
delivery?: Object; // delivery
postback?: Object; // postback with custom val
postback?: IFbPostbackPayload; // postback with custom val
}

export interface IFbMessageEntry {
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface IBotRequest {
location?: ILocation;
link?: ILink;
ref?: string;
payload?: string;
image?: IImage;
text?: string;
raw?: any;
Expand All @@ -87,6 +88,7 @@ export interface IUnknownMessage {

export interface IBotController {
newUser?(request: IBotRequest, reply: IBotReply): void;
postback?(request: IBotRequest, reply: IBotReply): void;
textMessage?(request: IBotRequest, reply: IBotReply): void;
imageMessage?(request: IBotRequest, reply: IBotReply): void;
linkMessage?(request: IBotRequest, reply: IBotReply): void;
Expand Down
24 changes: 22 additions & 2 deletions tests/bot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {IBotSettings, IBotRequest, IBotReply, IBotUser, IBotController} from '..
class DummyController implements IBotController {
cc = {
newUser: 0,
postback: 0,
textMessage: 0,
imageMessage: 0,
linkMessage: 0,
Expand All @@ -15,6 +16,7 @@ class DummyController implements IBotController {
catchAll: 0,
}
newUser(msg: IBotRequest, reply: IBotReply) {console.log('got new user'); this.cc.newUser++;}
postback(msg: IBotRequest, reply: IBotReply) {console.log('got new postback'); this.cc.postback++;}
textMessage(msg: IBotRequest, reply: IBotReply) {console.log('got new textMessage'); this.cc.textMessage++;}
imageMessage(image: IBotRequest, reply: IBotReply) {console.log('got new imageMessage'); this.cc.imageMessage++;}
linkMessage(msg: IBotRequest, reply: IBotReply) {console.log('got linkMessage'); this.cc.linkMessage++;}
Expand Down Expand Up @@ -54,16 +56,16 @@ describe('FacebookBot', () => {
it('should dispatch each message', (done) => {
bot.receiveMessage(fakeMessageMixed())
.then( dispatchResults => {
expect(dispatchResults.length).to.eql(4);
expect(dispatchResults.length).to.eql(5);
expect(ctrl.cc.delivered).to.eql(1);
expect(ctrl.cc.postback).to.eql(1);
expect(ctrl.cc.textMessage).to.eql(1);
expect(ctrl.cc.imageMessage).to.eql(1);
expect(ctrl.cc.locationMessage).to.eql(1);
done();
})
// just console.log testing for now :(
// if anyone knows a good mocking library for typescript youre welcome :)

});

});
Expand Down Expand Up @@ -177,6 +179,24 @@ function fakeMessageMixed() {
}
}
]
},
{
"id":234,
"time":1458692752478,
"messaging":[
{
"sender":{
"id":123
},
"recipient":{
"id":234
},
"timestamp":1458692752478,
"postback":{
"payload":"USER_DEFINED_PAYLOAD"
}
}
]
}
]
};
Expand Down

0 comments on commit ca7585d

Please sign in to comment.