Giter Site home page Giter Site logo

node-telegram-bot-api's Introduction

Build Status Build status Coverage Status bitHound Score

Node.js module to interact with official Telegram Bot API. A bot token is needed, to obtain one, talk to @botfather and create a new bot.

npm install node-telegram-bot-api
var TelegramBot = require('node-telegram-bot-api');

var token = 'YOUR_TELEGRAM_BOT_TOKEN';
// Setup polling way
var bot = new TelegramBot(token, {polling: true});
bot.on('text', function (msg) {
  var chatId = msg.chat.id;
  // photo can be: a file path, a stream or a Telegram file_id
  var photo = 'cats.png';
  bot.sendPhoto(chatId, photo, {caption: 'Lovely kittens'});
});

There are some other examples on examples.

Events

Every time TelegramBot receives a message, it emits a message. Depending on which message was received, emits an event from this ones: text, audio, document, photo, sticker, video, voice, contact, location, new_chat_participant, left_chat_participant, new_chat_title, new_chat_photo, delete_chat_photo, group_chat_created. Its much better to listen a specific event rather than a message in order to stay safe from the content.


TelegramBot

Both request method to obtain messages are implemented. To use standard polling, set polling: true on options. Notice that webHook will need a valid SSL certificate (self-signed certificates are allowed since August 29, 2015). Emits message when a message arrives.

See: https://core.telegram.org/bots/api

Params:

  • String token Bot Token
  • Object [options]
  • Boolean|Object [options.polling=false] Set true to enable polling or set options
  • String|Number [options.polling.timeout=4] Polling time
  • String|Number [options.polling.interval=2000] Interval between requests in miliseconds
  • Boolean|Object [options.webHook=false] Set true to enable WebHook or set options
  • String [options.webHook.key] PEM private key to webHook server
  • String [options.webHook.cert] PEM certificate key to webHook server

getMe()

Returns basic information about the bot in form of a User object.

See: https://core.telegram.org/bots/api#getme

Return:

  • Promise

setWebHook(url)

Specify an url to receive incoming updates via an outgoing webHook.

See: https://core.telegram.org/bots/api#setwebhook

Params:

  • String url URL where Telegram will make HTTP Post. Leave empty to delete webHook.

getUpdates([timeout], [limit], [offset])

Use this method to receive incoming updates using long polling

See: https://core.telegram.org/bots/api#getupdates

Params:

  • Number|String [timeout] Timeout in seconds for long polling.
  • Number|String [limit] Limits the number of updates to be retrieved.
  • Number|String [offset] Identifier of the first update to be returned.

Return:

  • Promise Updates

sendMessage(chatId, text, [options])

Send text message.

See: https://core.telegram.org/bots/api#sendmessage

Params:

  • Number|String chatId Unique identifier for the message recipient
  • String text Text of the message to be sent
  • Object [options] Additional Telegram query options

Return:

  • Promise

forwardMessage(chatId, fromChatId, messageId)

Forward messages of any kind.

Params:

  • Number|String chatId Unique identifier for the message recipient
  • Number|String fromChatId Unique identifier for the chat where the original message was sent
  • Number|String messageId Unique message identifier

Return:

  • Promise

sendPhoto(chatId, photo, [options])

Send photo

See: https://core.telegram.org/bots/api#sendphoto

Params:

  • Number|String chatId Unique identifier for the message recipient
  • String|stream.Stream photo A file path or a Stream. Can also be a file_id previously uploaded
  • Object [options] Additional Telegram query options

Return:

  • Promise

sendAudio(chatId, audio, [options])

Send audio

See: https://core.telegram.org/bots/api#sendaudio

Params:

  • Number|String chatId Unique identifier for the message recipient
  • String|stream.Stream audio A file path or a Stream. Can also be a file_id previously uploaded.
  • Object [options] Additional Telegram query options

Return:

  • Promise

sendDocument(chatId, A, [options])

Send Document

See: https://core.telegram.org/bots/api#sendDocument

Params:

  • Number|String chatId Unique identifier for the message recipient
  • String|stream.Stream A file path or a Stream. Can also be a file_id previously uploaded.
  • Object [options] Additional Telegram query options

Return:

  • Promise

sendSticker(chatId, A, [options])

Send .webp stickers.

See: https://core.telegram.org/bots/api#sendsticker

Params:

  • Number|String chatId Unique identifier for the message recipient
  • String|stream.Stream A file path or a Stream. Can also be a file_id previously uploaded.
  • Object [options] Additional Telegram query options

Return:

  • Promise

sendVideo(chatId, A, [options])

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

See: https://core.telegram.org/bots/api#sendvideo

Params:

  • Number|String chatId Unique identifier for the message recipient
  • String|stream.Stream A file path or a Stream. Can also be a file_id previously uploaded.
  • Object [options] Additional Telegram query options

Return:

  • Promise

sendVoice(chatId, A, [options])

Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Audio or Document).

See: https://core.telegram.org/bots/api#sendvoice

Params:

  • Number|String chatId Unique identifier for the message recipient
  • String|stream.Stream A file path or a Stream. Can also be a file_id previously uploaded.
  • Object [options] Additional Telegram query options

Return:

  • Promise

sendChatAction(chatId, action)

Send chat action. typing for text messages, upload_photo for photos, record_video or upload_video for videos, record_audio or upload_audio for audio files, upload_document for general files, find_location for location data.

See: https://core.telegram.org/bots/api#sendchataction

Params:

  • Number|String chatId Unique identifier for the message recipient
  • String action Type of action to broadcast.

Return:

  • Promise

getUserProfilePhotos(userId, [offset], [limit])

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

See: https://core.telegram.org/bots/api#getuserprofilephotos

Params:

  • Number|String userId Unique identifier of the target user
  • Number [offset] Sequential number of the first photo to be returned. By default, all photos are returned.
  • Number [limit] Limits the number of photos to be retrieved. Values between 1โ€”100 are accepted. Defaults to 100.

Return:

  • Promise

sendLocation(chatId, latitude, longitude, [options])

Send location. Use this method to send point on the map.

See: https://core.telegram.org/bots/api#sendlocation

Params:

  • Number|String chatId Unique identifier for the message recipient
  • Float latitude Latitude of location
  • Float longitude Longitude of location
  • Object [options] Additional Telegram query options

Return:

  • Promise

getFile(fileId)

Get file. Use this method to get basic info about a file and prepare it for downloading. Attention: link will be valid for 1 hour.

See: https://core.telegram.org/bots/api#getfile

Params:

  • String fileId File identifier to get info about

Return:

  • Promise

node-telegram-bot-api's People

Contributors

yagop avatar illyism avatar waterloo avatar feverqwe avatar palmerabollo avatar universome avatar lopezjurip avatar sebastiantroc avatar

Watchers

Evgeny Tevelevich 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.