-
Notifications
You must be signed in to change notification settings - Fork 11
/
switch.js
185 lines (152 loc) · 6.23 KB
/
switch.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var utils = require('../../lib/utils.js');
module.exports = function(RED) {
function SwitchNode(config) {
RED.nodes.createNode(this, config);
var node = this;
// Get base configuration
this.bot = RED.nodes.getNode(config.bot);
this.chatId = parseInt(config.chatId);
this.parseMode = config.parseMode;
this.question = config.question || "";
this.answers = config.answers || [];
this.timeoutValue = config.timeoutValue || null;
this.timeoutUnits = config.timeoutUnits || null;
this.timeoutCallback = null;
this.autoAnswerCallback = config.autoAnswerCallback;
this.verticalAnswers = config.verticalAnswers;
// Initialize bot
utils.initializeBot(node);
// Verify inputs
if (isNaN(this.chatId)) {
this.chatId = null;
}
if (this.timeoutValue !== null) {
if (this.timeoutUnits === null) {
utils.updateNodeStatusFailed(node, "timeout units not provided");
return;
}
this.timeoutDuration = utils.timeUnits(parseInt(this.timeoutValue, 10), this.timeoutUnits);
if (this.timeoutDuration === NaN) {
utils.updateNodeStatusFailed(node, "timeout not parsable");
return;
}
if (this.timeoutDuration <= 0) {
utils.updateNodeStatusFailed(node, "timeout should be greater than 0");
return;
}
}
// Compute output ports
var portCount = (this.timeoutValue === null) ? this.answers.length : this.answers.length + 1;
var ports = new Array(portCount);
for (var i = 0; i < portCount; i++) {
ports[i] = null;
}
this.on("input", function(msg){
if (!utils.validateChatId(node, msg)) {
utils.updateNodeStatusFailed(node, "message has no chatID");
return;
}
var chatId = node.chatId || msg.telegram.chat.id;
var question = node.question || msg.payload;
var answers = node.answers || [];
if (question && answers.length > 0) {
var listener = function(botMsg){
var username = botMsg.from.username;
var fromChatId = botMsg.message.chat.id;
var messageId = botMsg.message.message_id;
var callbackQueryId = botMsg.id;
if (botMsg.data && fromChatId === chatId && messageId === msg.telegram.messageId) {
if (node.bot.isAuthorized(chatId, username)) {
// Remove this listener since we got our reply
node.telegramBot.removeListener("callback_query", listener);
// Clear the timeout
if (node.timeoutCallback) {
clearTimeout(node.timeoutCallback);
}
// Update node status
utils.updateNodeStatusReceived(node);
if (node.autoAnswerCallback) {
// Answer the callback so progress can stop showing
node.telegramBot.answerCallbackQuery(callbackQueryId).then(function(sent){
// nothing to do here
});
// Remove quick reply options
node.telegramBot.editMessageReplyMarkup("{}", { chat_id: chatId, message_id: messageId }).then(function(sent){
// nothing to do here
});
}
// Augment original message with additional Telegram info
msg.telegram.callbackQueryId = callbackQueryId;
// Continue with the original message
var outPorts = ports.slice(0);
var outputPort = parseInt(botMsg.data);
if (!isNaN(outputPort) && outputPort < portCount) {
outPorts[outputPort] = msg;
node.send(outPorts);
} else {
node.warn("invalid callback data received from telegram");
}
} else {
node.warn(`received callback in ${chatId} from '${username}' was unauthorized`);
}
} else {
// This is not the listener you are looking for
}
};
var timeoutListener = function(sentMsg){
utils.updateNodeStatus(node, "yellow", "dot", "timed out waiting for reply");
// Remove this listener
node.telegramBot.removeListener("callback_query", listener);
var messageId = sentMsg.message_id;
var sentChatId = sentMsg.chat.id;
// Remove reply keyboard from message
if (messageId && sentChatId) {
node.telegramBot.editMessageReplyMarkup("{}", { chat_id: sentChatId, message_id: messageId }).then(function(sent){
// nothing to do here
});
}
// output to timeout
var outPorts = ports.slice(0);
var outputPort = portCount - 1;
outPorts[outputPort] = msg;
node.send(outPorts);
};
var chunkSize = 4000;
var answerOpts = answers.map(function(answer, idx){
var answer = { text: answer, callback_data: idx };
return node.verticalAnswers ? [answer] : answer;
});
var options = {
parse_mode: node.parseMode,
reply_markup: {
inline_keyboard: node.verticalAnswers ? answerOpts : [answerOpts]
}
};
if (question.length > chunkSize) {
utils.updateNodeStatusFailed(node, "message larger than allowed chunk size");
} else {
node.telegramBot.sendMessage(chatId, question, options).then(function(sent){
// Store sent message so we know how to respond later
msg.telegram = sent;
msg.telegram.chatId = chatId; // deprecated
msg.telegram.messageId = sent.message_id; // deprecated
if (node.timeoutDuration > 0) {
node.timeoutCallback = setTimeout(function(){
timeoutListener(sent);
}, node.timeoutDuration);
utils.updateNodeStatusPending(node, `waiting for reply (${node.timeoutValue}${node.timeoutUnits})`);
} else {
utils.updateNodeStatusPending(node, "waiting for reply");
}
});
node.telegramBot.on("callback_query", listener);
}
}
});
this.on("close", function(){
node.telegramBot.off("message");
node.status({});
});
};
RED.nodes.registerType("telegrambot-switch", SwitchNode);
};