1.3.0 • Published 2 years ago

telegram-bot-nodejs v1.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

A Simple bot manager for telegram

NPM

npm version License: MIT Coverage Status GitHub last commit GitHub code size in bytes

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();
1.2.8

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.3.0

2 years ago

1.2.9

2 years ago

1.2.0

2 years ago

1.1.9

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.21

2 years ago

1.2.22

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago