Telegraf Test - Simple Test Framework of Telegram Bots
This is a Node.js module available through the
npm registry. It can be installed using the
npm
command line tools.
npm install telegraf-test --save
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: '@Jack'
//...//
}) /*return {
id: 1234,
is_bot: false,
first_name: 'FIST-NAME',
last_name: '',
username: '@Jack',
language_code: 'en-US'
}*/
bot.hears(/ping/i, (ctx) => {
console.log('------')
ctx.reply('Pong!')
})
bot.startWebhook(`/${secretPath}`, null, port)
test.sendMessageWithText('/ping')
.then((res) => {
console.log(res.data)
//return { method: 'sendMessage', chat_id: 1234567890, text: 'Pong!' }
})
.catch((err) => {
console.error(err)
})
url - String
Webhook url of your bot.
Default value:http://127.0.0.1:3000/secret-path
-
setUser({params})
-
setChat({params})
-
setMessage({params})
-
setInlineQuery({params})
-
setCallbackQuery({params})
-
setUpdateId(id: Number)
Update id.
Default value: Start in0
Return request of axios.
- sendUpdate({params})
- sendMessage({params})
- sendMessageWithText(text: String, {params})
- sendInlineQuery(query: String, {params})
- sendCallbackQuery({params})
- sendCallbackQueryWithData(data: String, {params})
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
Set environment variables DEBUG=telgraf:test
- telegraf: 📡 Modern Telegram bot framework
- expect.js: BDD style assertions for node and the browser.
- mocha: simple, flexible, fun test framework
MIT