Node.js client for the Telegram messenger Bot API
node.js client for the Telegram messenger Bot API
Install via npm:
$ npm install telegram-bot-client --save
Important: Please note that this client does not and will not support automated polling or listening to webhooks. This can be done much better by the application running the bot. If you are interested in how this might look like you can have a look at telegram-bot-boilerplate.
Instantiate a new client using your bot’s token:
var TelegramBotClient = require('telegram-bot-client');
var client = new TelegramBotClient(MY_TOKEN);
client.sendMessage(CHAT_ID, 'I\'m a bot, so what?');
All methods on the client are chainable and will wait for the previous operations to finish:
client
.sendMessage(CHAT_ID, 'Hi!')
.sendMessage(CHAT_ID, 'How are you?')
.sendMessage(CHAT_ID, 'Be right back!')
.delay(25000)
.sendMessage(CHAT_ID, 'Back!')
.sendMessage(CHAT_ID, 'Wait, I want to show you something, where is it?')
.delay(7500)
.sendPhoto(CHAT_ID, SOME_URL_POINTING_TO_A_PHOTO)
.sendMessage(CHAT_ID, 'How do you like it?')
.catch(function(err){
// all errors ocurring in the above call chain
// will be passed down to this handler
console.log(err);
});
If you want to consume the response you can unwrap the chain using the .promise()
method:
client
.sendMessage(CHAT_ID, 'Did this really work?')
.promise()
.then(function(response){
console.log(response);
}, function(err){
console.log(err);
});
All methods are following the same convention: Required arguments are passed seperately, all optional parameters can be wrapped into an object and be supplied as the method’s last argument:
var messageText = 'Look at this: https://www.youtube.com/watch?v=qb_hqexKkw8';
var opts = { disable_web_page_preview: true };
client.sendMessage(CHAT_ID, messageText, opts);
All methods described by the API docs are present on the client.
#getMe()
gets info on the bot
#getUpdates([options])
gets updates (messages) sent to the bot
#getUserProfilePhotos(userId[, options])
gets a user’s profile photos
#setWebhook(url)
sets or removes the webhook url to use
''
#sendMessage(chatId, text[, options])
sends a message
#forwardMessage(chatId, fromChatId, messageId)
forwards a message
#sendPhoto(chatId, photoReference[, options])
sends a photo
#sendAudio(chatId, audioReference[, options])
sends audio
#sendVoice(chatId, voiceReference[, options])
sends audio
#sendDocument(chatId, documentReference[, options])
sends a document
#sendSticker(chatId, stickerReference[, options])
sends a sticker
#sendVideo(chatId, videoReference[, options])
sends video
#sendLocation(chatId, lat, lon[, options])
sends a location
#sendVenue(chatId, lat, lon, title, address[, options])
sends a venue
#sendContact(chatId, phoneNumber, firstName[, options])
sends a contact
#sendChatAction(chatId, action)
sends a chat action
#kickChatMember(chatId, userId)
kicks a user from a chat
#unbanChatMember(chatId, userId)
unbans a previously kicked user
#answerCallbackQuery(callbackQueryId[, options])
answer callback queries sent by inline keyboards
#editMessageText([chatId,] identifier, text[, options])
edit a message’s text
#editMessageCaption([chatId,] identifier[, options])
edit a message’s caption
#editMessageReplyMarkup([chatId,] identifier[, options])
edit a message’s reply markup
#answerInlineQuery(inlineQueryId, results[, options])
answer an inline query
#getChat(chatId)
get information on a chat
#getChatAdministrators(chatId)
get the administrators of a chat
#getChatMembersCount(chatId)
get the number of members of a chat
#getChatMember(chatId, memberId)
get information on a member of a chat
#leaveChat(chatId)
leave a chat
#delay(duration)
pauses the queue for the give duration
#promise()
unwraps the chain and returns a promise for the last operation
#then(successHandler, errorHandler)
exposes the last operation’s result via a promise interface
#catch(handler)
handles errors occured in the chain
MIT © Frederik Ring