Skip to content

Instantly share code, notes, and snippets.

@raphaklaus
Created July 14, 2016 15:06
Show Gist options
  • Save raphaklaus/f1b1d1d2f95a066c9e4af65b9bb1c24d to your computer and use it in GitHub Desktop.
Save raphaklaus/f1b1d1d2f95a066c9e4af65b9bb1c24d to your computer and use it in GitHub Desktop.
TelegramBot.prototype.onTextReply = function (regexp, userId, callback) {
this.textRegexpCallbacks.push({regexp: regexp, userId: userId,
type: 'reply', callback: callback});
};
TelegramBot.prototype._processUpdate = function (update) {
debug('Process Update %j', update);
var message = update.message;
var inline_query = update.inline_query;
var chosen_inline_result = update.chosen_inline_result;
if (message) {
debug('Process Update message %j', message);
this.emit('message', message);
var processMessageType = function (messageType) {
if (message[messageType]) {
debug('Emtting %s: %j', messageType, message);
this.emit(messageType, message);
}
};
this.messageTypes.forEach(processMessageType.bind(this));
if (message.text) {
debug('Text message');
this.textRegexpCallbacks.forEach(function (reg) {
debug('Matching %s whith', message.text, reg.regexp);
var result = reg.regexp.exec(message.text);
// Here was the only place I've changed...
if (result) {
debug('Matches', reg.regexp);
if (reg.type === 'reply') {
if (reg.userId === message.from.id) {
reg.callback(message, result);
}
} else
reg.callback(message, result);
}
// Ended here...
});
}
if (message.reply_to_message) {
// Only callbacks waiting for this message
this.onReplyToMessages.forEach(function (reply) {
// Message from the same chat
if (reply.chatId === message.chat.id) {
// Responding to that message
if (reply.messageId === message.reply_to_message.message_id) {
// Resolve the promise
reply.callback(message);
}
}
});
}
} else if (inline_query) {
debug('Process Update inline_query %j', inline_query);
this.emit('inline_query', inline_query);
} else if (chosen_inline_result) {
debug('Process Update chosen_inline_result %j', chosen_inline_result);
this.emit('chosen_inline_result', chosen_inline_result);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment