2.0.0 • Published 8 years ago

node-tg-bot v2.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

node-tg-bot

node-tg-bot is a node module for implementing Telegram bots in Nodejs using Telegram Bot Api 2.3.1

Installing

npm install node-tg-bot --save

Getting Started

const Telegram = require('node-tg-bot');

Then to create a bot (the polling way)

const token = 'YOUR BOT TOKEN';
const bot = new Telegram.Bot(token, {
    polling: {
        interval: 1000,
        timeout: 4000
    }
});

Or the webhook way (see https://core.telegram.org/bots/self-signed to create your self signed ssl cert/key)

var bot = new TelegramBot(token, {
    webHook: {
        key: 'your_private.key',
        cert: 'your_public.pem'
    }
});

Then if you are using a webhook

bot.setWebHook('https://yourdomain.com:port/' + token, 'your_public.pem', function (error) {
    if (!error) {
        console.log('webhook estabilished');
    } else {
        console.log('error:', error);
    }
});

Commands

Since node-tg-bot is an event emitter you can listen for events to be fired when commands with the same name are sent

bot.on('foo', (params, message) => {
    //on /foo command
    bot.sendMessage(message.chat.id, 'Bar!');
}).on('bar', function (params, message) {
    //on bar command
    this.sendMessage(message.chat.id, 'Foo!');
    //'this' is the Telegram.Bot instance itself
}).on('hello', (params, message) => {
    bot.sendMessage(message.chat.id, 'What\'s your favorite animal?', {
        reply_markup: {
            keyboard: [
                ['\uD83D\uDC31', '\uD83D\uDC36'],
                ['\uD83D\uDC30', '\uD83D\uDC39']
            ],
            resize_keyboard: true,
            one_time_keyboard: true
        }
    }).then(message => {
        bot.once('from.' + message.from.id, m => {
            if (m.text === '\uD83D\uDC31') bot.sendMessage(m.from.id, 'Ok... :)!');
            else if (m.text === '\uD83D\uDC36') bot.sendMessage(m.from.id, 'Me too!!!!');
            else if (m.text === '\uD83D\uDC30') bot.sendMessage(m.from.id, 'Ok... :)!');
            else if (m.text === '\uD83D\uDC39') bot.sendMessage(m.from.id, 'Ok... :)!');
            else bot.sendMessage(m.from.id, 'Bye.. :(');
        });
    });
});

Command parameters are passed in the params argument

/*
    we send "/hello world"
*/
bot.on('hello', (params, message) => {
    bot.sendMessage(message.chat.id, 'Hi, ' + params[0]);
});
/*
    bot sends "Hi, world"
*/

Some events are emitted by default by node-tg-bot such as 'update'. This event is called everytime an update is available

bot.on('update', function(update) {
    console.log(update);
});

or the '' event, called whenever an unknown command is sent and 'photo', fired when a photo is uploaded in a chat / conversation

bot.on('', function (params, msg) {
    console.log('uknown command, ' + params[0]);
}).on('photo', function (message) {
    console.log('photo recieved', message.photo);
}).on('audio', function (message) {
    console.log('audio recieved', message.audio);
}).on('video', function (message) {
    console.log('video recieved', message.video);
});

Here is how you can use your Message Instance

bot.on('location', function (message) {
    //message.chat.sendLocation(message.location.latitude, message.location.longitude);
    //or
    //message.from.sendLocation(message.location.latitude, message.location.longitude);
    //or
    //message.location.sendTo(message.from);
    //or
    message.location.sendTo(message.chat).then(message => {
        //do something with message
    }).catch(error => {
        console.log(error);
    });
}).on('', () => {

});

Classes, methods and properties

Each of the available types (most of them) are implemented as Classes. Properties, when possible, are Instances of available types as well. As an example when we have an incoming update or photo we get

bot.on('update', function (update) {
    //update is an instance of Update class
    //update.message is an instance of Message class which has a sendTo method
    //so we can do something like this
    update.message.sendTo('@agroup');
}).on('photo', function (message) {
    //photo is an array of Instance of PhotoSize
    //PhotoSize class has inherits get method from FileType so we can do
    message.photo[message.photo.length - 1].get(function (error, file) {
        message.from.sendMessage(file.url);
    });
});

For further details on types properties refer to https://core.telegram.org/bots/api#available-types

List of available Classes

Telegram.Bot

setWebhook getUpdates getMe sendMessage sendPhoto sendAudio sendDocument sendSticker sendVideo sendVoice sendLocation sendVenue sendContact sendChatAction getUserProfilePhotos getFile kickChatMember leaveChat unbanChatMember getChat getChatAdministrators getChatMembersCount getChatMember answerCallbackQuery answerInlineQuery editMessageText editMessageCaption editMessageReplyMarkup sendGame setGameScore getGameHighScores create an anchor

  • .setWebhook(url<String>, certificate<InputFile>)
    • Returns: Promise<Boolean>
  • .getUpdates(offset<Integer>, limit<Integer>, timeout<Integer>, allowed_updates<Array<String>>)
    • Returns: Promise<Array<Update>>
  • .getMe()
    • Returns: Promise<User>

Update

Properties

Message

Properties
Methods
  • message.forwardTo(recipient_id)
  • message.editText(text[, options)
  • message.editCaption(options)
  • message.editReplyMarkup(options)
  • message.getType(void)

User

Properties
Methods
  • user.sendMessage(text[,options)
  • user.forwardMessage(message)
  • user.sendPhoto(photo[, options)
  • user.sendAudio(audio[, options)
  • user.sendDocument(document[, options)
  • user.sendSticker(sticker[, options)
  • user.sendVideo(video[, options)
  • user.sendVoice(voice[, options)
  • user.sendLocation(latitude, longitude[, options)
  • user.sendVenue(latitude, longitude, title, address[, options)
  • user.sendChatAction(chat_action)
  • user.getUserProfilePhotos(offset, limit)
  • user.kickFromChat(chat_id)
  • user.unbanFromChat(chat_id)

Chat

Properties
Methods
  • chat.sendMessage(text[,options)
  • chat.forwardMessage(message)
  • chat.sendPhoto(photo[, options)
  • chat.sendAudio(audio[, options)
  • chat.sendDocument(document[, options)
  • chat.sendSticker(sticker[, options)
  • chat.sendVideo(video[, options)
  • chat.sendVoice(voice[, options)
  • chat.sendLocation(latitude, longitude[, options)
  • chat.sendVenue(latitude, longitude, title, address[, options)
  • chat.sendChatAction(chat_action)
  • chat.getUserProfilePhotos(offset, limit)
  • chat.kickMember(user_id)
  • chat.unbanMember(user_id)

Methods

setWebHook

getMe

sendMessage

forwardMessage

sendPhoto

sendAudio

sendVoice

sendVideo

sendDocument

sendSticker

sendChatAction

sendLocation

sendVenue

answerInlineQuery

2.0.0

8 years ago

1.9.4

9 years ago

1.9.3

9 years ago

1.9.2

9 years ago

1.9.1

9 years ago

1.9.0

9 years ago

1.8.2

9 years ago

1.8.1

9 years ago

1.8.0

9 years ago

1.3.4

9 years ago

1.3.3

9 years ago

1.3.2

9 years ago

1.3.1

9 years ago

1.1.21

9 years ago

1.1.20

9 years ago

1.1.19

9 years ago

1.1.17

9 years ago

1.1.16

9 years ago

1.1.15

9 years ago

1.1.14

9 years ago

1.1.13

9 years ago

1.1.12

9 years ago

1.1.11

9 years ago

1.1.10

9 years ago

1.1.9

9 years ago

1.1.8

9 years ago

1.1.7

9 years ago

1.1.6

9 years ago

1.1.5

9 years ago

1.1.4

9 years ago

1.1.3

9 years ago

1.1.2

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.0

9 years ago