Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
telegram-webhook-js
Advanced tools
This library provides a simplified interface to interact with the Telegram Bot API, enabling you to create, manage, and operate a Telegram bot. It includes methods to send messages, edit messages, handle updates, send various types of media, and manage ca
This library provides a simplified interface to interact with the Telegram Bot API, enabling you to create, manage, and operate a Telegram bot. It includes methods to send messages, edit messages, handle updates, send various types of media, and manage callback queries for inline buttons.
To install this library, you need to have Node.js and npm installed. Then, you can add this library to your project:
npm install telegram-webhook-js
First, import the TelegramBot
class:
import TelegramBot from 'telegram-webhook-js';
Create an instance of the TelegramBot
class by passing your bot token:
const bot = new TelegramBot('YOUR_BOT_TOKEN_HERE');
To receive updates via webhook, set the webhook URL:
bot.setWebhook('https://your-webhook-url.com');
To delete the webhook:
bot.deleteWebhook();
To get updates from Telegram servers:
bot.getUpdates().then(updates => {
updates.forEach(update => {
bot.handleUpdate(update);
});
});
Send a text message to a chat:
bot.sendMessage(chatId, 'Hello, world!', {
parseMode: 'Markdown',
disableWebPagePreview: true,
disableNotification: false,
replyToMessageId: someMessageId,
replyMarkup: someReplyMarkup,
});
Edit an existing message:
bot.editMessageText(chatId, messageId, 'Edited message text', {
parseMode: 'HTML',
disableWebPagePreview: true,
replyMarkup: someReplyMarkup,
});
Delete a message:
bot.deleteMessage(chatId, messageId);
Send a photo:
bot.sendPhoto(chatId, photoFile, {
caption: 'Photo caption',
});
Send a document:
bot.sendDocument(chatId, documentFile, {
caption: 'Document caption',
});
Send a sticker:
bot.sendSticker(chatId, stickerFile);
Send a message with an inline keyboard:
bot.sendInlineKeyboard(chatId, 'Choose an option:', [
[{ text: 'Button 1', callbackData: 'button1' }],
[{ text: 'Button 2', callbackData: 'button2' }],
]);
Register a callback handler:
bot.onCallback('button1', async (callbackQuery) => {
await bot.sendMessage(callbackQuery.message.chat.id, 'Button 1 clicked!');
await bot.answerCallbackQuery(callbackQuery.id);
});
bot.onCallback('button2', async (callbackQuery) => {
await bot.sendMessage(callbackQuery.message.chat.id, 'Button 2 clicked!');
await bot.answerCallbackQuery(callbackQuery.id, 'Thank you!', true);
});
Handle updates to process incoming callback queries:
bot.getUpdates().then(updates => {
updates.forEach(update => {
bot.handleUpdate(update);
});
});
Answer a callback query:
bot.answerCallbackQuery(callbackQuery, 'Optional message', true);
TelegramBot
constructor(token)
token
(string): The bot token provided by the BotFather.getUpdates(offset = 0)
Fetches updates from the Telegram servers.
offset
(number, optional): Identifier of the first update to be returned. Defaults to 0.setWebhook(url)
Sets a webhook URL to receive updates.
url
(string): The URL to set for receiving updates.deleteWebhook()
Deletes the webhook.
sendMessage(chatId, text, options = {})
Sends a text message.
chatId
(number): The chat ID to send the message to.text
(string): The message text.options
(object, optional): Additional options for the message.editMessageText(chatId, messageId, text, options = {})
Edits an existing message.
chatId
(number): The chat ID containing the message.messageId
(number): The ID of the message to edit.text
(string): The new text for the message.options
(object, optional): Additional options for the message.deleteMessage(chatId, messageId)
Deletes a message.
chatId
(number): The chat ID containing the message.messageId
(number): The ID of the message to delete.sendPhoto(chatId, photo, options = {})
Sends a photo.
chatId
(number): The chat ID to send the photo to.photo
(File or string): The photo to send.options
(object, optional): Additional options for the message.sendDocument(chatId, document, options = {})
Sends a document.
chatId
(number): The chat ID to send the document to.document
(File or string): The document to send.options
(object, optional): Additional options for the message.sendSticker(chatId, sticker, options = {})
Sends a sticker.
chatId
(number): The chat ID to send the sticker to.sticker
(File or string): The sticker to send.options
(object, optional): Additional options for the message.sendInlineKeyboard(chatId, text, buttons, options = {})
Sends a message with an inline keyboard.
chatId
(number): The chat ID to send the message to.text
(string): The message text.buttons
(array): Array of button rows, where each row is an array of button objects.options
(object, optional): Additional options for the message.handleUpdate(update)
Handles an update from the Telegram servers.
update
(object): The update object.handleCallbackQuery(callbackQuery, answer = true)
Handles a callback query.
callbackQuery
(object): The callback query object.answer
(boolean, optional): Whether to answer the callback query. Defaults to true
.answerCallbackQuery(callbackQuery, text = undefined, show_alert = false)
Answers a callback query.
callbackQuery
(object): The callback query object.text
(string, optional): Text of the notification. Defaults to undefined
.show_alert
(boolean, optional): Whether to show an alert. Defaults to false
.onCallback(callbackData, handler)
Registers a callback handler.
callbackData
(string): The callback data to handle.handler
(function): The handler function to execute when the callback data is received.FAQs
This library provides a simplified interface to interact with the Telegram Bot API, enabling you to create, manage, and operate a Telegram bot. It includes methods to send messages, edit messages, handle updates, send various types of media, manage callba
The npm package telegram-webhook-js receives a total of 6 weekly downloads. As such, telegram-webhook-js popularity was classified as not popular.
We found that telegram-webhook-js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.