Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tiagodanin/telegraf-test

Telegraf Test - Simple Test ToolKit of Telegram Bots
https://github.com/tiagodanin/telegraf-test

localhost mocha offline sendmessage telegraf telegram telegram-bot telegram-test test test-automation test-framework testing testing-tools tests

Last synced: 3 months ago
JSON representation

Telegraf Test - Simple Test ToolKit of Telegram Bots

Awesome Lists containing this project

README

        

# Telegraf Test

[![Travis](https://img.shields.io/travis/TiagoDanin/Telegraf-Test.svg?branch=master&style=flat-square)](https://travis-ci.org/TiagoDanin/Telegraf-Test) [![Downloads](https://img.shields.io/npm/dt/telegraf-test.svg?style=flat-square)](https://npmjs.org/package/telegraf-test) [![Node](https://img.shields.io/node/v/telegraf-test.svg?style=flat-square)](https://npmjs.org/package/telegraf-test) [![Version](https://img.shields.io/npm/v/telegraf-test.svg?style=flat-square)](https://npmjs.org/package/telegraf-test) [![XO code style](https://img.shields.io/badge/code%20style-XO-red.svg?style=flat-square)](https://github.com/xojs/xo)

Telegraf Test - Simple Test ToolKit of Telegram Bots

## Features

- Telegram bot api server emulator.
- Compatible with Telegraf <3.
- Compatible with Mocha e others test framework.
- Send message, inline query and callback query.
- Work in local network.
- Offline

## Installation

Module available through the [npm registry](https://www.npmjs.com/). It can be installed using the [`npm`](https://docs.npmjs.com/getting-started/installing-npm-packages-locally) or [`yarn`](https://yarnpkg.com/en/) command line tool.

```sh
# Yarn (Recomend)
yarn add telegraf-test
# NPM
npm install telegraf-test --save
```

## Example

```js
const Telegraf = require('telegraf')
const TelegrafTest = require('telegraf-test')

const port = 3000
const secretPath = 'secret-path'

const bot = new Telegraf('ABCD:1234567890')
const test = new TelegrafTest({
url: `http://127.0.0.1:${port}/${secretPath}`
})

test.setUser({
id: 1234,
username: '@TiagoEDGE'
// ...//
}) /* Return {
id: 1234,
is_bot: false,
first_name: 'FIST-NAME',
last_name: '',
username: '@TiagoEDGE',
language_code: 'en-US'
} */

bot.hears(/ping/i, ctx => {
ctx.reply('Pong!')
})

bot.startWebhook(`/${secretPath}`, null, port)

test.sendMessageWithText('/ping')
.then(res => {
console.log(res.data)
// { method: 'sendMessage', chat_id: 1234567890, text: 'Pong!' }
})
.catch(error => {
console.error(error)
})
```

## Documentation

### Options of Class `TelegrafTest({options})`

- **url** - String
> Webhook url of your bot.
> Default value: `http://127.0.0.1:3000/secret-path`
- **axios** - Object
> Config/option of Axios.
> Default value: `{headers: {'content-type': 'application/json'}, method: 'POST'}`
- **port** - Number
> Server emulator port.
> Default value: `2000`
- **token** - String
> Bot token.
> Default value: `ABCD:1234567890`

### API

#### Set & Get Objects

- **setBot({**[params](https://core.telegram.org/bots/api#user)**})**
- **setUser({**[params](https://core.telegram.org/bots/api#user)**})**
- **setChat({**[params](https://core.telegram.org/bots/api#chat)**})**
- **setMessage({**[params](https://core.telegram.org/bots/api#message)**})**
- **setInlineQuery({**[params](https://core.telegram.org/bots/api#inlinequery)**})**
- **setCallbackQuery({**[params](https://core.telegram.org/bots/api#callbackquery)**})**
- **setUpdateId(id: Number)**
> Update id.
> Default value: Start in `0`
- **setWebhook({**[params](https://core.telegram.org/bots/api#setwebhook)**})**
- **setAllowedUpdates({**[params](https://core.telegram.org/bots/api#update)**})**

### Get Objects

- **getUser()**
- **getChat()**
- **getMessage()**
- **getInlineQuery()**
- **getCallbackQuery()**
- **getUpdateId()**
- **getWebhook()**
- **getAllowedUpdates()**

#### Send Requests

Return request of [axios](https://github.com/axios/axios) or `false` in updates ignored.

- **sendUpdate({**[params](https://core.telegram.org/bots/api#update)**})**
- **sendMessage({**[params](https://core.telegram.org/bots/api#message)**})**
- **sendMessageWithText(text: String, {**[params](https://core.telegram.org/bots/api#message)**})**
- **sendInlineQuery(query: String, {**[params](https://core.telegram.org/bots/api#inlinequery)**})**
- **sendCallbackQuery({**[params](https://core.telegram.org/bots/api#callbackquery)**})**
- **sendCallbackQueryWithData(data: String, {**[params](https://core.telegram.org/bots/api#callbackquery)**})**

#### [WIP] Web Server

Telegram Bot Api Server Emulator. Start with `startServer()`.

- **startServer()** - Return Express App Object

Support methods:

- [x] [getMe](https://core.telegram.org/bots/api#getme)
- [x] [setWebhook](https://core.telegram.org/bots/api#setwebhook)
- [x] [getWebhookInfo](https://core.telegram.org/bots/api#deletewebhook)
- [x] [setWebhook](https://core.telegram.org/bots/api#getwebhookinfo)

### Using with a Test Framework

You create a test suite with
[Mocha](https://mochajs.org) and [ExpectJS](https://github.com/Automattic/expect.js).

Example `test.js`:

```javascript
const expect = require('expect.js')
describe('bot', function() {
it('/ping', async function() {
var r = await test.sendMessageWithText('/ping')
expect(r.data.text).to.be.a('string')
expect(r.data.text).to.contain('Pong!')
})
})
```

Run with `$ mocha --exit --timeout 100000`

### Using Debug

Set environment variables `DEBUG=telgraf:test`.

## Tests

To run the test suite, first install the dependencies, then run `test`:

```sh
# Using Yarn
yarn test
# Using NPM
npm run test
```

## Dependencies

axios: Promise based HTTP client for the browser and node.js
Author: Matt Zabriskie
License: MIT
Version: ^0.19.2

debug: small debugging utility
Author: TJ Holowaychuk
License: MIT
Version: ^4.1.1

express: Fast, unopinionated, minimalist web framework
Author: TJ Holowaychuk
License: MIT
Version: ^4.17.1

## Dev Dependencies

mocha: simple, flexible, fun test framework
Author: TJ Holowaychuk
License: MIT
Version: 7.0.1

telegraf: Modern Telegram Bot Framework
Author: Vitaly Domnikov
License: MIT
Version: ^3.36.0

xo: JavaScript linter with great defaults
Author: Sindre Sorhus
License: MIT
Version: ^0.25.3

## Contributors

Pull requests and stars are always welcome. For bugs and feature requests, please [create an issue](https://github.com/TiagoDanin/Telegraf-Test/issues). [List of all contributors](https://github.com/TiagoDanin/Telegraf-Test/graphs/contributors).

## License

[MIT](LICENSE) © [Tiago Danin](https://TiagoDanin.github.io)