0.23.4 • Published 8 years ago

mironov-node-telegram-bot-api v0.23.4

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

Build Status Build status Coverage Status bitHound Score https://telegram.me/node_telegram_bot_api(https://img.shields.io/badge/💬 Telegram-nodetelegrambot__api-blue.svg) https://telegram.me/Yago_Perez(https://img.shields.io/badge/💬 Telegram-Yago__Perez-blue.svg)

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});

// Matches /echo [whatever]
bot.onText(/\/echo (.+)/, function (msg, match) {
  var fromId = msg.from.id;
  var resp = match[1];
  bot.sendMessage(fromId, resp);
});

// Any kind of message
bot.on('message', 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 emits inline_query when receives an Inline Query and chosen_inline_result when receives a ChosenInlineResult. Bot must be enabled on inline mode


WebHooks

Telegram only supports HTTPS connections to WebHooks, in order to set a WebHook a private key file and public certificate must be used. Since August 29, 2015 Telegram supports self signed ones, to generate them:

# Our private cert will be key.pem, keep in private this file.
openssl genrsa -out key.pem 2048
# Our public certificate will be crt.pem
openssl req -new -sha256 -key key.pem -out crt.pem

Once they are generated, the crt.pem can be provided to telegramBot.setWebHook(url, crt) as crt.

API Reference

TelegramBot

TelegramBot

Kind: global class
See: https://core.telegram.org/bots/api

new TelegramBot(token, options)

Both request method to obtain messages are implemented. To use standard polling, set polling: true on options. Notice that webHook will need a SSL certificate. Emits message when a message arrives.

ParamTypeDefaultDescription
tokenStringBot Token
optionsObject
options.pollingBoolean | ObjectfalseSet true to enable polling or set options
options.polling.timeoutString | Number10Polling time in seconds
options.polling.intervalString | Number2000Interval between requests in miliseconds
options.webHookBoolean | ObjectfalseSet true to enable WebHook or set options
options.webHook.keyStringPEM private key to webHook server.
options.webHook.certStringPEM certificate (public) to webHook server.

telegramBot.stopPolling() ⇒ Promise

Stops polling after the last polling request resolves

Kind: instance method of TelegramBot
Returns: Promise - promise Promise, of last polling request

telegramBot.getMe() ⇒ Promise

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

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#getme

telegramBot.setWebHook(url, cert)

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

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#setwebhook

ParamTypeDescription
urlStringURL where Telegram will make HTTP Post. Leave empty to delete webHook.
certString | stream.StreamPEM certificate key (public).

telegramBot.getUpdates(timeout, limit, offset) ⇒ Promise

Use this method to receive incoming updates using long polling

Kind: instance method of TelegramBot
Returns: Promise - Updates
See: https://core.telegram.org/bots/api#getupdates

ParamTypeDescription
timeoutNumber | StringTimeout in seconds for long polling.
limitNumber | StringLimits the number of updates to be retrieved.
offsetNumber | StringIdentifier of the first update to be returned.

telegramBot.sendMessage(chatId, text, options) ⇒ Promise

Send text message.

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#sendmessage

ParamTypeDescription
chatIdNumber | StringUnique identifier for the message recipient
textStringText of the message to be sent
optionsObjectAdditional Telegram query options

telegramBot.answerInlineQuery(inlineQueryId, results, options) ⇒ Promise

Send answers to an inline query.

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#answerinlinequery

ParamTypeDescription
inlineQueryIdStringUnique identifier of the query
resultsArray.<InlineQueryResult>An array of results for the inline query
optionsObjectAdditional Telegram query options

telegramBot.forwardMessage(chatId, fromChatId, messageId) ⇒ Promise

Forward messages of any kind.

Kind: instance method of TelegramBot

ParamTypeDescription
chatIdNumber | StringUnique identifier for the message recipient
fromChatIdNumber | StringUnique identifier for the chat where the original message was sent
messageIdNumber | StringUnique message identifier

telegramBot.sendPhoto(chatId, photo, options) ⇒ Promise

Send photo

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#sendphoto

ParamTypeDescription
chatIdNumber | StringUnique identifier for the message recipient
photoString | stream.Stream | BufferA file path or a Stream. Can also be a file_id previously uploaded
optionsObjectAdditional Telegram query options

telegramBot.sendAudio(chatId, audio, options) ⇒ Promise

Send audio

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#sendaudio

ParamTypeDescription
chatIdNumber | StringUnique identifier for the message recipient
audioString | stream.Stream | BufferA file path, Stream or Buffer. Can also be a file_id previously uploaded.
optionsObjectAdditional Telegram query options

telegramBot.sendDocument(chatId, doc, options, fileOpts) ⇒ Promise

Send Document

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#sendDocument

ParamTypeDescription
chatIdNumber | StringUnique identifier for the message recipient
docString | stream.Stream | BufferA file path, Stream or Buffer. Can also be a file_id previously uploaded.
optionsObjectAdditional Telegram query options
fileOptsObjectOptional file related meta-data

telegramBot.sendSticker(chatId, sticker, options) ⇒ Promise

Send .webp stickers.

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#sendsticker

ParamTypeDescription
chatIdNumber | StringUnique identifier for the message recipient
stickerString | stream.Stream | BufferA file path, Stream or Buffer. Can also be a file_id previously uploaded. Stickers are WebP format files.
optionsObjectAdditional Telegram query options

telegramBot.sendVideo(chatId, video, options) ⇒ Promise

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

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#sendvideo

ParamTypeDescription
chatIdNumber | StringUnique identifier for the message recipient
videoString | stream.Stream | BufferA file path or Stream. Can also be a file_id previously uploaded.
optionsObjectAdditional Telegram query options

telegramBot.sendVoice(chatId, voice, options) ⇒ Promise

Send voice

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#sendvoice

ParamTypeDescription
chatIdNumber | StringUnique identifier for the message recipient
voiceString | stream.Stream | BufferA file path, Stream or Buffer. Can also be a file_id previously uploaded.
optionsObjectAdditional Telegram query options

telegramBot.sendChatAction(chatId, action) ⇒ Promise

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.

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#sendchataction

ParamTypeDescription
chatIdNumber | StringUnique identifier for the message recipient
actionStringType of action to broadcast.

telegramBot.kickChatMember(chatId, userId) ⇒ Promise

Use this method to kick a user from a group or a supergroup. In the case of supergroups, the user will not be able to return to the group on their own using invite links, etc., unless unbanned first. The bot must be an administrator in the group for this to work. Returns True on success.

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#kickchatmember

ParamTypeDescription
chatIdNumber | StringUnique identifier for the target group or username of the target supergroup
userIdStringUnique identifier of the target user

telegramBot.unbanChatMember(chatId, userId) ⇒ Promise

Use this method to unban a previously kicked user in a supergroup. The user will not return to the group automatically, but will be able to join via link, etc. The bot must be an administrator in the group for this to work. Returns True on success.

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#unbanchatmember

ParamTypeDescription
chatIdNumber | StringUnique identifier for the target group or username of the target supergroup
userIdStringUnique identifier of the target user

telegramBot.answerCallbackQuery(callbackQueryId, text, showAlert, options) ⇒ Promise

Use this method to send answers to callback queries sent from inline keyboards. The answer will be displayed to the user as a notification at the top of the chat screen or as an alert. On success, True is returned.

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#answercallbackquery

ParamTypeDescription
callbackQueryIdNumber | StringUnique identifier for the query to be answered
textStringText of the notification. If not specified, nothing will be shown to the user
showAlertBooleanWhether to show an alert or a notification at the top of the screen
optionsObjectAdditional Telegram query options

telegramBot.editMessageText(text, options) ⇒ Promise

Use this method to edit text messages sent by the bot or via the bot (for inline bots). On success, the edited Message is returned.

Note that you must provide one of chat_id, message_id, or inline_message_id in your request.

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#editmessagetext

ParamTypeDescription
textStringNew text of the message
optionsObjectAdditional Telegram query options (provide either one of chat_id, message_id, or inline_message_id here)

telegramBot.editMessageCaption(caption, options) ⇒ Promise

Use this method to edit captions of messages sent by the bot or via the bot (for inline bots). On success, the edited Message is returned.

Note that you must provide one of chat_id, message_id, or inline_message_id in your request.

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#editmessagecaption

ParamTypeDescription
captionStringNew caption of the message
optionsObjectAdditional Telegram query options (provide either one of chat_id, message_id, or inline_message_id here)

telegramBot.editMessageReplyMarkup(replyMarkup, options) ⇒ Promise

Use this method to edit only the reply markup of messages sent by the bot or via the bot (for inline bots). On success, the edited Message is returned.

Note that you must provide one of chat_id, message_id, or inline_message_id in your request.

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#editmessagetext

ParamTypeDescription
replyMarkupObjectA JSON-serialized object for an inline keyboard.
optionsObjectAdditional Telegram query options (provide either one of chat_id, message_id, or inline_message_id here)

telegramBot.getUserProfilePhotos(userId, offset, limit) ⇒ Promise

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

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#getuserprofilephotos

ParamTypeDescription
userIdNumber | StringUnique identifier of the target user
offsetNumberSequential number of the first photo to be returned. By default, all photos are returned.
limitNumberLimits the number of photos to be retrieved. Values between 1—100 are accepted. Defaults to 100.

telegramBot.sendLocation(chatId, latitude, longitude, options) ⇒ Promise

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

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#sendlocation

ParamTypeDescription
chatIdNumber | StringUnique identifier for the message recipient
latitudeFloatLatitude of location
longitudeFloatLongitude of location
optionsObjectAdditional Telegram query options

telegramBot.getFile(fileId) ⇒ Promise

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.

Kind: instance method of TelegramBot
See: https://core.telegram.org/bots/api#getfile

ParamTypeDescription
fileIdStringFile identifier to get info about

telegramBot.getFileLink(fileId) ⇒ Promise

Get link for file. Use this method to get link for file for subsequent use. Attention: link will be valid for 1 hour.

This method is a sugar extension of the (getFile)#getfilefileid method, which returns just path to file on remote server (you will have to manually build full uri after that).

Kind: instance method of TelegramBot
Returns: Promise - promise Promise which will have fileURI in resolve callback
See: https://core.telegram.org/bots/api#getfile

ParamTypeDescription
fileIdStringFile identifier to get info about

telegramBot.downloadFile(fileId, downloadDir) ⇒ Promise

Downloads file in the specified folder. This is just a sugar for (getFile)#getfilefiled method

Kind: instance method of TelegramBot
Returns: Promise - promise Promise, which will have filePath of downloaded file in resolve callback

ParamTypeDescription
fileIdStringFile identifier to get info about
downloadDirStringAbsolute path to the folder in which file will be saved

telegramBot.onText(regexp, callback)

Register a RegExp to test against an incomming text message.

Kind: instance method of TelegramBot

ParamTypeDescription
regexpRegExpRegExp to be executed with exec.
callbackfunctionCallback will be called with 2 parameters, the msg and the result of executing regexp.exec on message text.

telegramBot.onReplyToMessage(chatId, messageId, callback)

Register a reply to wait for a message response.

Kind: instance method of TelegramBot

ParamTypeDescription
chatIdNumber | StringThe chat id where the message cames from.
messageIdNumber | StringThe message id to be replied.
callbackfunctionCallback will be called with the reply message.