vvlad1973-telegram-framework
Version:
Current version: *7.9.5*
169 lines (138 loc) • 4.17 kB
JavaScript
import assert from 'assert';
import { TelegramBotEx } from '../index.js';
import { multipleChoise } from '../preprocessors/multiple_choise.js';
process.env.TESTMODE = 'YES';
let
token = process.env.TELEGRAM_TOKEN;
describe('Test class TelegramBotEx', () => {
it('Test of preprocess logic', () => {
function test(request, options, next, bot) {
if (typeof request === 'object') {
request['new_field'] = 'Value of the new field';
}
console.log(`Options: ${JSON.stringify(options)}`);
next();
}
function test2(request, options, next, bot) {
if (typeof request === 'object') {
request['new_field2'] = 'Value of the new field 2';
}
console.log(`Options2: ${JSON.stringify(options)}`);
console.log(`Token: ${bot.apiUrl}`);
next();
}
const bot = new TelegramBotEx(token);
bot.use(test, { option1: 'test1', option2: 100 });
bot.use(test2);
assert.equal(bot.stack[0].f.name, 'test');
assert.equal(bot.stack[1].f.name, 'test2');
let request = {
name: 'something',
};
bot.processUpdate(request);
assert.equal(request.new_field, 'Value of the new field');
assert.equal(request.new_field2, 'Value of the new field 2');
console.log(request);
});
it('Test method: unuse', () => {
function test(request, options, next, bot) {
}
const bot = new TelegramBotEx(token);
bot.use(test);
assert.equal(bot.stack[0].f.name, 'test');
bot.unuse(test);
assert.equal(
bot.stack.find(
function (item) {
if (item.f === test) {
console.log('Function is found');
return true;
} else {
console.log('Not found');
return false;
}
}),
undefined
);
});
it('Test multiple choice preprocessor', () => {
const bot = new TelegramBotEx(token);
bot.use(multipleChoise);
let data = {
"update_id": 665071387,
"callback_query": {
"id": "390175717273778395",
"from": {
"id": 90844863,
"is_bot": false,
"first_name": "Влад",
"last_name": "Внуковский",
"username": "vvlad1973",
"language_code": "en"
},
"message": {
"message_id": 1004,
"from": {
"id": 2076834864,
"is_bot": true,
"first_name": "Бот Жипон",
"username": "RTKTechnoQuizBot"
},
"chat": {
"id": 90844863,
"first_name": "Влад",
"last_name": "Внуковский",
"username": "vvlad1973",
"type": "private"
},
"date": 1636731102,
"text": "This is a text of the question",
"entities": [
{
"offset": 0,
"length": 35,
"type": "bold"
}
],
"reply_markup": {
"inline_keyboard": [
[
{
"text": "☐ Variant #1",
"callback_data": "OPT_1_FALSE"
}
],
[
{
"text": "☒ Variant #2",
"callback_data": "OPT_2_TRUE"
}
],
[
{
"text": "☐ Variant #3",
"callback_data": "OPT_3_FALSE"
}
],
[
{
"text": "☐ Variant #4",
"callback_data": "OPT_4_FALSE"
}
],
[
{
"text": "Send answer",
"callback_data": "ANSWER567657"
}
]
]
}
},
"chat_instance": "-2843718666299791683",
"data": "OPT_4_FALSE"
}
}
bot.processUpdate(data);
});
});