This repository was archived by the owner on Dec 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgenerics.js
105 lines (84 loc) · 2.9 KB
/
generics.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"use strict";
const { camelCase } = require("lodash");
const translator = require("../utils/translator");
const config = require("../config/config-loader").load();
const BUTTON_TYPE = {
URL: "web_url",
POSTBACK: "postback",
MORE: "postback_more",
ACCOUNT_LINKING: "account_link"
};
// Slack allow 5 buttons max (ie: 4 buttons + 1 button "more"), Reference : https://api.slack.com/docs/interactive-message-field-guide#attachment_fields
// Facebook allow 11 quick replies max (ie: 10 quick replies + 1 reply "more")
const MAX_LIMIT = 4;
class TextMessage {
constructor (text) {
this.text = text;
}
}
class Button {
constructor (type, value, text) {
this.type = type;
this.value = value;
this.text = text;
}
}
class ButtonsMessage {
constructor (text, buttons) {
this.text = text;
if (!Array.isArray(buttons) || buttons.length <= 0 || !(buttons[0] instanceof Button)) {
throw new Error("Buttons isn't correctly formated");
}
// FB allow 3 buttons max, Reference: https://developers.facebook.com/docs/messenger-platform/send-api-reference/button-template
if (buttons.length > 3) {
throw new Error("Buttons cant be longer than 3");
}
this.attachments = {
buttons
};
}
}
class ButtonsListMessage {
constructor (text, buttons) {
this.text = text;
if (!Array.isArray(buttons) || buttons.length <= 0 || !(buttons[0] instanceof Button)) {
throw new Error("Buttons isn't correctly formated");
}
this.attachments = {
buttons
};
}
}
function createPostBackList (text, listInfos, morePayload, offset, limit, locale = "en_US") {
const buttons = listInfos.slice(offset, limit + offset);
const moreButton = offset + limit >= listInfos.length ? null : new Button(BUTTON_TYPE.MORE, `${morePayload}_${offset + limit}`, translator("moreButton", locale, listInfos.length - (offset + limit)));
if (limit > MAX_LIMIT) {
throw new Error("Limit cant be greater than %d", MAX_LIMIT);
}
if (moreButton) {
buttons.push(moreButton);
}
return new ButtonsListMessage(text, buttons);
}
function createFeedback (intent, messageRaw, locale) {
const message = messageRaw.length >= config.maxMessageLength ? config.maxMessageLengthString : messageRaw;
if (intent === "unknown") {
return null;
}
const buttons = [
new Button(BUTTON_TYPE.POSTBACK, `FEEDBACK_MISUNDERSTOOD_${camelCase(intent)}_${message}`, translator("feedbackBadUnderstanding", locale)),
new Button(BUTTON_TYPE.POSTBACK, `FEEDBACK_BAD_${camelCase(intent)}_${message}`, translator("feedbackNo", locale)),
new Button(BUTTON_TYPE.POSTBACK, `FEEDBACK_GOOD_${camelCase(intent)}_${message}`, translator("feedbackYes", locale))
];
return new ButtonsMessage(translator("feedbackHelp", locale), buttons);
}
module.exports = {
TextMessage,
Button,
ButtonsMessage,
ButtonsListMessage,
createPostBackList,
createFeedback,
BUTTON_TYPE,
MAX_LIMIT
};