Skip to content

Commit d4915f0

Browse files
committed
Initial commit
0 parents  commit d4915f0

File tree

7 files changed

+204
-0
lines changed

7 files changed

+204
-0
lines changed

Diff for: .eslintrc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "airbnb",
3+
"rules": {
4+
"comma-dangle": 0
5+
}
6+
}

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.idea

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# node-telegram-bot-api-middleware

Diff for: bot-test.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const TelegramBot = require('node-telegram-bot-api');
2+
3+
const bot = new TelegramBot('93111836:AAFAO-7dFnbDwuUnQENy-GXl7RgSCSp7etY', {
4+
polling: true
5+
});

Diff for: index.js

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const co = require('co');
2+
3+
/**
4+
* Default context object to start with
5+
*
6+
* @type {{middlewares: Array}}
7+
*/
8+
const propertiesObject = {
9+
middlewares: []
10+
};
11+
12+
function defaultMiddleware(msg) {
13+
this.msg = msg;
14+
this.shouldStop = false;
15+
16+
this.stop = () => {
17+
this.shouldStop = true;
18+
};
19+
}
20+
21+
const botCallback = function botCallback() {
22+
const args = arguments;
23+
24+
// Check if there is msg.chat.id so caller is message to bot
25+
if (!args.length) {
26+
console.error(
27+
'[node-telegram-bot-api-middleware]: No arguments passed ' +
28+
'to a function used in bot event callback'
29+
);
30+
return;
31+
}
32+
33+
if (typeof args[0].chat === 'undefined') {
34+
console.error(
35+
'[node-telegram-bot-api-middleware]: Chat id not ' +
36+
'defined in arguments. Not executing middlewares'
37+
);
38+
39+
return;
40+
}
41+
42+
if (typeof args[0].chat.id === 'undefined') {
43+
console.error(
44+
'[node-telegram-bot-api-middleware]: There is no ' +
45+
'visible chat id in chat in arguments. Not executing middlewares'
46+
);
47+
return;
48+
}
49+
50+
const context = {
51+
msg: args[0]
52+
};
53+
54+
this.middlewares.unshift(defaultMiddleware);
55+
56+
return co(function* executeMiddlewares() {
57+
for (let i = 0, size = this.middlewares.length; i < size; i++) {
58+
if (context.shouldStop) {
59+
return;
60+
}
61+
62+
const middleware = co.wrap(this.middlewares[i]);
63+
64+
yield middleware.apply(context, args);
65+
}
66+
}.bind(this));
67+
};
68+
69+
function copyContextWithConcatenation(oldContext) {
70+
return {
71+
middlewares: oldContext.middlewares.slice()
72+
};
73+
}
74+
75+
/**
76+
* @param middleware
77+
* @returns {function(this:{middlewares})}
78+
*/
79+
const use = function use(middleware) {
80+
/**
81+
* @type {Object} [middlewares]
82+
*/
83+
const copy = copyContextWithConcatenation(this);
84+
85+
copy.middlewares.push(middleware);
86+
87+
const callback = botCallback.bind(copy);
88+
89+
callback.use = use.bind(copy);
90+
91+
return callback;
92+
}.bind(propertiesObject);
93+
94+
exports.use = use;

Diff for: package.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "node-telegram-bot-api-middleware",
3+
"version": "0.1.0",
4+
"description": "Package that helps using middleware with generators in node-telegram-bot-api",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"devDependencies": {
12+
"chai": "^3.5.0",
13+
"co-mocha": "^1.1.2",
14+
"eslint": "^2.10.2",
15+
"eslint-config-airbnb": "^9.0.1",
16+
"eslint-plugin-import": "^1.8.0",
17+
"eslint-plugin-jsx-a11y": "^1.2.0",
18+
"eslint-plugin-react": "^5.1.1",
19+
"mocha": "^2.4.5",
20+
"node-telegram-bot-api": "^0.21.1"
21+
},
22+
"dependencies": {
23+
"co": "^4.6.0"
24+
}
25+
}

Diff for: tests.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const expect = require('chai').expect;
2+
const use = require('./index').use;
3+
require('co-mocha');
4+
5+
const botArgumentsMock = {
6+
chat: {
7+
id: 1
8+
}
9+
};
10+
11+
describe('basic middleware usage', () => {
12+
it('should return function', () => {
13+
expect(use(() => {})).to.be.a('function');
14+
});
15+
16+
const integratedValues = [];
17+
18+
it('should execute middlewares in proper order', function* () {
19+
yield use(() => {
20+
integratedValues.push('A');
21+
}).use(() => {
22+
integratedValues.push('B');
23+
}).use(function* gen1() {
24+
integratedValues.push('C');
25+
}).use(function* gen2() {
26+
integratedValues.push('D');
27+
})(botArgumentsMock);
28+
29+
expect(integratedValues.join('')).to.equal('ABCD');
30+
});
31+
32+
it('should have properties from default middleware', function* () {
33+
let thereIsMsg = false;
34+
yield use(function () {
35+
thereIsMsg = typeof this.msg !== 'undefined';
36+
})(botArgumentsMock);
37+
38+
expect(thereIsMsg).to.equal(true);
39+
});
40+
41+
it('should have new middlewares and not have old used, when using use() from scratch', function* () {
42+
yield use(() => {
43+
integratedValues.push('E');
44+
})(botArgumentsMock);
45+
46+
// If test result would be false - value would be ABCDABCDE
47+
expect(integratedValues.join('')).to.equal('ABCDE');
48+
});
49+
50+
it('should stop executing middleware when .stop() is used', function* () {
51+
const values = [];
52+
53+
yield use(function() {
54+
this.stop();
55+
}).use(() => {
56+
values.push('A');
57+
})(botArgumentsMock);
58+
59+
expect(values).to.be.empty;
60+
61+
yield use(function() {
62+
values.push('A');
63+
}).use(function() {
64+
this.stop();
65+
}).use(function() {
66+
values.push('B');
67+
})(botArgumentsMock);
68+
69+
expect(values.join('')).to.equal('A');
70+
});
71+
});

0 commit comments

Comments
 (0)