From 0fb142f6ac1cd0c6395f5a1af9172507e7d989d5 Mon Sep 17 00:00:00 2001 From: GingerPlusPlus Date: Tue, 25 Oct 2016 18:32:35 +0200 Subject: [PATCH] Added onlyFirstMatch option --- README.md | 3 ++- src/telegram.js | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 06f7c7d..7765f91 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ Emits `message` when a message arrives. | [options.webHook] | Boolean | Object | false | Set true to enable WebHook or set options | | [options.webHook.key] | String | | PEM private key to webHook server. | | [options.webHook.cert] | String | | PEM certificate (public) to webHook server. | +| [options.onlyFirstMatch] | Boolean | false | false: try matching all regexps; true: stop after first match | @@ -537,7 +538,7 @@ username of a user, group or channel, etc.). | Param | Type | Description | | --- | --- | --- | -| chatId | Number | String | Unique identifier for the target chat or username of the target supergroup or channel (in the format @channelusername) | +| chatId | Number | String | Unique identifier for the target chat or username of the target supergroup or channel | diff --git a/src/telegram.js b/src/telegram.js index 653cb0d..38321d6 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -41,6 +41,7 @@ class TelegramBot extends EventEmitter { * @param {Boolean|Object} [options.webHook=false] Set true to enable WebHook or set options * @param {String} [options.webHook.key] PEM private key to webHook server. * @param {String} [options.webHook.cert] PEM certificate (public) to webHook server. + * @param {Boolean} [options.onlyFirstMatch=false] Set to true to stop after first match. Otherwise, all regexps are executed * @see https://core.telegram.org/bots/api */ constructor(token, options = {}) { @@ -99,12 +100,14 @@ class TelegramBot extends EventEmitter { TelegramBot.messageTypes.forEach(processMessageType); if (message.text) { debug('Text message'); - this.textRegexpCallbacks.forEach(reg => { + this.textRegexpCallbacks.some(reg => { debug('Matching %s whith', message.text, reg.regexp); const result = reg.regexp.exec(message.text); if (result) { debug('Matches', reg.regexp); reg.callback(message, result); + // returning truthy value exits .some + return this.options.onlyFirstMatch; } }); }