Giter Site home page Giter Site logo

api-telegram-bot's Introduction

I'm not maintaining this package anymore


Telegram Bot API for Node.js

Node.js module for Telegram Bot API.

Talk with @botfather on Telegram to create your bot.

This module is updated with Telegram API version 4.2. Except with Telegram Passport.

API reference

Click Here

Older Versions

Examples

There's a examples directory on GitHub. Take a look ;)

Before run any example code, please install deps with npm i and run npm run build to transpile TS code to JS in dist folder.

News

  • Removed experimental feature SmartMenu
  • Implemented menus builded with js generators
  • Changes Polling
    • Moved .startPolling() from Bot instance to Polling

Install

npm install api-telegram-bot

Start coding

const { Bot, Polling } = require('api-telegram-bot');
// or using commonjs imports
// const { Bot } = require("api-telegram-bot");

const TOKEN = "BOT_TOKEN";
const bot = new Bot(TOKEN);
// polling constructor accepts some options as second parameter, see docs
const polling = new Polling(bot);

// subscribe to all message types (texts, photos, videos, and others)
bot.messages().subscribe(data => {
  // data is a object with 2 props:
  //   data.update - is the update received,
  //   data.actions - actions object with some helper functions
  // log to see what it is:
  console.log(data);
});

// subscribe only to text messages
bot.messages('text').subscribe(
  // my opinion: use object destructuring allows a more beautiful code
  ({ update, actions }) => {

    /*
     * actions is an object with some functions to manipulate received message:
     *    banChatMember: (until: number) => Promise
     *    deleteMessage: () => Promise
     *    reply: (text: string, optionals?) => Promise
     * 
     * note: deleteMessage and banChatMember doesn't works on private chats
     */
    actions.reply(update.message.text);

    setTimeout(() => {
      bot.polling.stopPolling() // stopPolling() returns a promise fulfilled when polling ends (v5.2 or newer) (see docs for details)
        .then(() => console.log('polling stopped'));
    }, 30000)
  }
);

// NOTE: message actions are provided only for message updates (text, photo, ...)
bot.messages('edited_messages').subscribe(data => {
  // no actions here
  console.log(data);
});

Enable debug log

Start your application with DEBUG env variable containing 'api-telegram-bot' value. Reference to debug package

$ DEBUG=api-telegram-bot npm start

To see debug logs from webhook or polling:

$ DEBUG=api-telegram-bot:polling npm start
$ DEBUG=api-telegram-bot:webhook npm start

Reply Markup Builders

const { KeyboardBuilder } = require('api-telegram-bot');

const TOKEN = 'BOT_TOKEN';
const CONTACT_ID = 'CONTACT_ID';
const bot = new Bot(TOKEN);

const { keyboard } = KeyboardBuilder()
  .button({ text: "Yes" })
  .button({ text: "No" })
  .newRow()
  .button('Cancel');

bot.sendMessage(CONTACT_ID, "Confirm?", { reply_markup: { keyboard, resize_keyboard: true } });

Reply Keyboard Builder Result

const { KeyboardBuilder, Bot } = require('api-telegram-bot');

const TOKEN = 'BOT_TOKEN';
const CONTACT_ID = 'YOUR_TELEGRAM_ID';

const bot = new Bot(TOKEN);

const inline_keyboard = KeyboardBuilder()
  .newRow()
    .button({ text: "Yes", callback_data: "YES" })
    .button({ text: "No", callback_data: "NO" })
  .newRow()
    .button({ text: "Cancel", callback_data: "CANCEL" })
  .keyboard;

bot.sendMessage(CONTACT_ID, "Confirm?", { reply_markup: { inline_keyboard } });

See the message sent by code above:

Inline Keyboard Builder Result

api-telegram-bot's People

Contributors

felipebergamin avatar

Stargazers

 avatar

Watchers

 avatar  avatar

api-telegram-bot's Issues

Proxy option

In my country telegram website, including api, is blocked by government, so I have to use proxy. It would be nice to have such option only for bot without having to do it globally for request module.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.