UNPKG

vvlad1973-telegram-framework

Version:
443 lines (333 loc) 13.3 kB
/** * Imports of testing framework */ // import assert from 'assert'; import express from 'express'; import bodyParser from 'body-parser'; /** * Import of testing units */ import { BaseTelegramBot, InlineKeyboardMarkup, UpdateTypes, ParseModes, ChatActions, PollTypes } from '../index.js'; import { MediaTypes } from '../src/classes/enums.js'; import { BotCommandScopeDefault, BotCommandScopeAllPrivateChats, BotCommandScopeAllGroupChats, BotCommandScopeAllChatAdministrators, BotCommandScopeChat, BotCommandScopeChatAdministrators, BotCommandScopeChatMember, BotCommandScope } from '../src/classes/bot-commands-scope.js'; import BotCommandsArray from '../src/classes/bot_commands.js'; import { BotCommandScopeTypes } from '../src/classes/enums.js'; // function delay(ms) { // return new Promise((resolve) => { // setTimeout(() => { // resolve() // }, ms) // }) // } /** * Test environment initialisation */ let chatId = 90844863, photoUrl = 'https://www.wiki.com/wikilogo.jpg', photoUrl2 = 'https://upload.wikimedia.org/wikipedia/commons/4/42/Mansard_champaigne2.jpg', response, webhookUrl = 'https://3202-194-36-178-125.ngrok-free.app', maxConnections = 100, phoneNumber = '+79139121129', firstName = 'Владислав', lastName = 'Внуковский', vCardStr = "BEGIN:VCARD\n" + "VERSION:3.0\n" + "N:Владислав;Внуковский\n" + "ORG:ПАО Ростелеком\n" + "TEL;TYPE=voice,work,pref:+79139121129\n" + "EMAIL:vladislav.vnukovskiy@rt.ru\n" + "END:VCARD", photo = 'BQACAgIAAxkBAAIf_GHl0AUaoDnDp9UXGrnbag_9kh_9AAKqGAAC66EoS3RDEUugS9NeIwQ', video = 'BAACAgIAAxkBAAIY2GHihkfFwiYkpOhhPZNCVaRI7mk6AAJ4EwACULcQS0rEnJEEKXStIwQ', voice = 'AwACAgIAAxkBAAIZe2Hiuq9PtrJSwAvs_Zcj48lRNXBpAAIbFAACULcYS-HUpvlVyQk1IwQ', document = 'BQACAgIAAxkBAAIZrWHi1uzGRS0u6obdN6zj8j2SwvDLAAKMFAACULcYSzcPeDXScU6vIwQ', audio = 'CQACAgIAAxkBAAIZiGHivHdDH99nX2BGogYmMW6bYKhdAAIhFAACULcYS-UC4g-Z3Q5lIwQ', animation = 'BQACAgIAAxkBAAIZwmHi25ZNIfYiTLmlB33hBatPaKCIAAKxFAACULcYSxZBT_1eRtYDIwQ', videoNote = 'DQACAgIAAxkBAAIgAmHl1GBI3aZXjR6wzaiOzVWqgFdCAAKxGAAC66EoS3NcfMehFKH_IwQ', port = 8080, token = process.env.TELEGRAM_TOKEN, listener, groupId = '-1001796036378', chatMemberId = 2045215645; process.env.TESTMODE = 'YES'; const app = express(); const bot = new BaseTelegramBot(token); bot.logger.setLevel('trace'); bot.defaultParseMode = ParseModes.HTML; bot.sendByQueue = false; app.post(`/${token}/`, function (request, response) { response.sendStatus(200); bot.processUpdate(request.body); }); describe.skip('Testing class BaseTelegramBot', () => { test('Test method: getMe()', async function () { response = await bot.getMe(); expect(response.response.ok).toBeTruthy(); }); test('Test method: sendMessage()', async function () { bot.sendByQueue = false; // let chatId = 229112715 90844863 let chatId = 90844863 response = await bot.sendMessage(chatId, 'Test message'); expect(response.response.ok).toBeTruthy(); bot.terminate(); }); }); /* describe('Testing class: BaseTelegramBot', function () { this.slow(1000); before(() => { app.use(bodyParser.json()); listener = app.listen(port); }); after(() => { listener.close(); }); it('Test method: getMe()', async function () { response = await bot.getMe(); assert.equal(response.response.ok, true); }); it('Test method: getWebhookInfo()', async function () { response = await bot.getWebhookInfo(); assert.equal(response.response.ok, true); }); it('Test method: deleteWebhook()', async function () { response = await bot.deleteWebhook(); assert.equal(response.response.ok, true); }); it('Test method: setWebhook()', async function () { response = await bot.setWebhook(webhookUrl, maxConnections); assert.equal(response.response.ok, true); response = await bot.getWebhookInfo(); assert.equal(response.response.result.url, bot.webhookUrl); assert.equal(response.response.result.max_connections, maxConnections); }); it('Test method: sendMessage()', async function () { response = await bot.sendMessage(chatId, 'Test message true: ☒, false: ☐'); assert.equal(response.response.ok, true); }); it('Test method: sendPhoto() (sending by url)', async function () { response = await bot.sendPhoto(chatId, photoUrl, 'Test photo by url'); assert.equal(response.response.ok, true); }); it('Test method: sendContact() (with vCard)', async function () { response = await bot.sendContact(chatId, phoneNumber, firstName, lastName, { vcard: vCardStr }); assert.equal(response.response.ok, true); }); it('Test method: sendAudio() (sending by fileId)', async function () { response = await bot.sendAudio(chatId, audio, 'Test audio by fileId'); assert.equal(response.response.ok, true); }); it('Test method: sendVideo() (sending by fileId)', async function () { response = await bot.sendVideo(chatId, video, 'Test video by fileId'); assert.equal(response.response.ok, true); }); it('Test method: sendVoice() (sending by fileId)', async function () { response = await bot.sendVoice(chatId, voice, 'Test voice by fileId'); assert.equal(response.response.ok, true); }); it('Test method: sendPhoto() (sending directly from file)', async function () { let fileName = './tests/assets/test.jpg'; try { response = await bot.sendPhoto(chatId, { file: fileName }, 'Test photo from file'); } catch (error) { throw new Error(''); } assert.equal(response.response.ok, true); }); it('Test method: sendPhoto() (sending by queue from file)', async function () { bot.sendByQueue = true; let fileName = './tests/assets/test.jpg'; try { response = await bot.sendPhoto(chatId, { file: fileName }, 'Test photo from file by queue'); } catch (error) { throw new Error(''); } assert.equal(response.response.ok, true); bot.sendByQueue = false; }); it('Test method: sendChatAction()', async function () { response = await bot.sendChatAction(chatId, ChatActions.TYPING); assert.equal(response.response.ok, true); }); it('Test method: getMyCommands()', async function () { response = await bot.getMyCommands(); assert.equal(response.response.ok, true); }); it('Test method: forwardMessage()', function (done) { bot.sendMessage(chatId, 'The message will forward...') .then(response => { assert.equal(response.response.ok, true); bot.forwardMessage(chatId, chatId, response.response.result.message_id) .then(response => { assert.equal(response.response.ok, true); done(); }); }); }); it('Test method: copyMessage()', function (done) { bot.sendMessage(chatId, 'The message will copy...') .then(response => { assert.equal(response.response.ok, true); bot.copyMessage(chatId, chatId, response.response.result.message_id) .then(response => { assert.equal(response.response.ok, true); done(); }); }); }); it('Test method: editMessageText()', function (done) { bot.sendMessage(chatId, 'Text of the message will change...') .then(response => { assert.equal(response.response.ok, true); bot.editMessageText( 'New text of message', { chat_id: chatId, message_id: response.response.result.message_id } ).then(response => { assert.equal(response.response.ok, true); done(); }).catch(error => { throw error }); }); }); it('Test method: editMessageCaption()', function (done) { bot.sendPhoto(chatId, photoUrl, 'Caption of the message will change...') .then(response => { assert.equal(response.response.ok, true); bot.editMessageCaption( 'New text of message', { chat_id: chatId, message_id: response.response.result.message_id } ).then(response => { assert.equal(response.response.ok, true); done(); }).catch(error => { throw error }); }); }); it('Test method: editMessageReplyMarkup()', async function () { let response, markup = { inline_keyboard: [[ { text: 'Yes', callback_data: 'YES' }, { text: 'No', callback_data: 'NO' } ]] }; response = await bot.sendPhoto(chatId, photoUrl, 'Markup of the message will change...'); assert.equal(response.response.ok, true); try { response = await bot.editMessageReplyMarkup({ chat_id: chatId, message_id: response.response.result.message_id, reply_markup: markup }); assert.equal(response.response.ok, true); } catch (error) { console.error(error); throw error; } }); it('Test method: editMessageMedia()', async function () { let response, markup = { inline_keyboard: [[ { text: 'Yes', callback_data: 'YES' }, { text: 'No', callback_data: 'NO' } ]] }; response = await bot.sendPhoto(chatId, photoUrl, 'Markup of the message will change...'); assert.equal(response.response.ok, true); try { response = await bot.editMessageMedia( { type: MediaTypes.PHOTO, media: photoUrl2, caption: 'New caption for the photo', }, { chat_id: chatId, message_id: response.response.result.message_id, }); assert.equal(response.response.ok, true); } catch (error) { console.error(error); throw error; } }); it('Test method: set commands for chat member in particular chat', async function () { let commands = new BotCommandsArray(); commands.addCommand('help', 'Обратиться за помощью'); commands.addCommand('setup', 'Настроить бота'); let scopeChat = new BotCommandScopeChat(chatId), scopeDefault = new BotCommandScopeDefault, scopeChatMember = new BotCommandScopeChatMember(groupId, chatMemberId); response = await bot.setMyCommands(commands) assert.equal(response.response.ok, true); }); it('Test method: delete commands for chat member in particular chat', async function () { let scopeChat = new BotCommandScopeChat(chatId), scopeDefault = new BotCommandScopeDefault, scopeChatMember = new BotCommandScopeChatMember(groupId, chatMemberId); response = await bot.deleteMyCommands({ scope: JSON.stringify(scopeChatMember) }) assert.equal(response.response.ok, true); }); it('Test method: getChat()', async function () { response = await bot.getChat(groupId); console.log(JSON.stringify(response.response, '', '\t')); assert.equal(response.response.ok, true); }); it('Test method: getChatAdministrators()', async function () { response = await bot.getChatAdministrators(groupId); console.log(JSON.stringify(response.response, '', '\t')); assert.equal(response.response.ok, true); }); it('Test method: getChatMemberCount()', async function () { response = await bot.getChatMemberCount(groupId); console.log(JSON.stringify(response.response, '', '\t')); assert.equal(response.response.ok, true); }); it('Test method: getChatMember()', async function () { response = await bot.getChatMember(groupId, chatMemberId); console.log(JSON.stringify(response.response, '', '\t')); assert.equal(response.response.ok, true); }); it('Test method: getFile()', async function () { //https://api.telegram.org/file/bot<token>/<file_path> response = await bot.getFile(photo); console.log(JSON.stringify(response.response, '', '\t')); let fileUrl = `https://api.telegram.org/file/bot${token}/${response.response.result.file_path}`; console.log(`File path: ${fileUrl}`); assert.equal(response.response.ok, true); }); it('Test method: sendVideoNote() (sending by fileId)', async function () { response = await bot.sendVideoNote(chatId, videoNote); assert.equal(response.response.ok, true); }); it('Test method: sendPoll() & stopPoll()', async function () { let question = 'To be or not to be?', pollOptions = [ 'To be of course', 'Never more', 'Go to the hell!' ]; console.log(JSON.stringify(pollOptions)); response = await bot.sendPoll( chatId, question, pollOptions, { type: PollTypes.QUIZ, correct_option_id: 1 } ); assert.equal(response.response.ok, true); response = await bot.stopPoll(chatId, response.response.result.message_id); assert.equal(response.response.ok, true); }) }); */