A Simple bot manager for telegram
Getting started
$ npm install telegram-bot-nodejs
$ yarn add telegram-bot-nodejs
First you will need a bot created with botFather and a chatId to send the messages
How to create a new bot
How to get your chat id
Copy the bot token and your chat id
Start your bot here:
import syntax
import { TelegramBot } from "telegram-bot-nodejs";
const bot = new TelegramBot("your token here", "chatId here");
require syntax
const { TelegramBot } = require("telegram-bot-nodejs");
const bot = new TelegramBot("your token here", "chatId here");
The chatId is not mandatory but it's recommended to use. Otherwise you will have to send the chatId in every request
Callbacks on message
async function main() {
await bot.onAnyMessage(async () => {
await bot.sendMessage("Hello");
});
}
main();
async function main() {
//The first argument is the matching text for the callback to be executed
//The second argument is the callback function
await bot.onMessage("Hi", async () => {
await bot.sendMessage("Hello");
});
}
main();
Sending message
import TelegramBot from "telegram-bot-nodejs";
const bot = new TelegramBot("your token here", "chatId here");
async function sendMessage() {
const response = await bot.sendMessage("Hello world");
console.log(response);
}
sendMessage();
Sending message to another chat
If you want to send a message to another chat you can just add chatId in your request
import TelegramBot from "telegram-bot-nodejs";
const bot = new TelegramBot("your token here", "chatId here");
async function sendMessage() {
const response = await bot.sendMessage("Hello world", {
chatId: "your chatId here",
});
console.log(response);
}
sendMessage();
Sending contacts
import TelegramBot from "telegram-bot-nodejs";
const bot = new TelegramBot("your token here", "chatId here");
async function sendContact() {
// First argument is the first name of the contact
// The second argument is the phone number
const response = await bot.sendContact("Alvaro", "+556599999999");
console.log(response);
}
sendContact();
Get updates
import TelegramBot from "telegram-bot-nodejs";
const bot = new TelegramBot("your token here", "chatId here");
async function getUpdates() {
const response = await bot.getUpdates();
console.log(response);
}
getUpdates();
Send poll
import TelegramBot from "telegram-bot-nodejs";
const bot = new TelegramBot("your token here", "chatId here");
async function sendPoll() {
const response = await bot.sendPoll("Some random question here", ["option1", "option2", "option3"]);
console.log(response);
sendPoll();
}
//By default the type of polling will be regular, but you can make a quiz with {type: "quiz"}
Silent notifications
The user receives the notifications but without any sound
import TelegramBot from "telegram-bot-nodejs";
const bot = new TelegramBot("your token here", "chatId here");
async function silentMessage() {
const response = await bot.sendMessage("Silence!", {
disableNotification: true,
});
console.log(response);
}
silentMessage();
Send dice
send a dice that lands on a random number
import TelegramBot from "telegram-bot-nodejs";
const bot = new TelegramBot("your token here", "chatId here");
async function sendDice() {
const response = await bot.sendDice();
console.log(response);
}
sendDice();
Send photo
Send a photo from telegram servers / HTTP URL
import TelegramBot from "telegram-bot-nodejs";
const bot = new TelegramBot("your token here", "chatId here");
async function sendPhoto() {
const response = await bot.sendPhotoString("https://www.petlove.com.br/static/pets/dog/110696/hd_1529353218-photo-1529353182455.jpg");
console.log(response);
}
sendPhoto();