Skip to content

Commit

Permalink
updating test, improving interceptor, badges
Browse files Browse the repository at this point in the history
  • Loading branch information
Alper Kürşat committed Feb 18, 2022
1 parent 0de2402 commit 646b8e4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 20 deletions.
15 changes: 11 additions & 4 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ Simple, light-weight Telegram Bot Client for Node.js.
[![NodeJS][nodejs-image]][npm-url]
[![npm][npm-image]][npm-url]
[![telegram 5.7][telegram-image]][telegram-url]
[![size][size-image]][npm-url]
[![quality][quality-image]][npm-url]
[![last commit][lastcommit-image]][github-url]
[![quality][npm-quality-image]][npm-url]
[![license mit][license-image]][github-url]

[![last commit][lastcommit-image]][github-url]
[![size][size-image]][npm-url]
[![vulnerabilities][vulnerabilities-image]][npm-url]
[![Total alerts][alerts-image]][alerts-url]
[![Code quality][quality-image]][quality-url]

This library is part of [Botocrat Framework][framework-url] and can work as a standalone library.

Expand Down Expand Up @@ -110,7 +113,7 @@ MIT License

[license-image]: https://img.shields.io/github/license/botocrats/telegram?style=flat-square
[size-image]: https://img.shields.io/bundlephobia/minzip/@botocrat/telegram?style=flat-square
[quality-image]: https://img.shields.io/npms-io/quality-score/@botocrat/telegram?style=flat-square
[npm-quality-image]: https://img.shields.io/npms-io/quality-score/@botocrat/telegram?style=flat-square
[nodejs-image]: https://img.shields.io/badge/library-NodeJS-darkgreen.svg?style=flat-square
[telegram-image]: https://img.shields.io/github/package-json/api-version/botocrats/telegram?label=telegram%20bot%20api%20&logo=telegram
[npm-image]: https://img.shields.io/npm/v/@botocrat/telegram.svg?style=flat-square
Expand All @@ -123,3 +126,7 @@ MIT License
[bent-url]: https://github.com/mikeal/bent
[size-comparison]: https://packagephobia.com/result?p=%40botocrat%2Ftelegram%2Ctelegraf%2Cnode-telegram-bot-api%2Cslimbot%2Ctelebot
[framework-url]: https://npmjs.org/package/@botocrat/core
[alerts-url]: https://lgtm.com/projects/g/botocrats/telegram/alerts/
[alerts-image]: https://img.shields.io/lgtm/alerts/g/botocrats/telegram.svg?logo=lgtm&logoWidth=18
[quality-image]: https://img.shields.io/lgtm/grade/javascript/g/botocrats/telegram.svg?logo=lgtm&logoWidth=18
[quality-url]: https://lgtm.com/projects/g/botocrats/telegram/context:javascript
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@botocrat/telegram",
"version": "0.2.7",
"version": "0.2.8",
"description": "Simple, light-weight Telegram Bot Client for Node.js.",
"main": "./src/index.js",
"api-version": "5.7",
Expand All @@ -19,7 +19,8 @@
"bot",
"api",
"nodejs",
"bent"
"bent",
"botocrat"
],
"license": "MIT",
"dependencies": {
Expand Down
20 changes: 17 additions & 3 deletions src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,21 @@ const handleFormData = (request, interceptor) =>
}
return request(method, params)
}

const attachInterceptor = (interceptor, next) =>
interceptor
? (...params) => {
const handle = interceptor(...params)
return handle instanceof Promise
? handle
: next(...params)
}
: next

module.exports = (url, debug, interceptor) =>
handleResponse(
handleFormData(require('bent')(url, 'json', 'POST'), interceptor)
, debug)
attachInterceptor(
interceptor,
handleResponse(
handleFormData( require('bent')(url, 'json', 'POST') ),
debug)
)
48 changes: 37 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
const createClient = require('../src');
const fs = require('fs');
/* eslint-disable no-undef */
const createClient = require('../src')
const fs = require('fs')

if (!process.env.BOT_TOKEN) {
console.log(
'\n\nError!\nPlease provide bot token by environment variable. export BOT_TOKEN=123456789:BB...\n\n\n\n'
);
process.exit(1);
)
process.exit(1)
}
if (!process.env.TEST_CHAT_ID) {
console.log(
'\n\nError!\nPlease provide TEST_CHAT_ID by environment variable. export TEST_CHAT_ID=-12232\n\n\n\n'
);
process.exit(1);
)
process.exit(1)
}
const chat_id = process.env.TEST_CHAT_ID
const file_id = 'AgACAgQAAx0ESTL-FQACARdh-b9i4dboNlj6-j31Gf7x8vHhnQACeLcxG5nl0VPx5i_9jRdB_wEAAwIAA3kAAyME'
const client = createClient({
token: process.env.BOT_TOKEN,
debug: (err) => console.log(err)
});
})
const photoStream = fs.createReadStream(__dirname + '/file.jpg')
let test = (module.exports = {
me: client
Expand All @@ -40,8 +41,8 @@ let test = (module.exports = {
.getFile({file_id})
.then(client.download)
.then(([buffer, ext, size]) => {
fs.writeFileSync(__dirname + '/file.' + ext, buffer);
return true;
fs.writeFileSync(__dirname + '/file.' + ext, buffer)
return true
}),
uploadPhoto: () =>
client
Expand All @@ -53,6 +54,31 @@ let test = (module.exports = {
e.photo && e.photo instanceof Array
? true
: Promise.reject('Should upload photo')),
interceptor: () => new Promise((resolve, reject)=>{
const client2 = createClient({
token: process.env.BOT_TOKEN,
debug: (err) => console.log(err),
interceptor: (method, params) => {
(method === 'getMe')
? resolve()
: reject()
}
})
client2.getMe()
}),
interceptorIntervention: () => {
const client2 = createClient({
token: process.env.BOT_TOKEN,
debug: (err) => console.log(err, 2),
interceptor: async (method, params) => ({ username: 'test' })
})
return client2.getMe()
.then((res) => {
res.username === 'test'
? true
: Promise.reject(false)
})
},
uploadMediaGroup: async () =>
client
.sendMediaGroup({
Expand All @@ -73,5 +99,5 @@ let test = (module.exports = {
e instanceof Array && e[0].media_group_id
? true
: Promise.reject('Should upload media group')
),
});
)
})

0 comments on commit 646b8e4

Please sign in to comment.