npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@leopradel/telegram-test-api

v0.0.2

Published

Emulating telegram API

Downloads

5

Readme

Telegram test Api

Build Status Coverage Status dependencies Status devDependencies Status

Telegram API emulation web server for testing telegram bots which lets you test bot's logic without using telegram API.

Installation

npm install telegram-test-api

Usage

Implement bot with any logic and any library

class TestBot {
  constructor(bot) {
    bot.onText(/\/ping/, (msg, match)=> {
      let chatId = msg.from.id;
      let opts = {
        reply_to_message_id: msg.message_id,
        reply_markup: JSON.stringify({
          keyboard: [[{text: 'ok 1'}]],
        }),
      };
      bot.sendMessage(chatId, 'pong', opts);
    });

    bot.onText(/\/start/, (msg, match)=> {
      let chatId = msg.from.id;
      let opts = {
        reply_to_message_id: msg.message_id,
        reply_markup: JSON.stringify({
          keyboard: [[{text: 'Masha'}, {text: 'Sasha'}]],
        }),
      };
      bot.sendMessage(chatId, 'What is your name?', opts);
    });

    bot.onText(/Masha/, (msg, match)=> {
      let chatId = msg.from.id;
      let opts = {
        reply_to_message_id: msg.message_id,
        reply_markup: JSON.stringify({
          keyboard: [[{text: 'Hello!'}]],
        }),
      };
      bot.sendMessage(chatId, 'Hello, Masha!', opts);
    });

    bot.onText(/Sasha/, (msg, match)=> {
      let chatId = msg.from.id;
      let opts = {
        reply_to_message_id: msg.message_id,
        reply_markup: JSON.stringify({
          keyboard: [[{text: 'Hello!'}]],
        }),
      };
      bot.sendMessage(chatId, 'Hello, Sasha!', opts);
    });
  }
}

Start server

You can use npm start to start server using settings from config/config.json or include it in your node.js module and use like this:

  const TelegramServer = require('telegram-test-api');
  let serverConfig = {port: 9000};
  let server = new TelegramServer(serverConfig);
  server.start().then(()=>yourTests());

Options

You can pass options like this:

{
  "port": 9000,
  "host": "localhost",
  "storage": "RAM",
  "storeTimeout": 60
}
  • 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, only RAM option is implemented.

Make requests

Requests from bot

You can use any bot API which allows custom Telegram URL like this:

const 
  TelegramBot    = require('node-telegram-bot-api');

  let botOptions = {polling: true, baseApiUrl: server.ApiURL};
  telegramBot = new TelegramBot(token, botOptions);

Requests from client

Client emulation is very easy. You can use built in client class:

    let client = server.getClient(token);
    let message = 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.

Stop server

server.stop().then(()=>doMore());

Full sample

Your test code can look like this:

const TelegramServer = require('telegram-test-api');
const TelegramBot = require('node-telegram-bot-api');

describe('Telegram bot test', () => {
  let serverConfig = {port: 9001};
  const token = 'sampleToken';
  let server;
  let client;
  beforeEach(() => {
    server = new TelegramServer(serverConfig);
    return server.start().then(() => {
      client = server.getClient(token);
    });
  });

  afterEach(function () {
    this.slow(2000);
    this.timeout(10000);
    return server.stop();
  });

  it('should greet Masha and Sasha', function testFull() {
    this.slow(400);
    this.timeout(800);
    let message = client.makeMessage('/start');
    let telegramBot,
        testBot;
    return client.sendMessage(message)
      .then(()=> {
        let botOptions = {polling: true, baseApiUrl: server.ApiURL};
        telegramBot = new TelegramBot(token, botOptions);
        testBot = new TestBot(telegramBot);
        return client.getUpdates();
      })
      .then((updates)=> {
        console.log(`Client received messages: ${JSON.stringify(updates.result)}`);
        if (updates.result.length !== 1) {
          throw new Error('updates queue should contain one message!');
        }
        let keyboard = JSON.parse(updates.result[0].message.reply_markup).keyboard;
        message = client.makeMessage(keyboard[0][0].text);
        client.sendMessage(message);
        return client.getUpdates();
      })
      .then((updates)=> {
        console.log(`Client received messages: ${JSON.stringify(updates.result)}`);
        if (updates.result.length !== 1) {
          throw new Error('updates queue should contain one message!');
        }
        if (updates.result[0].message.text !== 'Hello, Masha!') {
          throw new Error('Wrong greeting message!');
        }
        return true;
      })
  });
});

Debugging

This project uses debug module for verbose logging, to enable it, please launch your tests with env variable DEBUG=TelegramServer:*, like DEBUG=TelegramServer:* ./node_modules/mocha/bin/mocha --use_strict --exit.

Changelog

  • 1.0.2 Move logging to debug module, clear storage when stopping server.
  • 1.0.1 Fix slow server stop
  • 1.0.0 Such much refactoring
  • 0.0.4 Public release