telegram-bot-ts
v0.0.6
Published
Typescript, Node.js module for Telegram Bot API (https://core.telegram.org/bots/api)
Downloads
8
Readme
Typescript, Node.js module for Telegram Bot API (https://core.telegram.org/bots/api)
Install
npm install telegram-bot-ts
or
yarn add telegram-bot-ts
Examples
Answer on text
import { TelegramBot, Message, SendMessageParam } from 'telegram-bot-ts';
let bot = new TelegramBot(<Your bot token here>);
bot.onText(async function (message: Message) {
bot.sendMessage({
chatId: message.chat.id,
text: message.text,
replyToMessageId: message.messageId,
});
});
bot.startLongPoll();
Receive callback from inline keyboard
bot.onCallback(async function(callback: CallbackQuery) {
bot.sendMessage({
chatId: callback.chat.id,
text: callback.data,
});
});
Send keyboard
bot.sendMessage({
chatId: <chat id>,
text: <message text>,,
replyMarkup: new ReplyKeyboardMarkup({
keyboard: [[new KeyboardButton({ text: 'a' })]]
})
});
Remove keyboard
bot.sendMessage({
chatId: <chat id>,
text: <message text>,
replyMarkup: new ReplyKeyboardRemove({})
});
Send inline keyboard
You must use exactly one of the optional fields in InlineKeyboardButton.
bot.sendMessage({
chatId: <chat id>,
text: <message text>,,
replyMarkup: new InlineKeyboardMarkup({
inlineKeyboard: [
[new InlineKeyboardButton({ text: 'google', url: 'https://google.com' })],
]
})
});
Send Animation
You can set animation as InputFile or String (url or file_id) https://core.telegram.org/bots/api#sendanimation
bot.sendAnimation({
chatId: <chat id>,
animation: new InputFile('name.mp4', fs.readFileSync('name.mp4')),
});
bot.sendAnimation({
chatId: <chat id>,
animation: "some URL or File ID",
});
Send Video
You can set animation as InputFile or String (url or file_id) https://core.telegram.org/bots/api#sendvideo
bot.sendVideo({
chatId: <chat id>,
video: new InputFile('name.mp4', fs.readFileSync('name.mp4')),
});
bot.sendVideo({
chatId: <chat id>,
video: "some URL or File ID",
});
Edit Message Text
bot.editMessageText({
text: 'new Text',
chatId: <chat Id>,
messageId: <message id>
});
Edit Inline Keyboard
bot.editMessageReplyMarkup({
chatId: <chat Id>,
messageId: <message id>,
replyMarkup: new InlineKeyboardMarkup({
inlineKeyboard: [
[new InlineKeyboardButton({ text: 'text a', callbackData: 'callback_a' }), new InlineKeyboardButton({ text: 'text_b', callbackData: 'callback_b' })],
[new InlineKeyboardButton({ text: 'text_c', callbackData: 'callback_c' })]
],
}),
});