Skip to content

Commit

Permalink
fix messenger templates
Browse files Browse the repository at this point in the history
  • Loading branch information
guidone committed Mar 13, 2022
1 parent fa4b3f8 commit 09fb402
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 12 deletions.
67 changes: 66 additions & 1 deletion __tests__/messenger-template-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('Chat generic template node', () => {
RED.node.get().emit('input', msg);
return RED.node.get().await()
.then(
function() {},
function () { },
function () {
assert.isNull(RED.node.message());
}
Expand Down Expand Up @@ -210,4 +210,69 @@ describe('Chat generic template node', () => {
});
});

it('should be possible to create generic templates upstream', async () => {
const msg = RED.createMessage({

elements: [
{
templateType: 'generic',
title: 'my title',
subtitle: 'I am a sub title',
imageUrl: 'https://picsum.photos/200/300',
buttons: [
{
type: 'url',
url: 'http://javascript-jedi.com',
label: 'Javascript Jedi'
}
]
},
{
templateType: 'generic',
title: 'another title',
subtitle: 'Second sub title',
imageUrl: 'https://picsum.photos/200/300',
buttons: [
{
type: 'url',
url: 'http://javascript-jedi.com',
label: 'Javascript Jedi'
}
]
}
]

}, 'facebook');
RED.node.config({});
MessengerTemplateBlock(RED);
RED.node.get().emit('input', msg);
await RED.node.get().await()

const payload = RED.node.message().payload;

assert.equal(payload.type, 'template');
assert.equal(payload.templateType, 'generic');
assert.equal(payload.chatId, 42);
assert.isArray(payload.elements);
assert.lengthOf(payload.elements, 2);
assert.equal(payload.elements[0].title, 'my title');
assert.equal(payload.elements[0].subtitle, 'I am a sub title');
assert.equal(payload.elements[0].imageUrl, 'https://picsum.photos/200/300');
assert.isArray(payload.elements[0].buttons);
assert.lengthOf(payload.elements[0].buttons, 1);
const buttons = payload.elements[0].buttons;
assert.equal(buttons[0].type, 'url');
assert.equal(buttons[0].url, 'http://javascript-jedi.com');
assert.equal(buttons[0].label, 'Javascript Jedi');

assert.equal(payload.elements[1].title, 'another title');
assert.equal(payload.elements[1].subtitle, 'Second sub title');
assert.equal(payload.elements[1].imageUrl, 'https://picsum.photos/200/300');
assert.isArray(payload.elements[1].buttons);
assert.lengthOf(payload.elements[1].buttons, 1);
const buttons2 = payload.elements[1].buttons;
assert.equal(buttons2[0].type, 'url');
assert.equal(buttons2[0].url, 'http://javascript-jedi.com');
assert.equal(buttons2[0].label, 'Javascript Jedi');
});
});
7 changes: 7 additions & 0 deletions lib/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ function checkValidators(types, name, value) {
});
};
break;
case 'arrayOfFacebookTemplateElements':
validator = function(value) {
return _.isArray(value) && _(value).all((obj) => {
return ['generic', 'product'].includes(obj.templateType);
});
};
break;
default:
// eslint-disable-next-line no-console
console.log(`Unable to find a validator for type "${type}" in extractValue`);
Expand Down
36 changes: 25 additions & 11 deletions nodes/chatbot-messenger-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = function(RED) {
// get values from config
const buttons = extractValue('buttons', 'buttons', node, msg);
const title = extractValue('string', 'title', node, msg);
const templateType = extractValue('string', 'templateType', node, msg);
let templateType = extractValue('string', 'templateType', node, msg);
const text = extractValue('string', 'text', node, msg);
const json = extractValue(['string', 'hash'], 'json', node, msg);
const subtitle = extractValue('string', 'subtitle', node, msg);
Expand All @@ -62,8 +62,16 @@ module.exports = function(RED) {
const mediaType = extractValue('string', 'mediaType', node, msg);
const attachmentId = extractValue('string', 'attachmentId', node, msg);
const productId = extractValue('string', 'productId', node, msg);
let elements = extractValue('arrayOfFacebookTemplateElements', 'elements', node, msg);

let elements = [];
elements = elements != null ? elements : [];

// infer the templateType from a list of elements defined upstream
if (!_.isEmpty(elements) && ['generic', 'product'].includes(elements[0].templateType)
) {
templateType = elements[0].templateType;
}
//let elements = [];
let payload;
// if inbound is another message from a generic template, then push them toghether to create a carousel
if (msg.payload != null && msg.payload.type === 'template') {
Expand Down Expand Up @@ -102,12 +110,15 @@ module.exports = function(RED) {

switch (templateType) {
case 'generic':
elements.push({
title: translated.title,
subtitle: translated.subtitle,
imageUrl: translated.imageUrl,
buttons: translated.buttons
});
// skip if empty
if (!_.isEmpty(title)) {
elements.push({
title: translated.title,
subtitle: translated.subtitle,
imageUrl: translated.imageUrl,
buttons: translated.buttons
});
}
break;
case 'button':
payload = {
Expand All @@ -129,9 +140,12 @@ module.exports = function(RED) {
};
break;
case 'product':
elements.push({
id: translated.productId
});
// skip if empty
if (!_.isEmpty(translated.productId)) {
elements.push({
id: translated.productId
});
}
break;
}

Expand Down

0 comments on commit 09fb402

Please sign in to comment.