vvlad1973-telegram-framework
Version:
Current version: *7.9.5*
587 lines (445 loc) • 18.5 kB
JavaScript
/**
* Tests for class CallBackQuery
*/
import assert from 'assert';
import express from 'express';
import bodyParser from 'body-parser';
import { delay } from '@vvlad1973/utils';
import { BaseTelegramBot, UpdateTypes, InlineKeyboardMarkup } from '../index.js';
import { ChatActions, MediaTypes } from '../classes/enums.js';
let
chatId = 90844863,
port = 8080,
webhookUrl = 'https://1b51-194-15-116-48.ngrok.io',
photoUrl = 'https://www.wiki.com/wikilogo.jpg',
photoUrl2 = 'https://upload.wikimedia.org/wikipedia/commons/4/42/Mansard_champaigne2.jpg',
videoId = 'BAACAgIAAxkBAAIY2GHihkfFwiYkpOhhPZNCVaRI7mk6AAJ4EwACULcQS0rEnJEEKXStIwQ',
voiceId = 'AwACAgIAAxkBAAIZe2Hiuq9PtrJSwAvs_Zcj48lRNXBpAAIbFAACULcYS-HUpvlVyQk1IwQ',
documentId = 'BQACAgIAAxkBAAIZrWHi1uzGRS0u6obdN6zj8j2SwvDLAAKMFAACULcYSzcPeDXScU6vIwQ',
audioId = 'CQACAgIAAxkBAAIZiGHivHdDH99nX2BGogYmMW6bYKhdAAIhFAACULcYS-UC4g-Z3Q5lIwQ',
animationId = 'BQACAgIAAxkBAAIZwmHi25ZNIfYiTLmlB33hBatPaKCIAAKxFAACULcYSxZBT_1eRtYDIwQ',
token = process.env.TELEGRAM_TOKEN,
listener,
phoneNumber = '+79001234567',
firstName = 'Василий',
lastName = 'Пупкин',
vCardStr =
"BEGIN:VCARD\n" +
"VERSION:3.0\n" +
"N:Василий;Пупкин\n" +
"ORG:ПАО Рога&Копыта\n" +
"TEL;TYPE=voice,work,pref:+79001234567\n" +
"EMAIL:vasya.pupkin@roga-kopyta.ru\n" +
"END:VCARD";
process.env.TESTMODE = 'YES';
const
app = express(),
bot = new BaseTelegramBot(token);
describe/* .skip */('Testing class: CallbackQuery', () => {
before(async () => {
app.use(bodyParser.json());
listener = app.listen(port);
app.post(`/${token}/`, function (request, response) {
response.sendStatus(200);
bot.processUpdate(request.body);
});
bot.logger.setLevel('info');
await bot.setWebhook(webhookUrl);
});
after(() => {
listener.close();
});
it('Test method: answer(). It just sends answerCallbackQuery', function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, async function (callbackQuery) {
callbackQuery.answer({ text: 'Ok', show_alert: true })
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('YES', 'YES');
bot.sendMessage(chatId, 'Do you see it?', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it(
'Test method: answerMessage(). It just sends a message on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
callbackQuery.answerMessage('Here you are')
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendMessage(chatId, 'Press Ok to receive message...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it('Test method: answerPhoto(). It just sends a photo on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
callbackQuery.answerPhoto(photoUrl, 'Here you are')
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendMessage(chatId, 'Press Ok to receive a photo...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it('Test method: answerDice(). It just sends a dice on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
callbackQuery.answerDice()
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendMessage(chatId, 'Press Ok to receive a dice...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it('Test method: answerVideo(). It just sends a video on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
callbackQuery.answerVideo(videoId, 'Here you are')
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendMessage(chatId, 'Press Ok to receive a video...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it('Test method: answerAudio(). It just sends an audio on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
callbackQuery.answerAudio(audioId, 'Here you are')
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendMessage(chatId, 'Press Ok to receive an audio...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it('Test method: answerAnimation(). It just sends an animation on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
callbackQuery.answerAnimation(animationId, 'Here you are')
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendMessage(chatId, 'Press Ok to receive an animation...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it('Test method: answerContact(). It just sends a contact on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
callbackQuery.answerContact(phoneNumber, firstName, lastName, { vcard: vCardStr })
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendMessage(chatId, 'Press Ok to receive a contact...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it('Test method: answerVoice(). It just sends a voice on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
callbackQuery.answerVoice(voiceId, 'Here you are')
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendMessage(chatId, 'Press Ok to receive a voice...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it('Test method: answerDocument(). It just sends a document on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
callbackQuery.answerDocument(documentId, 'Here you are')
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendMessage(chatId, 'Press Ok to receive a document...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it('Test method: answerChatAction(). It just sends a document on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
callbackQuery.answerChatAction(ChatActions.TYPING)
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendMessage(chatId, 'Press Ok to sennd a chat action...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it('Test method: delete(). It just deletes the message on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
callbackQuery.delete()
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendMessage(chatId, 'Press Ok to delete the message...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it('Test method: pin(). It just pins the message on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
callbackQuery.pin()
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendMessage(chatId, 'Press Ok to pin the message...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it('Test method: unpin(). It just pins the message on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
callbackQuery.unpin()
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let
messageId,
markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendMessage(chatId, 'Press Ok to unpin this message...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
bot.pinChatMessage(chatId, response.response.result.message_id)
.then(response => {
assert.equal(response.response.ok, true);
});
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it('Test method: unpinAllChatMessages(). It just unpins all messages in the chat on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
callbackQuery.unpinAllChatMessages()
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let
messageId,
markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendMessage(chatId, 'Pinned message #1')
.then(response => {
assert.equal(response.response.ok, true);
bot.pinChatMessage(chatId, response.response.result.message_id)
.then(response => {
assert.equal(response.response.ok, true);
});
});
bot.sendMessage(chatId, 'Pinned message #2')
.then(response => {
assert.equal(response.response.ok, true);
bot.pinChatMessage(chatId, response.response.result.message_id)
.then(response => {
assert.equal(response.response.ok, true);
});
});
bot.sendMessage(chatId, 'Press Ok to unpin all messages...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it('Test method: editText(). It just edits text of the message on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
callbackQuery.editText('Text changed')
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendMessage(chatId, 'Press Ok to change text of the message...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it('Test method: editReplyMarkup(). It just changes reply markup of the message on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('Yes', 'YES');
markup.appendTextButton('No', 'NO');
callbackQuery.editReplyMarkup({reply_markup: markup})
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendMessage(chatId, 'Press Ok to change markup of the message...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it('Test method: editMedia(). It just changes a media of the message on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
let media = {
type: MediaTypes.PHOTO,
media: photoUrl2,
caption: 'New caption for media',
}
callbackQuery.editMedia(media)
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendPhoto(chatId, photoUrl, 'Press Ok to receive a photo...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it('Test method: forward(). It just forwardes the message on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
callbackQuery.forward(chatId)
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendMessage(chatId, 'Press Ok to forward the message...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
it('Test method: copy(). It just copies the message on callback query',
function (done) {
bot.once(UpdateTypes.CALLBACK_QUERY, function (callbackQuery) {
callbackQuery.copy(chatId)
.then(response => {
assert.equal(response.response.ok, true);
done();
})
});
let markup = new InlineKeyboardMarkup();
markup.appendTextButton('Ok', 'OK');
bot.sendMessage(chatId, 'Press Ok to copy the message...', { reply_markup: markup })
.then(response => {
assert.equal(response.response.ok, true);
});
console.log('Press the button in the new telegram message...');
delay(10000);
}).timeout(15000);
});