forked from guidone/node-red-contrib-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot-audio.js
60 lines (49 loc) · 1.51 KB
/
chatbot-audio.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
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 ChatBotAudio(config) {
RED.nodes.createNode(this, config);
var node = this;
this.filename = config.filename;
this.name = config.name;
this.transports = ['telegram', 'slack', 'facebook'];
this.on('input', function(msg) {
var path = node.filename;
var name = node.name;
var originalMessage = msg.originalMessage;
var chatId = msg.payload.chatId || (originalMessage && originalMessage.chat.id);
var messageId = msg.payload.messageId || (originalMessage && originalMessage.message_id);
// check transport compatibility
if (!utils.matchTransport(node, msg)) {
return;
}
// todo make asynch here
var content = msg.payload;
if (!_.isEmpty(path)) {
content = fs.readFileSync(path);
}
// get filename
var filename = 'image';
if (!_.isEmpty(path)) {
filename = Path.basename(path);
} else if (!_.isEmpty(name)) {
filename = sanitize(name);
}
// send out the message
msg.payload = {
type: 'audio',
content: content,
filename: filename,
chatId: chatId,
messageId: messageId,
inbound: false
};
// send out reply
node.send(msg);
});
}
RED.nodes.registerType('chatbot-audio', ChatBotAudio);
};