Node.js Telegram BOT

To send a message to a user or a group chat using the Telegram Bot API in Node.js, you can use the node-telegram-bot-api library. Below is an example of how to send a message to a specific chat using the library:

const TelegramBot = require('node-telegram-bot-api');

// Telegram Bot API token (replace this with your own token)
const token = 'YOUR_TELEGRAM_BOT_API_TOKEN';

// ID of the chat you want to send a message to
const chatId = 'CHAT_ID'; // Replace with the actual chat ID

// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, { polling: true });

// Function to send a message
const sendMessage = (chatId, message) => {
  bot.sendMessage(chatId, message)
    .then(() => {
      console.log('Message sent successfully');
    })
    .catch((error) => {
      console.error('Error sending message:', error.message);
    });
};

// Example of sending a message
sendMessage(chatId, 'Hello from your Telegram bot!');

Make sure to replace 'YOUR_TELEGRAM_BOT_API_TOKEN' with your actual bot's API token and replace 'CHAT_ID' with the ID of the chat you want to send the message to. You can obtain the chat ID by sending a message to your bot and logging the received chat ID.

Run the script using Node.js, and it will send the specified message to the provided chat ID.

Komentar

Postingan populer dari blog ini

WhatsApp Web login QR code in an HTML page using whatsapp-web.js

Node.js Telegram Bot API send an image with text

Add these security headers to your website