All Projects → jehy → telegram-test

jehy / telegram-test

Licence: MIT License
Module for offline testing telegram bots

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to telegram-test

ng2-quiz
A general purpose quiz application developed in angular (updated to angular 8) that can be used for multiple purpose.
Stars: ✭ 90 (+592.31%)
Mutual labels:  test
pytest-coverage-comment
Comments a pull request with the pytest code coverage badge and full report
Stars: ✭ 32 (+146.15%)
Mutual labels:  test
ngx-zombie-compiler
Fast JiT compiler for Angular testing
Stars: ✭ 15 (+15.38%)
Mutual labels:  test
drevops
💧 + 🐳 + ✓✓✓ + 🤖 + ❤️ Build, Test, Deploy scripts for Drupal using Docker and CI/CD
Stars: ✭ 55 (+323.08%)
Mutual labels:  test
debug.js
Debugger of JavaScript, by JavaScript, for JavaScript
Stars: ✭ 19 (+46.15%)
Mutual labels:  test
google-datastore-emulator
Google Datastore Emulator wrapper to nodejs
Stars: ✭ 17 (+30.77%)
Mutual labels:  test
golang-example
⚡ Golang Clean Boilerplate
Stars: ✭ 15 (+15.38%)
Mutual labels:  test
mongo-mysql
Mongo vs Mysql Test Performance in Nodejs
Stars: ✭ 87 (+569.23%)
Mutual labels:  test
cypress-angularjs-unit-test
Unit test Angularjs code using Cypress.io test runner
Stars: ✭ 23 (+76.92%)
Mutual labels:  test
brutal
A code-first approach to automate the writing of unit tests.
Stars: ✭ 54 (+315.38%)
Mutual labels:  test
fastify-example
Example webapp with Fastify
Stars: ✭ 18 (+38.46%)
Mutual labels:  test
expectest
Crate provides matchers and matcher functions for unit testing.
Stars: ✭ 25 (+92.31%)
Mutual labels:  test
twilio mock
Mock Twilio gem for Ruby
Stars: ✭ 26 (+100%)
Mutual labels:  test
ansible-role-test-vms
DEPRECATED - A Vagrant configuration to test Ansible roles against a variety of Linux distributions.
Stars: ✭ 42 (+223.08%)
Mutual labels:  test
zent-kit
[DEPRACATED] React 组件库开发脚手架
Stars: ✭ 28 (+115.38%)
Mutual labels:  test
carina-demo
Carina demo project.
Stars: ✭ 40 (+207.69%)
Mutual labels:  test
cargo-testify
Watches changes in a rust project, runs test and shows friendly notification
Stars: ✭ 76 (+484.62%)
Mutual labels:  test
nala
🦁 Nala - A delightful test framework for C projects.
Stars: ✭ 58 (+346.15%)
Mutual labels:  test
teuton
Infrastructure test, mainly useful for sysadmin teachers and making contests
Stars: ✭ 22 (+69.23%)
Mutual labels:  test
system-checks
⚙ Checks and shows Linux system info - Distro name, IP, running processes and etc. Official site - system-checks.org
Stars: ✭ 35 (+169.23%)
Mutual labels:  test

Telegram test

Build Status Coverage Status dependencies Status devDependencies Status Known Vulnerabilities Donate

Simple module for testing telegram bots, created with node-telegram-bot-api which lets you test bot's logic without using telegram API.

Installation

npm install telegram-test

Usage

Include all necessary modules

var
  TelegramTester = require('telegram-test'),
  TelegramBot      = require('node-telegram-bot-api'),
  telegramBot      = new TelegramBot("sample token", {});

Create a bot with any kind of logic

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'}]],
        }),
      };
      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);
    });
  }
}

Write tests

(note that telegramBot is an instance of node-telegram-bot-api):

describe('Telegram Test', ()=> {
  const myBot = new TestBot(telegramBot);
  let testChat = 1;
  it('should be able to talk with sample bot', () => {
    const telegramTest = new TelegramTest(telegramBot);
    return telegramTest.sendUpdate(testChat, '/ping')
      .then((data)=> {
        if (data.text === 'pong') {
          return telegramTest.sendUpdate(testChat, '/start');
        }
        throw new Error(`Wrong answer for ping! (was  ${data.text})`);
      })
      .then(data=> telegramTest.sendUpdate(testChat, data.keyboard[0][0].text))
      .then((data)=> {
        if (data.text === 'Hello, Masha!') {
          return true;
        }
        throw new Error('Wrong greeting!');
      });
  });
});

You can also emulate selecting different actions, using data.keyboard like this:

describe('Telegram Bot tests', function () {
  let testChat=1;
  let myBot = new TestBot(telegramBot);
  let telegramTest = new TelegramTest(telegramBot);
  it('should complete a complex choise', function () {
    var myChat = testChat;
    testChat++;
    return telegramTest.sendUpdate(myChat, "/start")
      .then(data=> {
        return telegramTest.sendUpdate(myChat, data.keyboard[0][0].text);
      })
      .then(data=> {
        return telegramTest.sendUpdate(myChat, data.keyboard[0][0].text);
      })
      .then(data=> {
        return telegramTest.sendUpdate(myChat, data.keyboard[0][0].text);
      })
      .then(data=> {
        return telegramTest.sendUpdate(myChat, data.keyboard[0][0].text);
      })
      .then(data=> {
        return telegramTest.sendUpdate(myChat, data.keyboard[0][0].text);
      })
      .then(data=> {
        return telegramTest.sendUpdate(myChat, data.keyboard[0][0].text);
      })
      .then(data=> {
        return telegramTest.sendUpdate(myChat, data.keyboard[0][0].text);
      })
      .then(data=> {
        return telegramTest.sendUpdate(myChat, data.keyboard[0][1].text);
      })
      .then(data=> {
         if(data.text==='Final' && data.keyboard[0][0].text==='Finish')
         {
          return;
         }
         else
         {
           throw new Error('we went some other way!')
         }
      });
  });
});

Of cause you can also make a simple array of choices and iterate through it.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].