forked from guidone/node-red-contrib-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot-image.js
71 lines (59 loc) · 1.85 KB
/
chatbot-image.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
var _ = require('underscore');
var fs = require('fs');
var Path = require('path');
var sanitize = require("sanitize-filename");
var utils = require('./lib/helpers/utils');
module.exports = function(RED) {
function ChatBotImage(config) {
RED.nodes.createNode(this, config);
var node = this;
this.filename = config.filename;
this.name = config.name;
this.caption = config.caption;
this.transports = ['telegram', 'slack', 'facebook', 'smooch'];
this.on('input', function(msg) {
var path = node.filename;
var name = node.name;
var chatId = utils.getChatId(msg);
var messageId = utils.getMessageId(msg);
var content = null;
// check transport compatibility
if (!utils.matchTransport(node, msg)) {
return;
}
if (!_.isEmpty(path)) {
content = fs.readFileSync(path);
} else if (msg.payload instanceof Buffer) {
content = msg.payload;
} else if (_.isObject(msg.payload) && msg.payload.image instanceof Buffer) {
content = msg.payload.image;
}
// get filename
var filename = 'image';
if (!_.isEmpty(path)) {
filename = Path.basename(path);
} else if (!_.isEmpty(name)) {
filename = sanitize(name);
}
var caption = null;
if (!_.isEmpty(node.caption)) {
caption = node.caption;
} else if (_.isObject(msg.payload) && _.isString(msg.payload.caption) && !_.isEmpty(msg.payload.caption)) {
caption = msg.payload.caption;
}
// send out the message
msg.payload = {
type: 'photo',
content: content,
filename: filename,
caption: caption,
chatId: chatId,
messageId: messageId,
inbound: false
};
// send out reply
node.send(msg);
});
}
RED.nodes.registerType('chatbot-image', ChatBotImage);
};