1.4.0 • Published 10 years ago

tg-bot v1.4.0

Weekly downloads
2
License
MIT
Repository
github
Last release
10 years ago

Telegram Bot API module for NodeJS

The Bot API is an HTTP-based interface created for developers keen on building bots for Telegram. Read more about the bots at the introduction for developers page.

Installation

Installation via npm:

$ npm install tg-bot --save

Initialization

var Bot = require('tg-bot');
var bot = new Bot();

Setting token

For calling API you need special token wich you can receive using BotFather. Read more about how to receive token.

You can set token in three different ways. At the initialization:

var Bot = require('tg-bot');
var bot = new Bot({token: "115552161:AAGBbCX-y9O2tBdMXs6GOpeDrGrXpbiqlCs"});

Using special method:

bot.setToken("115552161:AAGBbCX-y9O2tBdMXs6GOpeDrGrXpbiqlCs");

At the API call:

var token = "115552161:AAGBbCX-y9O2tBdMXs6GOpeDrGrXpbiqlCs";
bot.sendMessage({token: token, chat_id: 1234, text: "Hello world!"}, function(err, reply){
	
});

Calling API

See the full list of API at the Telegram Bot API page.

You can use two methods for calling API. Using object's methods:

bot.getMe(function(err, my_data){
	
});

bot.sendMessage({chat_id: 1234, text: "Hello world!"}, function(err, reply){
	
});

Or using direct API calls:

bot.apiCall("getMe", function(err, my_data){
	
});

Parsing commands

Module automatic detect commands in updates and parsing params of command. If we will send message like this:

/ticketadd some values -i info "some long tex"

We will receive next update object with additional field command:

{
	"update_id": 123456,
		"message":{
		"message_id": 123456,
		"from":{},
		"chat":{},
		"date":1435701586,
		"text":"/activate some params -i info \"some long tex\" --flag --state=activated",
		"command":{
			"name":"activate",
			"params":{
				"_":[
					"some",
					"params",
					"some long tex"
				],
				"i":"info",
				"flag":true,
				"state":"activated"
			}
		}
	}
}

Uploading files

You can send files in four different ways. Using file_id:

var file_id = "AgADAgADrKcxG6Ev4wbe4YsNsXRAbEG9WSoABJyhV2Lvy8uetB4AAgI";
bot.sendPhoto({chat_id: 123456, photo: file_id}, function(err, data){

})

Using file URL:

var file_url = "http://d.ibtimes.co.uk/en/full/1372072/doge.jpg";
bot.sendPhoto({chat_id: 123456, photo: file_url}, function(err, data){

})

Using file path:

var file_path = "/Users/websnipter/Downloads/my_doc.doc";
bot.sendDocument({chat_id: 123456, document: file_path}, function(err, data){

})

Using readStream:

var file_path = "/Users/websnipter/Downloads/video.mp4";
var read_stream = fs.createReadStream(file_path)
bot.sendVideo({chat_id: 123456, video: read_stream}, function(err, data){

})

This methods working for all methods that require upload:

  • sendPhoto
  • sendAudio
  • sendDocument
  • sendSticker
  • sendVideo

Aditional methods

Waiting for new updates. This method the same as getUpdates. But it will be wait for not empty response. If you will set offset param to last update_id value increased by one, this method will be waiting for new income updates.

bot.waitForUpdates({offset: 937477095}, function(err, new_updates){
	
});

Useful links

Contacts

Jaroslav Khorishchenko

websnipter@gmail.com

1.4.0

10 years ago

1.3.0

10 years ago

1.2.1

10 years ago

1.2.0

10 years ago

1.1.0

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago