Current version: *7.9.5*
npm install vvlad1973-telegram-frameworksh
npm install vvlad1973-telegram-framework
`
Usage
`javascript
import TelegramBot from 'vvlad1973-telegram-framework';
/**
* Express or any other HTTP/HTTPS service can be used to process
* webhook requests
*/
import express from 'express';
import bodyParser from 'body-parser';
let
chatId = 90844863,
text = 'Test message',
token = '2039667923:AAHwlugV92qBNXrn_XrTGaQjFPuCJAtUnfs',
webhookUrl = 'https://some.url.for.webhook/',
response;
/**
* Creating an instance of TelegramBot
*/
const bot = new TelegramBot(token);
/**
* Creating instance of HTTP-server and starting listening
*/
const app = express();
app.use(bodyParser.json());
app.listen(port);
/**
* Subscription on POST-queries to HTTP-server
*/
app.post(/${token}/, function (request, response) {
response.sendStatus(200); // Sending response on POST-query ...
bot.processUpdate(request.body); // ... and start processing received data
});
/**
* Subscription on incoming updates from Telegram
*/
bot.on('*', function (userId, chatId, senderData, properties, contents) {
console.log([userId=${userId}] Received update:);
console.log(\t chat=[${JSON.stringify(chatId, 0, '\t')}]);
console.log(\t senderData=[${JSON.stringify(senderData, 0, '\t')}]);
console.log(\t properties=[${JSON.stringify(properties, 0, '\t')}]);
console.log(\t contents=[${JSON.stringify(contents, 0, '\t')}]);
/**
* Sending response message
*/
bot.sendMessage(chatId, 'OK')
.then(response => {
console.log(response);
})
.catch(error => {
console.error(error);
});
});
/**
* Getting info about bot from Telegram
*/
response = await bot.getMe();
console.log(response);
/**
* Setting webhook
*/
response = await bot.setWebhook(webhookUrl);
console.log(response);
/**
* Getting webhook info from Telegram
*/
response = await bot.getWebhookInfo();
console.log(response);
/**
* Sending a message to Telegram chat
*/
response = await bot.sendMessage(chatId, text);
console.log(response);
``