2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-08-28 21:07:39 +00:00

Add simple example in README (PR #54) (#224)

* Fixes the readme with an appropriate example that doesn't crash

Addresses #45

* [docs] Use simple example in Readme

References:

  * Original PR: https://github.com/yagop/node-telegram-bot-api/pull/54

* [docs] Improve the example in the Readme
This commit is contained in:
Gocho Mugo 2016-11-11 16:54:04 +03:00 committed by GitHub
parent 578ad1fa13
commit 75d940c473
2 changed files with 38 additions and 20 deletions

View File

@ -9,23 +9,32 @@ npm install node-telegram-bot-api
```js
var TelegramBot = require('node-telegram-bot-api');
// replace the value below with the Telegram token you receive from @BotFather
var token = 'YOUR_TELEGRAM_BOT_TOKEN';
// Setup polling way
// Create a bot that uses 'polling' to fetch new updates
var bot = new TelegramBot(token, { polling: true });
// Matches /echo [whatever]
// Matches "/echo [whatever]"
bot.onText(/\/echo (.+)/, function (msg, match) {
var fromId = msg.from.id;
var resp = match[1];
bot.sendMessage(fromId, resp);
// 'msg' is the received Message from Telegram
// 'match' is the result of executing the regexp above on the text content
// of the message
var chatId = msg.chat.id;
var resp = match[1]; // the captured "whatever"
// send back the matched "whatever" to the chat
bot.sendMessage(chatId, resp);
});
// Any kind of message
// Listen for any kind of message. There are different kinds of
// messages.
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';
bot.sendPhoto(chatId, photo, {caption: 'Lovely kittens'});
// send a message to the chat acknowledging receipt of their message
bot.sendMessage(chatId, "Received your message");
});
```

View File

@ -9,23 +9,32 @@ npm install node-telegram-bot-api
```js
var TelegramBot = require('node-telegram-bot-api');
// replace the value below with the Telegram token you receive from @BotFather
var token = 'YOUR_TELEGRAM_BOT_TOKEN';
// Setup polling way
// Create a bot that uses 'polling' to fetch new updates
var bot = new TelegramBot(token, { polling: true });
// Matches /echo [whatever]
// Matches "/echo [whatever]"
bot.onText(/\/echo (.+)/, function (msg, match) {
var fromId = msg.from.id;
var resp = match[1];
bot.sendMessage(fromId, resp);
// 'msg' is the received Message from Telegram
// 'match' is the result of executing the regexp above on the text content
// of the message
var chatId = msg.chat.id;
var resp = match[1]; // the captured "whatever"
// send back the matched "whatever" to the chat
bot.sendMessage(chatId, resp);
});
// Any kind of message
// Listen for any kind of message. There are different kinds of
// messages.
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';
bot.sendPhoto(chatId, photo, {caption: 'Lovely kittens'});
// send a message to the chat acknowledging receipt of their message
bot.sendMessage(chatId, "Received your message");
});
```