JSPM

telegraf

2.6.4
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 131555
  • Score
    100M100P100Q154938F
  • License MIT

📢 Telegram bot framework

Package Exports

  • telegraf

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (telegraf) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

npm NPM Version node Build Status David js-standard-style

📢 Telegram bot framework for Node.js

Features

Installation

$ npm install telegraf

Examples

const Telegraf = require('telegraf')

const app = new Telegraf(process.env.BOT_TOKEN)
app.on('message', (ctx) => ctx.reply('42'))
app.startPolling()

There are some other examples.

API

Application

A Telegraf application is an object containing an array of middlewares which are composed and executed in a stack-like manner upon request. Is similar to many other middleware systems that you may have encountered such as Koa, Ruby's Rack, Connect.

Context

A Telegraf Context encapsulates telegram message. Context is created per request and contains following props:

app.on('message', (ctx) => {
  ctx.telegram           // Telegram wrapper instance
  ctx.updateType         // Update type(message, inline_query, etc.)
  [ctx.message]          // Received message
  [ctx.inlineQuery]      // Received inline query
  [...]                  // See api.doc
})

Context API docs

Cascading

Middleware normally takes two parameters (ctx, next), ctx is the context for one Telegram message, next is a function that is invoked to execute the downstream middleware. It returns a Promise with a then function for running code after completion.

const app = new Telegraf(process.env.BOT_TOKEN)

app.use((ctx, next) => {
  const start = new Date()
  return next().then(() => {
    const ms = new Date() - start
    console.log('Response time %sms', ms)
  })
})

app.on('text', (ctx) => ctx.reply('Hello World'))

Cascading with async functions (Babel required)

app.use(async (ctx, next) => {
  const start = new Date()
  await next()
  const ms = new Date() - start
  console.log('Response time %sms', ms)
})

Known middleware

State

The recommended namespace to share information between middlewares.

const app = new Telegraf(process.env.BOT_TOKEN)

app.use((ctx, next) => {
  ctx.state.role = getUserRole(ctx.message) 
  return next()
})

app.on('text', (ctx) => {
  return ctx.reply(`Hello ${ctx.state.role}`)
})

Session

const app = new Telegraf(process.env.BOT_TOKEN)
app.use(Telegraf.memorySession())
app.on('text', (ctx) => {
  ctx.session.counter = ctx.session.counter || 0
  ctx.session.counter++
  return ctx.reply(`Message counter:${ctx.session.counter}`)
})

Note: For persistent sessions you can use any of telegraf-session-* middleware.

Error Handling

By default Telegraf will print all errors to stderr and rethrow error. To perform custom error-handling logic you can set onError handler:

telegraf.onError = (err) => {
  log.error('Ooops', err)
  throw err
}

Developer docs

Telegraf API docs

License

The MIT License (MIT)

Copyright (c) 2016 Telegraf

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.