Skip to content

Commit

Permalink
Merge pull request #504 from guidone/fix-sticker
Browse files Browse the repository at this point in the history
Fix sticker
  • Loading branch information
guidone authored Sep 26, 2021
2 parents f3361c3 + d488a44 commit 3998b22
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 61 deletions.
29 changes: 24 additions & 5 deletions __tests__/rules-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var _ = require('underscore');
var assert = require('chai').assert;
var RED = require('../lib/red-stub')();
var RulesBlock = require('../nodes/chatbot-rules');
const assert = require('chai').assert;
const RED = require('../lib/red-stub')();
const RulesBlock = require('../nodes/chatbot-rules');

describe('Chat rules node', () => {

Expand Down Expand Up @@ -675,6 +674,27 @@ describe('Chat rules node', () => {
});
});

it('should go through the first if the variable is eq to test_value with louse checking', () => {
const msg = RED.createMessage({
content: 'no command',
type: 'image'
}, 'telegram', { environment: 'development'});
RED.node.config({
rules: [
{ type: 'isVariable', variable: 'myVar', value: '1' },
{ type: 'catchAll' }
]
});
msg.chat().set('myVar', 1);
RulesBlock(RED);
RED.node.get().emit('input', msg);
return RED.node.get().await()
.then(() => {
assert.equal(RED.node.message(0).originalMessage.chat.id, '42');
assert.isNull(RED.node.message(1));
});
});

it('should go through the second if the variable is neq to test_value', () => {
const msg = RED.createMessage({
content: 'no command',
Expand Down Expand Up @@ -938,4 +958,3 @@ describe('Chat rules node', () => {
});

});

1 change: 1 addition & 0 deletions bin/generate-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var mappings = {
'Context-node.md': 'chatbot-context.html',
'Sticker-node.md': 'chatbot-sticker.html',
'Animation-node.md': 'chatbot-animation.html',
'Telegram-Menu-node.md': 'chatbot-telegram-menu.html',
'Waiting-node.md': 'chatbot-waiting.html',
'Slack-Receiver-node.md': 'chatbot-slack-receive.html|chatbot-slack-node',
'Telegram-Receiver-node.md': 'chatbot-telegram-receive.html|chatbot-telegram-node',
Expand Down
55 changes: 39 additions & 16 deletions lib/platforms/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,13 @@ Telegram.in(async function(message) {
});

Telegram.in(function(message) {
var connector = this.getOptions().connector;
var botMsg = message.originalMessage;
var chatServer = this;
return new Promise(function(resolve, reject) {
var type = null;
var fileId = null;
const connector = this.getOptions().connector;
const botMsg = message.originalMessage;
const chatServer = this;
return new Promise((resolve, reject) => {
let type = null;
let fileId = null;
let duration = null;
// download one of these binary types
if (botMsg.photo != null) {
type = 'photo';
Expand All @@ -339,26 +340,29 @@ Telegram.in(function(message) {
} else if (botMsg.voice != null) {
type = 'audio';
fileId = botMsg.voice.file_id;
duration = botMsg.voice.duration;
} else if (botMsg.document != null) {
type = 'document';
fileId = botMsg.document.file_id;
} else if (botMsg.video_note != null) {
type = 'video_note';
fileId = botMsg.video_note.file_id;
duration = botMsg.video_note.duration;
}
// if not one of these pass thru
if (type != null) {
message.payload.type = type;
connector.getFileLink(fileId)
.then(function(path) {
return chatServer.downloadFile(path);
})
.then(path => chatServer.downloadFile(path))
.then(
function(buffer) {
buffer => {
message.payload.content = buffer;
message.payload.caption = botMsg.caption;
message.payload.duration = duration;
resolve(message);
},
function() {
reject('Error downloading photo');
});
() => reject('Error downloading photo')
);
} else {
resolve(message);
}
Expand Down Expand Up @@ -446,7 +450,10 @@ Telegram.out('reset-buttons', function(message) {
message.payload.content,
{
reply_markup: { remove_keyboard: true },
parse_mode: param('parseMode', null)
parse_mode: param('parseMode', null),
disable_web_page_preview: param('disableWebPagePreview', false),
disable_notification: param('silent', false),
reply_to_message_id: param('replyToMessage', false) ? getMessageId(message) : null
}
)
.then(() => message);
Expand Down Expand Up @@ -487,7 +494,10 @@ Telegram.out('buttons', function(message) {
resize_keyboard: true,
one_time_keyboard: true
}),
parse_mode: param('parseMode', null)
parse_mode: param('parseMode', null),
disable_web_page_preview: param('disableWebPagePreview', false),
disable_notification: param('silent', false),
reply_to_message_id: param('replyToMessage', false) ? getMessageId(message) : null
};
// finally send
connector.sendMessage(
Expand All @@ -505,6 +515,7 @@ Telegram.out('buttons', function(message) {
Telegram.out('request', function(message) {
var options = this.getOptions();
var connector = options.connector;
var param = params(message);
return new Promise(function(resolve, reject) {
var keyboard = null;
if (message.payload.requestType === 'location') {
Expand All @@ -529,7 +540,11 @@ Telegram.out('request', function(message) {
keyboard: keyboard,
'resize_keyboard': true,
'one_time_keyboard': true
})
}),
parse_mode: param('parseMode', null),
disable_web_page_preview: param('disableWebPagePreview', false),
disable_notification: param('silent', false),
reply_to_message_id: param('replyToMessage', false) ? getMessageId(message) : null
})
.then(function() {
resolve(message);
Expand Down Expand Up @@ -603,6 +618,7 @@ Telegram.out('inline-buttons', function(message) {
inline_keyboard: inlineKeyboard
}),
parse_mode: param('parseMode', null),
disable_web_page_preview: param('disableWebPagePreview', false),
disable_notification: param('silent', false),
reply_to_message_id: param('replyToMessage', false) ? getMessageId(message) : null
});
Expand Down Expand Up @@ -647,6 +663,7 @@ Telegram.out('message', function(message) {
task = task.then(() =>
connector.sendMessage(message.payload.chatId, partial, {
parse_mode: param('parseMode', null),
disable_web_page_preview: param('disableWebPagePreview', false),
disable_notification: param('silent', false),
reply_to_message_id: param('replyToMessage', false) ? getMessageId(message) : null
})
Expand Down Expand Up @@ -950,6 +967,7 @@ Telegram.registerMessageType(
return null;
}
);
Telegram.registerMessageType('video_note', 'Video Note', 'Video note (short video)');

Telegram.registerMessageType(
'document',
Expand Down Expand Up @@ -1013,6 +1031,11 @@ Telegram.registerParam(
]
}
);
Telegram.registerParam(
'disableWebPagePreview',
'boolean',
{ label: 'Disable web page preview', description: 'Disables link previews for links in this message' }
);

Telegram.registerEvent('new-user', 'New user');

Expand Down
4 changes: 4 additions & 0 deletions nodes/chatbot-redbot.html
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@
}
return valid;
};
$.RedBot.validate.telegram = {};
$.RedBot.validate.telegram.command = function(params) {
return typeof params === 'object' && typeof params.command === 'string' && params.command != null && params.command !== '';
};
$.RedBot.buttonTypes.findButton = function(type) {
var button = null;
this.forEach(function(current) {
Expand Down
4 changes: 2 additions & 2 deletions nodes/chatbot-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const Types = {
} else {
reject();
}
});
});
});
},

Expand Down Expand Up @@ -139,7 +139,7 @@ const Types = {
when(chatContext.get(rule.variable))
.then(
function(variable) {
if (variable === rule.value) {
if (variable == rule.value) {
resolve(rule);
} else {
reject();
Expand Down
30 changes: 15 additions & 15 deletions nodes/chatbot-sticker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const fetchers = require('../lib/helpers/fetchers-obj');
const validators = require('../lib/helpers/validators');
const { ChatExpress } = require('chat-platform');
const RegisterType = require('../lib/node-installer');
const {
enrichFilePayload,
isValidMessage,
getChatId,
getMessageId,
getTransport,
extractValue
const {
enrichFilePayload,
isValidMessage,
getChatId,
getMessageId,
getTransport,
extractValue
} = require('../lib/helpers/utils');

module.exports = function(RED) {
Expand All @@ -24,7 +24,7 @@ module.exports = function(RED) {
this.name = config.name;
this.caption = config.caption;
this.filename = config.filename; // for retrocompatibility

this.on('input', function(msg) {
const chatId = getChatId(msg);
const messageId = getMessageId(msg);
Expand All @@ -45,7 +45,7 @@ module.exports = function(RED) {
|| extractValue('stringWithVariables', 'sticker', node, msg)
|| extractValue('string', 'filename', node, msg, false, true, false); // for retrocompatibility
let caption = extractValue('string', 'caption', node, msg, false);

template({ content, caption })
.then(({ content, caption }) => {
// get the content
Expand All @@ -60,7 +60,7 @@ module.exports = function(RED) {
node.error('Looks like you are passing a very long string (> 4064 bytes) in the payload as image url or path\n'
+ 'Perhaps you are using a "Http request" and passing the result as string instead of buffer?');
return;
} else if (_.isString(content) && content.length < 50) {
} else if (_.isString(content) && content.length < 256) {
// could be a sticker id, it's ok
fetcher = fetchers.identity;
} else {
Expand All @@ -69,11 +69,11 @@ module.exports = function(RED) {
}

fetcher(content)
.then(file => enrichFilePayload(file, msg, node))
.then(file => enrichFilePayload(file, msg, node))
.then(file => {
// check if a valid file
const error = ChatExpress.isValidFile(transport, 'sticker', file);
if (error != null) {
if (error != null) {
node.error(error);
throw error;
}
Expand All @@ -88,9 +88,9 @@ module.exports = function(RED) {
type: 'sticker',
content: file.buffer,
filename: file.filename,
caption: caption,
chatId: chatId,
messageId: messageId,
caption,
chatId,
messageId,
inbound: false
}
});
Expand Down
89 changes: 89 additions & 0 deletions nodes/chatbot-telegram-menu.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<script type="text/javascript">
$.RedBot.registerType('chatbot-telegram-menu', {
category: $.RedBot.config.name,
color: '#FFCC66',
defaults: {
bot: {
value: "",
type: 'chatbot-telegram-node',
required: true
},
items: {
value: [],
validate: function(items) {
var valid = true;
var idx;
for(idx = 0; idx < items.length; idx++) {
if (!$.RedBot.validate.telegram.command(items[idx])) {
valid = false;
}
}
return valid;
}
}
},
inputs: 1,
outputs: 0,
paletteLabel: 'Telegram Menu',
icon: 'chatbot-menu.png',
label: function() {
return this.name || 'Telegram Menu';
},
oneditsave: function() {
var items = $("#node-input-items-container").editableList('items');
var node = this;
node.items = [];
var idx;
for(idx = 0; idx < items.length; idx++) {
var container = $(items[idx]);
node.items.push({
command: $('input[name=command]', container).val(),
description: $('input[name=description]', container).val(),
});
}
},
oneditprepare: function() {
$('#node-input-items-container').css('min-height','300px').css('min-width','450px').editableList({
addButton: 'Add command',
addItem: function(container, i, item) {
var main = $('<div/>').appendTo(container)
.append('<input class="node-input-rule-type" type="text" name="command" style="width:30%" placeholder="/command"/>')
.append('<input class="node-input-rule-type" type="text" name="description" style="width:60%;margin-left:10px;" placeholder="Description"/>')
$('input[name=command]', container).val(item.command);
$('input[name=description]', container).val(item.description);
},
removable: true,
sortable: true
});
if (this.items != null) {
this.items.forEach(function(item) {
$('#node-input-items-container').editableList('addItem', item);
});
}
},
oneditresize: function() {
var dialogForm = $('#dialog-form');
var rowBot = $('.form-row-bot', dialogForm);
var rowLabel = $('.form-row-label', dialogForm);
var height = dialogForm.height() - rowLabel.height() - rowBot.height() - 30;
$('#node-input-items-container').editableList('height', height);
}
});
</script>

<script type="text/x-red" data-template-name="chatbot-telegram-menu">
<div class="form-row form-row-bot">
<label for="node-input-bot" style="display:block;width:100%;">Telegram Bot</label>
<input type="text" id="node-input-bot" placeholder="Bot">
</div>
<div class="form-row form-row-label form-row-buttons" style="margin-bottom:0;">
<label><span>Commands</span></label>
</div>
<div class="form-row form-row-items node-input-rule-container-row">
<ol id="node-input-items-container"></ol>
</div>
</script>

<script type="text/x-red" data-help-name="chatbot-telegram-menu"><p>This node allows to create a persistant shortcuts menu in a Telegram bot. It&#39;s only possible to specify commands (i.e. <code>/my_command</code>) along with a simple description.</p>
<p>There&#39;s no need to connect this node with a <code>Telegram Receiver node</code> or a <code>Telegram Sender node</code>, its generally connect to a Node-RED <code>Inject node</code> which is configured to fire when the flow starts.</p>
</script>
Loading

0 comments on commit 3998b22

Please sign in to comment.