From 430adbba7f144e6f7fffca2eddecf26af945a470 Mon Sep 17 00:00:00 2001 From: Yago Date: Sat, 10 Oct 2015 18:09:25 +0200 Subject: [PATCH] Updated Readme --- README.hbs | 10 +++++++++- README.md | 22 +++++++++++++++++++++- src/telegram.js | 12 ++++++------ 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/README.hbs b/README.hbs index 8a9e61d..ab00242 100644 --- a/README.hbs +++ b/README.hbs @@ -12,7 +12,15 @@ var TelegramBot = require('node-telegram-bot-api'); var token = 'YOUR_TELEGRAM_BOT_TOKEN'; // Setup polling way var bot = new TelegramBot(token, {polling: true}); -bot.on('text', function (msg) { + +bot.onText(/\/echo (.+)/, function (msg, match) { + var fromId = msg.from.id; + var resp = match[1]; + bot.sendMessage(fromId, resp); +}); + +// Any kind of message +bot.on('message', function (msg) { var chatId = msg.chat.id; // photo can be: a file path, a stream or a Telegram file_id var photo = 'cats.png'; diff --git a/README.md b/README.md index 706456f..230e6fe 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,15 @@ var TelegramBot = require('node-telegram-bot-api'); var token = 'YOUR_TELEGRAM_BOT_TOKEN'; // Setup polling way var bot = new TelegramBot(token, {polling: true}); -bot.on('text', function (msg) { + +bot.onText(/\/echo (.+)/, function (msg, match) { + var fromId = msg.from.id; + var resp = match[1]; + bot.sendMessage(fromId, resp); +}); + +// Any kind of message +bot.on('message', function (msg) { var chatId = msg.chat.id; // photo can be: a file path, a stream or a Telegram file_id var photo = 'cats.png'; @@ -64,6 +72,7 @@ TelegramBot * [.getFile(fileId)](#TelegramBot+getFile) ⇒ Promise * [.getFileLink(fileId)](#TelegramBot+getFileLink) ⇒ Promise * [.downloadFile(fileId, downloadDir)](#TelegramBot+downloadFile) ⇒ Promise + * [.onText(regexp, callback)](#TelegramBot+onText) ### new TelegramBot(token, [options]) @@ -305,4 +314,15 @@ This is just a sugar for (getFile)[#getfilefiled] method | fileId | String | File identifier to get info about | | downloadDir | String | Absolute path to the folder in which file will be saved | + +### telegramBot.onText(regexp, callback) +Register a RegExp to test against an incomming text message. + +**Kind**: instance method of [TelegramBot](#TelegramBot) + +| Param | Type | Description | +| --- | --- | --- | +| regexp | RegExp | RegExp to be executed with `exec`. | +| callback | function | Callback will be called with 2 parameters, the `msg` and the result of executing `regexp.exec` on message text. | + * * * diff --git a/src/telegram.js b/src/telegram.js index e5fe1c6..dba6f43 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -489,13 +489,13 @@ TelegramBot.prototype.downloadFile = function(fileId, downloadDir) { }; /** - * Register a RegExp to test against an incomming text message - * @param {RegExp} regexp [RegExp to be executed with `exec`] - * @param {Function} fn [Callback receives 2 parameters, the msg and the - * result of executing `regexp.exec` on message text] + * Register a RegExp to test against an incomming text message. + * @param {RegExp} regexp RegExp to be executed with `exec`. + * @param {Function} callback Callback will be called with 2 parameters, + * the `msg` and the result of executing `regexp.exec` on message text. */ -TelegramBot.prototype.onText = function (regexp, fn) { - this.textRegexpCallbacks.push({regexp: regexp, callback: fn}); +TelegramBot.prototype.onText = function (regexp, callback) { + this.textRegexpCallbacks.push({regexp: regexp, callback: callback}); }; module.exports = TelegramBot;