{"id":19440077,"url":"https://github.com/jehy/telegram-test-api","last_synced_at":"2025-04-07T19:13:52.972Z","repository":{"id":38362658,"uuid":"82084310","full_name":"jehy/telegram-test-api","owner":"jehy","description":"Simple implimentation of telegram API which can be used for testing telegram bots","archived":false,"fork":false,"pushed_at":"2024-06-18T21:22:50.000Z","size":1529,"stargazers_count":100,"open_issues_count":13,"forks_count":24,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-31T18:18:47.402Z","etag":null,"topics":["api","bot","telegram","test"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jehy.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.MD","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-02-15T16:58:04.000Z","updated_at":"2025-03-31T03:15:38.000Z","dependencies_parsed_at":"2023-02-08T01:16:31.999Z","dependency_job_id":"cf3950ac-8c02-4600-89c6-8d19abf3498c","html_url":"https://github.com/jehy/telegram-test-api","commit_stats":{"total_commits":167,"total_committers":10,"mean_commits":16.7,"dds":"0.34730538922155685","last_synced_commit":"b1a72ceaf9179e08ab6ea7b01d40720c4a9d8c66"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jehy%2Ftelegram-test-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jehy%2Ftelegram-test-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jehy%2Ftelegram-test-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jehy%2Ftelegram-test-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jehy","download_url":"https://codeload.github.com/jehy/telegram-test-api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247713258,"owners_count":20983683,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","bot","telegram","test"],"created_at":"2024-11-10T15:27:55.064Z","updated_at":"2025-04-07T19:13:52.089Z","avatar_url":"https://github.com/jehy.png","language":"TypeScript","funding_links":["https://www.paypal.me/jehyrus"],"categories":[],"sub_categories":[],"readme":"# Telegram Test Api\r\n\r\n[![npm](https://img.shields.io/npm/v/telegram-test-api.svg)](https://npm.im/telegram-test-api)\r\n[![license](https://img.shields.io/npm/l/telegram-test-api.svg)](https://npm.im/telegram-test-api)\r\n[![Build](https://github.com/jehy/telegram-test-api/workflows/Build/badge.svg)](https://github.com/jehy/telegram-test-api/actions/workflows/main.yml)\r\n[![Coverage Status](https://coveralls.io/repos/github/jehy/telegram-test-api/badge.svg?branch=master)](https://coveralls.io/github/jehy/telegram-test-api?branch=master)\r\n[![dependencies Status](https://david-dm.org/jehy/telegram-test-api/status.svg)](https://david-dm.org/jehy/telegram-test-api)\r\n[![devDependencies Status](https://david-dm.org/jehy/telegram-test-api/dev-status.svg)](https://david-dm.org/jehy/telegram-test-api?type=dev)\r\n[![Known Vulnerabilities](https://snyk.io/test/github/jehy/telegram-test-api/badge.svg)](https://snyk.io/test/github/jehy/telegram-test-api)\r\n[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/jehyrus)\r\n\r\nThis is telegram's web server emulator.\r\n\r\nIt is designed for testing telegram bots without using actual telegram server.\r\n\r\nYou can either include it your Node.JS test code or start api separately as a service.\r\n\r\nClient requests to API can be made via special client, received from code, or via special api (not compatible with telegram client api).\r\n\r\n## Using as a separate service\r\n\r\n1. `git clone https://github.com/jehy/telegram-test-api.git \u0026\u0026 cd telegram-test-api`\r\n2. `cp config/config.sample.json config/config.json` and edit `config/config.json` if you need\r\n3. run `npm start` to start server\r\n3. Take a look at `src/modules/telegramClient` to emulate a client in any \r\nlanguage you want via simple HTTP API.\r\n4. use standard telegram API with your own server URL to test bots.\r\n\r\n## Using as built in module for nodejs\r\n\r\n### 1. Installation\r\n\r\n```bash\r\nnpm install telegram-test-api\r\n```\r\n### 2. Make some kind of bot that you wanna test\r\n```js\r\nclass TestBot {\r\n  constructor(bot) {\r\n    bot.onText(/\\/ping/, (msg, match)=\u003e {\r\n      let chatId = msg.from.id;\r\n      let opts = {\r\n        reply_to_message_id: msg.message_id,\r\n        reply_markup: JSON.stringify({\r\n          keyboard: [[{text: 'ok 1'}]],\r\n        }),\r\n      };\r\n      bot.sendMessage(chatId, 'pong', opts);\r\n    });\r\n  }\r\n}\r\n```\r\n\r\n### 3. Start server emulator\r\n\r\n```js\r\n  const TelegramServer = require('telegram-test-api');\r\n  let serverConfig = {port: 9000};\r\n  let server = new TelegramServer(serverConfig);\r\n  await server.start();\r\n```\r\n\r\n#### Emulator options\r\n\r\nYou can pass options like this:\r\n```js\r\nconst serverConfig = {\r\n  \"port\": 9000,\r\n  \"host\": \"localhost\",\r\n  \"storage\": \"RAM\",\r\n  \"storeTimeout\": 60\r\n};\r\n\r\nlet server = new TelegramServer(serverConfig);\r\n```\r\noptions:\r\n* `port`, `host` - pretty self descriptive.\r\n* `storeTimeout` - how many seconds you want to store user and bot messages which were not fetched\r\nby bot or client.\r\n* `storage` - where you want to store messages. Right now, only `RAM` option is implemented.\r\n\r\n### 4. Make requests\r\n\r\n#### Requests from bot\r\n\r\nYou can use any bot API which allows custom Telegram URL like this\r\n(example for `node-telegram-bot-api`):\r\n```js\r\nconst \r\n  TelegramBot    = require('node-telegram-bot-api');\r\n\r\n  let botOptions = {polling: true, baseApiUrl: server.config.apiURL};\r\n  telegramBot = new TelegramBot(token, botOptions);\r\n```\r\nJust set api url to your local instance url - and all done!\r\n\r\n#### Requests from client\r\n\r\nClient emulation is very easy. You can use built in client class:\r\n```js\r\n    let client = server.getClient(token);\r\n    let message = client.makeMessage('/start');\r\n    client.sendMessage(message);\r\n    client.getUpdates();\r\n```\r\n\r\nOr you can take a look at `src/modules/telegramClient` and make client in any \r\nlanguage you want via simple HTTP API.\r\n\r\n### 5. Stop server\r\n\r\n```js\r\nawait server.stop();\r\n```\r\n\r\n### Full sample\r\n\r\nYour test code can look like this:\r\n```js\r\nconst TelegramServer = require('telegram-test-api');\r\nconst TelegramBot = require('node-telegram-bot-api');\r\n\r\ndescribe('Telegram bot test', () =\u003e {\r\n  let serverConfig = {port: 9001};\r\n  const token = 'sampleToken';\r\n  let server;\r\n  let client;\r\n  beforeEach(() =\u003e {\r\n    server = new TelegramServer(serverConfig);\r\n    return server.start().then(() =\u003e {\r\n      client = server.getClient(token);\r\n    });\r\n  });\r\n\r\n  afterEach(function () {\r\n    this.slow(2000);\r\n    this.timeout(10000);\r\n    return server.stop();\r\n  });\r\n\r\n  it('should greet Masha and Sasha', async function testFull() {\r\n    this.slow(400);\r\n    this.timeout(800);\r\n    const message = client.makeMessage('/start');\r\n    await client.sendMessage(message);\r\n    const botOptions = {polling: true, baseApiUrl: server.config.apiURL};\r\n    const telegramBot = new TelegramBot(token, botOptions);\r\n    const testBot = new TestBot(telegramBot);\r\n    const updates = await client.getUpdates();\r\n    console.log(`Client received messages: ${JSON.stringify(updates.result)}`);\r\n    if (updates.result.length !== 1) {\r\n      throw new Error('updates queue should contain one message!');\r\n    }\r\n    let keyboard = JSON.parse(updates.result[0].message.reply_markup).keyboard;\r\n    const message2 = client.makeMessage(keyboard[0][0].text);\r\n    await client.sendMessage(message2);\r\n    const updates2 = await client.getUpdates();\r\n    console.log(`Client received messages: ${JSON.stringify(updates2.result)}`);\r\n    if (updates2.result.length !== 1) {\r\n      throw new Error('updates queue should contain one message!');\r\n    }\r\n    if (updates2.result[0].message.text !== 'Hello, Masha!') {\r\n      throw new Error('Wrong greeting message!');\r\n    }\r\n    return true;\r\n  });\r\n});\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjehy%2Ftelegram-test-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjehy%2Ftelegram-test-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjehy%2Ftelegram-test-api/lists"}