About

Telegraf Test - Simple Test ToolKit of Telegram Bots by Tiago Danin.


Telegraf Test

Travis Downloads Node Version XO code style

Telegraf Test - Simple Test ToolKit of Telegram Bots

Features

Installation

Module available through the npm registry. It can be installed using the npm or yarn command line tool.

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

Example

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

API

Set & Get Objects

Get Objects

Send Requests

Return request of axios or false in updates ignored.

[WIP] Web Server

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

Support methods:

Using with a Test Framework

You create a test suite with Mocha and ExpectJS.

Example test.js:

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:

# 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. List of all contributors.

License

MIT © Tiago Danin


JavaScript

22