2.2.6 • Published 6 years ago

tgapi v2.2.6

Weekly downloads
47
License
MIT
Repository
github
Last release
6 years ago

tgapi

Build Status Greenkeeper badge

Actual Telegram bot API JS implementation

Installation

  • npm: npm install --save tgapi
  • yarn: yarn add tgapi

Usage

Calling bot API methods

In order to send requests to API, we need to create the Bot instance. The Bot is an object that contains all bot API methods. Each bot API method returns a promise, on successful execution of which the result will be the response object of the API server described in the documentation.

In this example, we call getMe and sendPhoto methods:

const { Bot } = require('tgapi')
const fs = require('fs')

const bot = new Bot('<your bot token>')

bot
  .getMe()
  .then(console.log)

  // { ok: true,
  //   result: {
  //     id: 12345,
  //     is_bot: true,
  //     first_name: "My awesome bot",
  //     username: "myawesomebot" } }

const chat_id = 100500
const photo = fs.createReadStream('./path/to/photo.jpg')

bot
  .sendPhoto({ chat_id, photo })
  .then(console.log)

  // { ok: true, result: { Message object } }

bot
  .sendPhoto({
    chat_id,
    photo: fs.createReadStream('./path/to/photo.jpg'),
  })
  .then(console.log)

  // { ok: true, result: { Message object } }

Etc. for all other methods.

Serializable method parameters

Telegram requires serializing some parameters of methods. You do not need to do this, because this is done automatically.

Working with updates

As EventEmitter

// All options are optioal
const polling = bot.polling({
  limit: 50,
  timeout: 60,
  allowedUpdates: [ ... ],
})

polling.on('update', update => { ... })
polling.on('message', message => { ... })
polling.on('error', error => { ... })

As Observable stream

const { from } = require('most')

const updates$ = from(polling)

const messages$ = updates$
  .map(update => update.message)
  .filter(Boolean)
2.2.6

6 years ago

2.2.5

6 years ago

2.2.3

6 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.4.0

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago