# node-tg-bot

> Node Telegram Bot Api Interface

Latest version **2.0.0** (published 2017-03-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-tg-bot
pnpm add node-tg-bot
yarn add node-tg-bot
bun add node-tg-bot
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2017-03-03 |
| First published | 2015-08-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | razorxan |
| Maintainers | razorxan |
| Keywords | node, telegram, bot |

## Links

- npm: https://www.npmjs.com/package/node-tg-bot
- Repository: https://github.com/razorxan/node-tg-bot
- Homepage: https://github.com/razorxan/node-tg-bot#readme
- Issues: https://github.com/razorxan/node-tg-bot/issues
- npm.io page: https://npm.io/package/node-tg-bot

## Dependencies (2)

- [mime](https://npm.io/package/mime.md) ^1.3.4
- [request](https://npm.io/package/request.md) ^2.71.0

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2017-03-03
- 1.9.4 — 2016-04-21
- 1.9.3 — 2016-04-20
- 1.9.2 — 2016-04-19
- 1.9.1 — 2016-04-19
- 1.9.0 — 2016-04-19
- 1.8.2 — 2016-04-19
- 1.8.1 — 2016-04-19
- 1.8.0 — 2016-04-15
- 1.3.4 — 2016-04-05
- 1.3.3 — 2016-04-04
- 1.3.2 — 2016-04-04
- 1.3.1 — 2016-01-10
- 1.1.21 — 2016-01-10
- 1.1.20 — 2015-11-22
- … 20 more at https://npm.io/package/node-tg-bot/versions

## README

# node-tg-bot

node-tg-bot is a node module for implementing Telegram bots in Nodejs using Telegram Bot Api 2.3.1

## Installing

```js
npm install node-tg-bot --save
```

## Getting Started

```js
const Telegram = require('node-tg-bot');
```

Then to create a bot (the polling way)

```js
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](https://core.telegram.org/bots/self-signed) to create your self signed ssl cert/key)

```js
var bot = new TelegramBot(token, {
    webHook: {
        key: 'your_private.key',
        cert: 'your_public.pem'
    }
});
```

Then if you are using a webhook

```js
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

```js
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

```js
/*
    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

```js
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

```js
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

```js
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](https://core.telegram.org/bots/api#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

```js
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](#anchors-in-markdown)
* ```.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

- Refer to https://core.telegram.org/bots/api#update

#### Message

##### Properties

- Refer to https://core.telegram.org/bots/api#message

##### Methods

- ```message.forwardTo(recipient_id)```
- ```message.editText(text[, options)```
- ```message.editCaption(options)```
- ```message.editReplyMarkup(options)```
- ```message.getType(void)```

#### User

##### Properties

- Refer to https://core.telegram.org/bots/api#user

##### 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

- Refer to https://core.telegram.org/bots/api#chat

##### 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

---
_Source: https://npm.io/package/node-tg-bot · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
