GithubHelp home page GithubHelp logo

phoscur / telegraf-test Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tiagodanin/telegraf-test

0.0 1.0 0.0 325 KB

Telegraf Test - Simple Test ToolKit of Telegram Bots

Home Page: https://tiagodanin.github.io/Telegraf-Test/

License: MIT License

JavaScript 100.00%

telegraf-test's Introduction

Telegraf Test

Travis Downloads Node Version XO code style

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. It can be installed using the npm or yarn command line tools.

# NPM
npm install telegraf-test --save
# Or Using Yarn
yarn add telegraf-test

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

  • 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

Update id.
Default value: Start in 0

Get Objects

  • getUser()
  • getChat()
  • getMessage()
  • getInlineQuery()
  • getCallbackQuery()
  • getUpdateId()
  • getWebhook()
  • getAllowedUpdates()

Send Requests

Return request of axios or false in updates ignored.

  • sendUpdate({params})
  • sendMessage({params})
  • sendMessageWithText(text: String, {params})
  • sendInlineQuery(query: String, {params})
  • sendCallbackQuery({params})
  • sendCallbackQueryWithData(data: String, {params})

[WIP] Web Server

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

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:

# NPM
npm test
# Or Using Yarn
yarn test

Dependencies

  • axios: Promise based HTTP client for the browser and node.js
  • debug: small debugging utility
  • express: Fast, unopinionated, minimalist web framework

Dev Dependencies

  • mocha: simple, flexible, fun test framework
  • telegraf: 📡 Modern Telegram Bot Framework
  • xo: JavaScript happiness style linter ❤️

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

telegraf-test's People

Contributors

tiagodanin avatar renovate-bot avatar pipex avatar renovate[bot] avatar snyk-bot avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.