Skip to content

Commit

Permalink
Fix Quick Replies processing, thanks to @smaheshw
Browse files Browse the repository at this point in the history
  • Loading branch information
Danil Skachkov committed Feb 14, 2017
1 parent 80107be commit 3d9c5d3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,32 @@ class FacebookBot {
});
}

getEventText(event) {
if (event.message) {
if (event.message.quick_reply && event.message.quick_reply.payload) {
return event.message.quick_reply.payload;
}

if (event.message.text) {
return event.message.text;
}
}

if (event.postback && event.postback.payload) {
return event.postback.payload;
}

return null;

}

processEvent(event) {
const sender = event.sender.id.toString();
const text = this.getEventText(event);

if ((event.message && event.message.text) || (event.postback && event.postback.payload)) {
const text = event.message ? event.message.text : event.postback.payload;
// Handle a text message from this sender
if (text) {

// Handle a text message from this sender
if (!this.sessionIds.has(sender)) {
this.sessionIds.set(sender, uuid.v4());
}
Expand Down
37 changes: 37 additions & 0 deletions test/app_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,41 @@ describe('app', () => {

result.should.deepEqual(expected);
});

it("getEventText should get right text", () => {

const fbBot = app.__get__('facebookBot');
const getEventText = fbBot.getEventText;

should(getEventText({})).be.equal(null);
should(getEventText({message: {}})).be.equal(null);

const expectedText = 'expectedText';

const simpleMessage = {
"message": {
"text": expectedText,
}
};
getEventText(simpleMessage).should.be.equal(expectedText);

const postbackMessage = {
"postback": {
"payload": expectedText
}
};
getEventText(postbackMessage).should.be.equal(expectedText);

const quickReplyMessage = {
"message": {
"text": "Red",
"quick_reply": {
"payload": expectedText
}
}
};
getEventText(quickReplyMessage).should.be.equal(expectedText);

});

});

0 comments on commit 3d9c5d3

Please sign in to comment.