Git Product home page Git Product logo

telebot's Introduction

The easy way to write Telegram bots.

Build Status Dependency Status Node.js Version TeleBot Group

Library features:

  • ๐ŸŽ Simple. Easy to use.
  • ๐Ÿฐ Full Telegram Bot API support.
  • ๐Ÿ”Œ Supports plugins!
  • ๐Ÿ“ก Build-in modification and event system.
  • ๐Ÿ›  Extendable and hackable.
  • ๐Ÿ”ฎ No callbacks, Promises only.
  • ๐Ÿค“ Readable changelog.
  • โ˜บ๏ธ Friendly TeleBot community group.

๐Ÿ”จ Installation

Download and install via npm package manager (stable):

npm install telebot --save

Or clone fresh code directly from git:

git clone https://github.com/mullwar/telebot.git
cd telebot
npm install

๐Ÿ•น Usage

Import telebot module and create a new bot object:

const TeleBot = require('telebot');

const bot = new TeleBot({
    token: 'TELEGRAM_BOT_TOKEN', // Required. Telegram Bot API token.
    polling: { // Optional. Use polling.
        interval: 1000, // Optional. How often check updates (in ms).
        timeout: 0, // Optional. Update polling timeout (0 - short polling).
        limit: 100, // Optional. Limits the number of updates to be retrieved.
        retryTimeout: 5000, // Optional. Reconnecting timeout (in ms).
        proxy: 'http://username:[email protected]:8080' // Optional. An HTTP proxy to be used (supports Basic Auth).
    },
    webhook: { // Optional. Use webhook instead of polling.
        key: 'key.pem', // Optional. Private key for server.
        cert: 'cert.pem', // Optional. Public key.
        url: 'https://....', // HTTPS url to send updates to.
        host: '0.0.0.0', // Webhook server host.
        port: 443, // Server port.
        maxConnections: 40 // Optional. Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery
    },
    allowedUpdates: [], // Optional. List the types of updates you want your bot to receive. Specify an empty list to receive all updates.
    usePlugins: ['askUser'], // Optional. Use build-in plugins from pluginFolder.
    pluginFolder: '../plugins/', // Optional. Plugin folder location relative to telebot package.
    pluginConfig: { // Optional. Plugin configuration.
        // myPluginName: {
        //   data: 'my custom value'
        // }
    }
});

Or just:

const TeleBot = require('telebot');
const bot = new TeleBot('TELEGRAM_BOT_TOKEN');

Don't forget to insert your Telegram Bot API token key.

To start polling updates, use bot.start().

bot.on('text', (msg) => msg.reply.text(msg.text));

bot.start();

We just created echo bot!

๐ŸŒฑ Quick examples

Send text on /start or /hello command:

bot.on(['/start', '/hello'], (msg) => msg.reply.text('Welcome!'));

When sticker received, reply back:

bot.on('sticker', (msg) => {
    return msg.reply.sticker('http://i.imgur.com/VRYdhuD.png', { asReply: true });
});

Sends photo on "show kitty" or "kitty" text message (using RegExp):

bot.on(/(show\s)?kitty*/, (msg) => {
    return msg.reply.photo('http://thecatapi.com/api/images/get');
});

Command with arguments /say <your message>:

bot.on(/^\/say (.+)$/, (msg, props) => {
    const text = props.match[1];
    return bot.sendMessage(msg.from.id, text, { reply: msg.message_id });
});

When message was edited:

bot.on('edit', (msg) => {
    return msg.reply.text('I saw it! You edited message!', { asReply: true });
});

See more examples!

โฐ Events

Use bot.on(<event>, <function>) to handle all possible TeleBot events.

For example, to catch a command, just add a slash:

bot.on('/hello', (msg) => {
  return bot.sendMessage(msg.from.id, `Hello, ${ msg.from.first_name }!`);
});

Also, you can catch multiple events:

bot.on(['/start', 'audio', 'sticker'], msg => {
  return bot.sendMessage(msg.from.id, 'Bam!');
});

TeleBot events:

  • /* โ€“ any user command
  • /<cmd> โ€“ on specific command
  • start โ€“ bot started
  • stop โ€“ bot stopped
  • reconnecting โ€“ bot reconnecting
  • reconnected โ€“ bot successfully reconnected
  • update - on update
  • tick โ€“ on bot tick
  • error โ€“ an error occurred
  • inlineQuery - inline query data
  • inlineChoice - inline query chosen result
  • callbackQuery - button callback data

Events:

keyboard, button, inlineKeyboard, inlineQueryKeyboard, inlineButton, answerList, getMe, sendMessage, forwardMessage, sendPhoto, sendAudio, sendDocument, sendSticker, sendVideo, sendVoice, sendLocation, sendVenue, sendContact, sendChatAction, getUserProfilePhotos, getFile, kickChatMember, unbanChatMember, answerInlineQuery, answerCallbackQuery, editMessageText, editMessageCaption, editMessageReplyMarkup, setWebhook

Telegram message events:

  • * - any type of message
  • text โ€“ text message
  • audio โ€“ audio file
  • voice โ€“ voice message
  • document โ€“ document file (any kind)
  • photo โ€“ photo
  • sticker โ€“ sticker
  • video โ€“ video file
  • contact โ€“ contact data
  • location โ€“ location data
  • venue โ€“ venue data
  • game - game data
  • edit โ€“ edited message
  • forward โ€“ forwarded message
  • pinnedMessage โ€“ message was pinned
  • userJoined โ€“ new member was added
  • userLeft โ€“ member was removed
  • newTitle โ€“ new chat title
  • newPhoto โ€“ new chat photo
  • deletePhoto โ€“ chat photo was deleted
  • groupCreated โ€“ group has been created
  • channelCreated โ€“ channel has been created
  • supergroupCreated โ€“ supergroup has been created
  • migrateTo โ€“ group has been migrated to a supergroup
  • migrateFrom โ€“ supergroup has been migrated from a group

Read more about Telegram Bot API response types: https://core.telegram.org/bots/api#available-types

๐Ÿšœ Modifiers

You can add modifier to process data before passing it to event.

bot.mod('text', (data) => {
  let msg = data.message;
  msg.text = `๐Ÿ“ข ${ msg.text }`;
  return data;
});

This code adds emoji to every text message.

TeleBot modifiers:

  • property - mod form properties
  • updateList - list of updates in one tick
  • update - every update
  • message - process any type of message
  • <type> - specific type of message (text, voice, document, photo, sticker, video, contact, location or venue)

๐Ÿ”Œ Plugins

Use usePlugins config option to load build-in plugins from pluginFolder:

const bot = new TeleBot({
    token: 'TELEGRAM_BOT_TOKEN',
    usePlugins: ['askUser', 'commandButtons'],
    pluginFolder: '../plugins/',
    pluginConfig: {
        // Plugin configs
    }
});

Or use bot.use(require(<plugin_path>)) to plug an external plugin.

Check out build-in plugin folder!

Plugin structure

module.exports = {
    id: 'myPlugin', // Unique plugin name
    defaultConfig: {
        // Default plugin config
        key: 'value'
    },
    plugin(bot, pluginConfig) {
        // Plugin code
    }
};

โš™๏ธ Methods

TeleBot methods:

on(<events>, <function>)

Handles events.

event(<event>, <data>)

Invokes the event handlers.

mod(<name>, <fn>)

Add data modifier.

modRun(<names>, <data>)

Run data modifiers.

use(<function>)

Use module function.

keyboard([array of arrays], {resize, once, selective})

Creates ReplyKeyboardMarkup keyboard markup object.

button(<location | contact>, <text>)

Creates KeyboardButton button.

inlineButton(<text>, {url | callback | game | inline | inlineCurrent})

Creates InlineKeyboardButton button object.

inlineKeyboard([array of arrays])

Creates inlineKeyboard object for normal bot messages.

answerList(<inline_query_id>, {nextOffset, cacheTime, personal, pmText, pmParameter})

Creates answerInlineQuery answer list object.

inlineQueryKeyboard([array of arrays])

Creates inlineKeyboard object for answerList articles.

start()

Start polling updates.

stop(<message>)

Stop polling updates.

Telegram methods:

TeleBot use standard Telegram Bot API method names.

getMe()

A simple method for testing your bot's auth token.

answerQuery(<answerList>)

Use this method to send answerList to an inline query.

getFile(<file_id>)

Use this method to get basic info about a file and prepare it for downloading.

sendMessage(<chat_id>, <text>, {parse, reply, markup, notify, preview})

Use this method to send text messages.

forwardMessage(<chat_id>, <from_chat_id>, <message_id>, {notify})

Use this method to forward messages of any kind.

sendPhoto(<chat_id>, <file_id | path | url | buffer | stream>, {caption, fileName, serverDownload, reply, markup, notify})

Use this method to send photos.

sendAudio(<chat_id>, <file_id | path | url | buffer | stream>, {title, performer, duration, caption, fileName, serverDownload, reply, markup, notify})

Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message.

sendDocument(<chat_id>, <file_id | path | url | buffer | stream>, {caption, fileName, serverDownload, reply, markup, notify})

Use this method to send general files.

sendSticker(<chat_id>, <file_id | path | url | buffer | stream>, {fileName, serverDownload, reply, markup, notify})

Use this method to send .webp stickers.

sendVideo(<chat_id>, <file_id | path | url | buffer | stream>, {duration, width, height, caption, fileName, serverDownload, reply, markup, notify})

Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document).

sendVoice(<chat_id>, <file_id | path | url | buffer | stream>, {duration, caption, fileName, serverDownload, reply, markup, notify})

Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message.

sendLocation(<chat_id>, [<latitude>, <longitude>], {reply, markup, notify})

Use this method to send point on the map.

sendVenue(<chat_id>, [<latitude>, <longitude>], <title>, <address>, {foursquareId, reply, markup, notify})

Use this method to send information about a venue.

sendContact(<chat_id>, <number>, <firstName>, <lastName>, { reply, markup, notify})

Use this method to send phone contacts.

sendAction(<chat_id>, <action>)

Use this method when you need to tell the user that something is happening on the bot's side.

sendGame(<chat_id>, <game_short_name>, {notify, reply, markup})

Use this method to send a game.

setGameScore(<user_id>, <score>, {force, disableEditMessage, chatId, messageId, inlineMessageId})

Use this method to set the score of the specified user in a game. On success, if the message was sent by the bot, returns the edited Message, otherwise returns True. Returns an error, if the new score is not greater than the user's current score in the chat and force is False.

getGameHighScores(<user_id>, {chatId, messageId, inlineMessageId})

Use this method to get data for high score tables. Will return the score of the specified user and several of his neighbours in a game. On success, returns an Array of GameHighScore objects.

getUserProfilePhotos as getUserPhoto(<user_id>, {offset, limit})

Use this method to get a list of profile pictures for a user.

getFile(<file_id>)

Use this method to get basic info about a file and prepare it for downloading.

getChat(<chat_id>)

Use this method to get up to date information about the chat.

leaveChat(<chat_id>)

Use this method for your bot to leave a group, supergroup or channel.

getChatAdministrators as getAdmins(<chat_id>)

Use this method to get a list of administrators in a chat.

getChatMembersCount as countMembers(<chat_id>)

Use this method to get the number of members in a chat.

getChatMember as getMember(<chat_id>, <user_id>)

Use this method to get information about a member of a chat.

kickChatMember as kick(<chat_id>, <user_id>)

Use this method to kick a user from a group or a supergroup.

unbanChatMember as unban(<chat_id>, <user_id>)

Use this method to unban a previously kicked user in a supergroup.

editMessageText as editText({chatId & messageId | inlineMsgId}, <text>)

Use this method to edit text messages sent by the bot or via the bot (for inline bots).

editMessageCaption as editCaption({chatId & messageId | inlineMsgId}, <caption>)

Use this method to edit captions of messages sent by the bot or via the bot (for inline bots).

editMessageReplyMarkup as editMarkup({chatId & messageId | inlineMsgId}, <markup>)

Use this method to edit only the reply markup of messages sent by the bot or via the bot (for inline bots).

answerCallbackQuery as answerCallback(<callback_query_id>, {text, url, showAlert, cacheTime})

Use this method to send answers to callback queries sent from inline keyboards.

setWebhook(<url>, <certificate>, <allowed_updates>, <max_connections>)

Use this method to specify a url and receive incoming updates via an outgoing webhook.

getWebhookInfo()

Use this method to get current webhook status.

deleteWebhook()

Use this method to remove webhook integration if you decide to switch back to getUpdates. Returns True on success.

telebot's People

Contributors

kosmodrey avatar mullwar avatar flaplim avatar g07cha avatar pyriaxis avatar noplanman avatar 11-a-11 avatar fparaggio avatar rene-demonsters avatar s-a-y avatar shah-smit avatar

Watchers

 avatar James Cloos avatar  avatar

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.