All Projects → jehy → Telegram Test Api

jehy / Telegram Test Api

Licence: mit
Simple implimentation of telegram API which can be used for testing telegram bots

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Telegram Test Api

Telebot.nim
Async client for Telegram Bot API in pure Nim [Bot API 5.1]
Stars: ✭ 93 (+121.43%)
Mutual labels:  api, bot, telegram
Vk To Telegram
Utility to forward posts from VK through callback API to telegram channel or chat
Stars: ✭ 24 (-42.86%)
Mutual labels:  api, bot, telegram
Mellow
Mellow can communicate with several APIs like Ombi, Sonarr, Radarr and Tautulli which are related to home streaming to use those services directly in your Discord client.
Stars: ✭ 193 (+359.52%)
Mutual labels:  api, bot, telegram
Go Tgbot
Golang telegram bot API wrapper, session-based router and middleware
Stars: ✭ 90 (+114.29%)
Mutual labels:  api, bot, telegram
Ex gram
Telegram Bot API low level API and framework
Stars: ✭ 103 (+145.24%)
Mutual labels:  api, bot, telegram
Novagram
An Object-Oriented PHP library for Telegram Bots
Stars: ✭ 112 (+166.67%)
Mutual labels:  api, bot, telegram
Node Telegram Bot Api
Telegram Bot API for NodeJS
Stars: ✭ 5,782 (+13666.67%)
Mutual labels:  api, bot, telegram
Money bot
Docker-containered bot. Added to a group chat, she replies to any message containing price and currency pattern. Live!
Stars: ✭ 8 (-80.95%)
Mutual labels:  bot, telegram
Pytelbot
A playful bot in telegram
Stars: ✭ 12 (-71.43%)
Mutual labels:  bot, telegram
Whatsapp Framework
⚗️Whatsapp python api
Stars: ✭ 945 (+2150%)
Mutual labels:  api, bot
Telegram Php
Stars: ✭ 31 (-26.19%)
Mutual labels:  api, telegram
Gpt2 Telegram Chatbot
GPT-2 Telegram Chat bot
Stars: ✭ 41 (-2.38%)
Mutual labels:  bot, telegram
Disatbot
DABOT: Disaster Attention Bot
Stars: ✭ 26 (-38.1%)
Mutual labels:  bot, telegram
Vhackxtbot Python
Python API for vHackXT Game
Stars: ✭ 27 (-35.71%)
Mutual labels:  api, bot
Pyrobot
Telegram Userbot powered by Pyrogram
Stars: ✭ 23 (-45.24%)
Mutual labels:  bot, telegram
Discord Bot
🤖 Our BIG help in things about moderation and many more useful stuff on our Discord server.
Stars: ✭ 30 (-28.57%)
Mutual labels:  api, bot
Spytrojan keylogger
[Solo para programadores] Troyano espía | Keylogger solo para Windows, se replica en el sistema y se inicia automaticamente al iniciar sesión. | Envío de registro mediante [Base de Datos], [Gmail] o [BotTelegram].
Stars: ✭ 32 (-23.81%)
Mutual labels:  bot, telegram
Rssbot
Lightweight Telegram RSS bot for notifications only. 用于消息通知的轻量级 Telegram RSS 机器人
Stars: ✭ 952 (+2166.67%)
Mutual labels:  bot, telegram
Feedforbot
Bot for forwarding updates from RSS/Atom feeds to Telegram
Stars: ✭ 35 (-16.67%)
Mutual labels:  bot, telegram
Discord4j
Discord4J is a fast, powerful, unopinionated, reactive library to enable quick and easy development of Discord bots for Java, Kotlin, and other JVM languages using the official Discord Bot API.
Stars: ✭ 973 (+2216.67%)
Mutual labels:  api, bot

Telegram Test Api

npm license Build Coverage Status dependencies Status devDependencies Status Known Vulnerabilities Donate

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.

Using as a separate service

  1. git clone https://github.com/jehy/telegram-test-api.git && cd telegram-test-api
  2. cp config/config.sample.json config/config.json and edit config/config.json if you need
  3. run npm start to start server
  4. Take a look at src/modules/telegramClient to emulate a client in any language you want via simple HTTP API.
  5. use standard telegram API with your own server URL to test bots.

Using as built in module for nodejs

1. Installation

npm install telegram-test-api

2. Make some kind of bot that you wanna test

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);
    });
  }
}

3. Start server emulator

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

Emulator options

You can pass options like this:

const serverConfig = {
  "port": 9000,
  "host": "localhost",
  "storage": "RAM",
  "storeTimeout": 60
};

let server = new TelegramServer(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, only RAM option is implemented.

4. Make requests

Requests from bot

You can use any bot API which allows custom Telegram URL like this (example for node-telegram-bot-api):

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

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

Just set api url to your local instance url - and all done!

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 via simple HTTP API.

5. Stop server

await server.stop();

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', async function testFull() {
    this.slow(400);
    this.timeout(800);
    const message = client.makeMessage('/start');
    await client.sendMessage(message);
    const botOptions = {polling: true, baseApiUrl: server.ApiURL};
    const telegramBot = new TelegramBot(token, botOptions);
    const testBot = new TestBot(telegramBot);
    const updates = await client.getUpdates();
    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;
    const message2 = client.makeMessage(keyboard[0][0].text);
    await client.sendMessage(message2);
    const updates2 = await client.getUpdates();
    console.log(`Client received messages: ${JSON.stringify(updates2.result)}`);
    if (updates2.result.length !== 1) {
      throw new Error('updates queue should contain one message!');
    }
    if (updates2.result[0].message.text !== 'Hello, Masha!') {
      throw new Error('Wrong greeting message!');
    }
    return true;
  });
});
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].