2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-08-22 18:07:16 +00:00

Updated Example to use onText

This commit is contained in:
Rey 2015-10-20 00:01:02 +08:00
parent 2b58384ba5
commit 59d47f1fba

View File

@ -11,26 +11,30 @@ var bot = new TelegramBot(token, options);
bot.getMe().then(function (me) { bot.getMe().then(function (me) {
console.log('Hi my name is %s!', me.username); console.log('Hi my name is %s!', me.username);
}); });
bot.on('text', function (msg) {
var chatId = msg.chat.id; bot.onText(/\/photo.*/, function (msg) {
if (msg.text == '/photo') { var fromId = msg.from.id;
// From file // From file
var photo = __dirname+'/../test/bot.gif'; var photo = __dirname+'/../test/bot.gif';
bot.sendPhoto(chatId, photo, {caption: "I'm a bot!"}); bot.sendPhoto(fromId, photo, {caption: "I'm a bot!"});
} });
if (msg.text == '/audio') {
var url = 'https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg'; bot.onText(/\/audio.*/, function (msg) {
var fromId = msg.from.id;
var url = 'https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg';
// From HTTP request! // From HTTP request!
var audio = request(url); var audio = request(url);
bot.sendAudio(chatId, audio) bot.sendAudio(fromId, audio)
.then(function (resp) { .then(function (resp) {
// Forward the msg // Forward the msg
var messageId = resp.message_id; var messageId = resp.message_id;
bot.forwardMessage(chatId, chatId, messageId); bot.forwardMessage(fromId, fromId, messageId);
}); });
} });
if (msg.text == '/love') {
var opts = { bot.onText(/\/love.*/, function (msg) {
var fromId = msg.from.id;
var opts = {
reply_to_message_id: msg.message_id, reply_to_message_id: msg.message_id,
reply_markup: JSON.stringify({ reply_markup: JSON.stringify({
keyboard: [ keyboard: [
@ -38,6 +42,11 @@ bot.on('text', function (msg) {
['No, sorry there is another one...']] ['No, sorry there is another one...']]
}) })
}; };
bot.sendMessage(chatId, 'Do you love me?', opts); bot.sendMessage(fromId, 'Do you love me?', opts);
} });
bot.onText(/\/echo (.+)/, function (msg, match) {
var fromId = msg.from.id;
var resp = match[1];
bot.sendMessage(fromId, resp);
}); });