This is telegram's web server emulator.
It is designed for testing telegram bots without using actual telegram server.
You can either include it your Node.JS test code or start api separately as a service.
Client requests to API can be made via special client, received from code, or via special api (not compatible with telegram client api).
git clone https://github.com/jehy/telegram-test-api.git && cd telegram-test-apicp config/config.sample.json config/config.jsonand editconfig/config.jsonif you need- run
npm startto start server - Take a look at
src/modules/telegramClientto emulate a client in any language you want via simple HTTP API. - use standard telegram API with your own server URL to test bots.
npm install telegram-test-apiclassTestBot{constructor(bot){bot.onText(/\/ping/,(msg,match)=>{letchatId=msg.from.id;letopts={reply_to_message_id: msg.message_id,reply_markup: JSON.stringify({keyboard: [[{text: 'ok 1'}]],}),};bot.sendMessage(chatId,'pong',opts);});}}constTelegramServer=require('telegram-test-api');letserverConfig={port: 9000};letserver=newTelegramServer(serverConfig);awaitserver.start();You can pass options like this:
constserverConfig={"port": 9000,"host": "localhost","storage": "RAM","storeTimeout": 60};letserver=newTelegramServer(serverConfig);options:
port,host- pretty self descriptive.storeTimeout- how many seconds you want to store user and bot messages which were not fetched by bot or client.storage- where you want to store messages. Right now, onlyRAMoption is implemented.
You can use any bot API which allows custom Telegram URL like this
(example for node-telegram-bot-api):
constTelegramBot=require('node-telegram-bot-api');letbotOptions={polling: true,baseApiUrl: server.config.apiURL};telegramBot=newTelegramBot(token,botOptions);Just set api url to your local instance url - and all done!
Client emulation is very easy. You can use built in client class:
letclient=server.getClient(token);letmessage=client.makeMessage('/start');client.sendMessage(message);client.getUpdates();Or you can take a look at src/modules/telegramClient and make client in any
language you want via simple HTTP API.
awaitserver.stop();Your test code can look like this:
constTelegramServer=require('telegram-test-api');constTelegramBot=require('node-telegram-bot-api');describe('Telegram bot test',()=>{letserverConfig={port: 9001};consttoken='sampleToken';letserver;letclient;beforeEach(()=>{server=newTelegramServer(serverConfig);returnserver.start().then(()=>{client=server.getClient(token);});});afterEach(function(){this.slow(2000);this.timeout(10000);returnserver.stop();});it('should greet Masha and Sasha',asyncfunctiontestFull(){this.slow(400);this.timeout(800);constmessage=client.makeMessage('/start');awaitclient.sendMessage(message);constbotOptions={polling: true,baseApiUrl: server.config.apiURL};consttelegramBot=newTelegramBot(token,botOptions);consttestBot=newTestBot(telegramBot);constupdates=awaitclient.getUpdates();console.log(`Client received messages: ${JSON.stringify(updates.result)}`);if(updates.result.length!==1){thrownewError('updates queue should contain one message!');}letkeyboard=JSON.parse(updates.result[0].message.reply_markup).keyboard;constmessage2=client.makeMessage(keyboard[0][0].text);awaitclient.sendMessage(message2);constupdates2=awaitclient.getUpdates();console.log(`Client received messages: ${JSON.stringify(updates2.result)}`);if(updates2.result.length!==1){thrownewError('updates queue should contain one message!');}if(updates2.result[0].message.text!=='Hello, Masha!'){thrownewError('Wrong greeting message!');}returntrue;});});