Skip to content

Commit 035a8ff

Browse files
committed
Updated docs. Added postback serialization. Improved tests.
1 parent 8dd5cce commit 035a8ff

File tree

10 files changed

+461
-21
lines changed

10 files changed

+461
-21
lines changed

README.md

Lines changed: 367 additions & 11 deletions
Large diffs are not rendered by default.

incoming/postback.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = postback;
66
*
77
* @this Fbbot#
88
* @param {object} payload - messaging envelop object
9-
* @param {function} callback - invoked after type casting is done
9+
* @param {function} callback - invoked after parsing is done
1010
*/
1111
function postback(payload, callback)
1212
{

lib/send.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ function generateMessage(type, data)
174174
break;
175175

176176
case types.TEXT:
177-
message = {text: data};
177+
// `text` must be UTF-8 and has a 320 character limit
178+
// https://developers.facebook.com/docs/messenger-platform/send-api-reference/text-message
179+
message = {text: data.substr(0, 320)};
178180
break;
179181

180182
case types.AUDIO:
@@ -187,7 +189,7 @@ function generateMessage(type, data)
187189
case types.QUICK_REPLIES:
188190
// `quick_replies` is limited to 10
189191
// https://developers.facebook.com/docs/messenger-platform/send-api-reference/quick-replies
190-
message = {text: data.text, quick_replies: (data.quick_replies || data.elements).slice(0, 10)};
192+
message = {text: data.text, quick_replies: data.quick_replies.slice(0, 10)};
191193
break;
192194

193195
case types.GENERIC:

outgoing/button_postback.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = buttonPostback;
2+
3+
/**
4+
* Stringifies provided button.postback payload
5+
*
6+
* @this Fbbot#
7+
* @param {object} payload - button.postback object
8+
* @param {function} callback - invoked after stringification is done
9+
*/
10+
function buttonPostback(payload, callback)
11+
{
12+
if (typeof payload.payload != 'string')
13+
{
14+
payload.payload = JSON.stringify(payload.payload);
15+
}
16+
17+
callback(null, payload);
18+
}

outgoing/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
var attach = require('../lib/attach.js');
22

33
var filters = {
4+
5+
'send.button.postback': [
6+
require('./button_postback.js')
7+
],
8+
9+
'send.quick_reply': [
10+
require('./quick_reply.js')
11+
]
12+
413
};
514

615
module.exports = attach.bind(null, filters);

outgoing/quick_reply.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = quickReply;
2+
3+
/**
4+
* Stringifies provided quick reply payload
5+
*
6+
* @this Fbbot#
7+
* @param {object} payload - quick_reply object
8+
* @param {function} callback - invoked after stringification is done
9+
*/
10+
function quickReply(payload, callback)
11+
{
12+
if (typeof payload.payload != 'string')
13+
{
14+
payload.payload = JSON.stringify(payload.payload);
15+
}
16+
17+
callback(null, payload);
18+
}

test/fixtures/outgoing/button.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
}, {
1313
"type": "postback",
1414
"title": "Start Chatting",
15-
"payload": "USER_DEFINED_PAYLOAD"
15+
"payload": {
16+
"custom": "payload",
17+
"provides_as": "object"
18+
}
1619
}]
1720
}
1821
},
@@ -34,7 +37,7 @@
3437
}, {
3538
"type": "postback",
3639
"title": "Start Chatting",
37-
"payload": "USER_DEFINED_PAYLOAD"
40+
"payload": "{\"custom\":\"payload\",\"provides_as\":\"object\"}"
3841
}]
3942
}
4043
}

test/fixtures/outgoing/message.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,9 @@
319319
}, {
320320
"content_type": "text",
321321
"title": "Green",
322-
"payload": "DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_GREEN"
322+
"payload": {
323+
"developer": ["defined", "payload", "for", "picking", "green"]
324+
}
323325
}]
324326
}
325327
},
@@ -337,7 +339,7 @@
337339
}, {
338340
"content_type": "text",
339341
"title": "Green",
340-
"payload": "DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_GREEN"
342+
"payload": "{\"developer\":[\"defined\",\"payload\",\"for\",\"picking\",\"green\"]}"
341343
}]
342344
}
343345
},

test/fixtures/outgoing/quick_replies.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"count": {
4-
"expected": 2,
4+
"expected": 3,
55
"hook": "send.quick_reply"
66
},
77
"arguments": {
@@ -16,7 +16,12 @@
1616
}, {
1717
"content_type": "text",
1818
"title": "Green",
19-
"payload": "DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_GREEN"
19+
"payload": {
20+
"custom": "payload",
21+
"for": "quick_reply"
22+
}
23+
}, {
24+
"content_type": "location"
2025
}]
2126
}
2227
},
@@ -34,7 +39,9 @@
3439
}, {
3540
"content_type": "text",
3641
"title": "Green",
37-
"payload": "DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_GREEN"
42+
"payload": "{\"custom\":\"payload\",\"for\":\"quick_reply\"}"
43+
}, {
44+
"content_type": "location"
3845
}]
3946
}
4047
},

test/fixtures/outgoing/text.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,30 @@
1919
"recipient_id": "10157033896470555",
2020
"message_id": "mid.1456970487948:c34767dfe57ee6e339"
2121
}
22+
},
23+
24+
{
25+
"meta": {
26+
"description": "Truncates extra characters from the provided string"
27+
},
28+
"arguments": {
29+
"user": "10157033896470556",
30+
"type": "TEXT",
31+
"data": "In 1988, Joseph D. Becker published the first Unicode draft proposal. At the basis of his design was the naive assumption that 16 bits per character would suffice. In 1991, the first version of the Unicode standard was published, with code points limited to 16 bits. In the following years many systems have added support for Unicode and switched to the UCS-2 encoding. It was especially attractive for new technologies, such as the Qt framework (1992), Windows NT 3.1 (1993) and Java (1995)."
32+
},
33+
34+
"expected": {
35+
"recipient": {
36+
"id": "10157033896470556"
37+
},
38+
"message": {
39+
"text": "In 1988, Joseph D. Becker published the first Unicode draft proposal. At the basis of his design was the naive assumption that 16 bits per character would suffice. In 1991, the first version of the Unicode standard was published, with code points limited to 16 bits. In the following years many systems have added suppor"
40+
}
41+
},
42+
43+
"response": {
44+
"recipient_id": "10157033896470556",
45+
"message_id": "mid.1456970487948:c44767dfe57ee6e339"
46+
}
2247
}
2348
]

0 commit comments

Comments
 (0)