Skip to content

Commit 3b2d7fc

Browse files
committed
gramscript stable v1.0.3
1 parent 4db6725 commit 3b2d7fc

11 files changed

+6114
-0
lines changed

package-lock.json

Lines changed: 5620 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/askUser.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Get direct answers from user.
3+
*/
4+
5+
const userList = {};
6+
7+
module.exports = {
8+
9+
id: 'askUser',
10+
defaultConfig: {
11+
messageTypes: ['text']
12+
},
13+
14+
plugin(bot, pluginConfig) {
15+
16+
const indx = pluginConfig.messageTypes.indexOf('*');
17+
if (indx > -1) {
18+
console.error('ERROR using askUser plugin: type \'*\' is not allowed, it cause a bug. removing');
19+
pluginConfig.messageTypes.splice(indx, 1);
20+
}
21+
if (pluginConfig.messageTypes.length === 0) {
22+
console.error('ERROR using askUser plugin: you must specify at least one valid type. adding type \'text\'');
23+
pluginConfig.messageTypes.push('text');
24+
}
25+
// On every message
26+
bot.on(pluginConfig.messageTypes, (msg, props) => {
27+
28+
const id = msg.chat.id;
29+
const ask = userList[id];
30+
31+
// If no question, then it's a regular message
32+
if (!ask) return;
33+
34+
// Delete user from list and send custom event
35+
delete userList[id];
36+
bot.event('ask.' + ask, msg, props);
37+
38+
});
39+
40+
// Before call sendMessage method
41+
bot.on('sendMessage', (args) => {
42+
43+
const id = args[0];
44+
const opt = args[2] || {};
45+
46+
const ask = opt.ask;
47+
48+
// If "ask" in options, add user to list
49+
if (ask) userList[id] = ask;
50+
51+
});
52+
53+
}
54+
};

plugins/botan.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Advanced analytics for your Telegram bot - http://botan.io
3+
Requires "botanio" npm package.
4+
*/
5+
6+
module.exports = {
7+
8+
id: 'botan',
9+
defaultConfig: null,
10+
11+
plugin(bot, config) {
12+
13+
// Check AppMetrika key
14+
const token = config;
15+
16+
if (token) {
17+
18+
// Require botanio
19+
const botan = require('botanio')(token);
20+
21+
// Track every type of message
22+
bot.on('*', (msg, props) => botan.track(msg, props.type));
23+
24+
} else {
25+
console.error('[botan] no token key');
26+
}
27+
28+
}
29+
30+
};

plugins/commandButton.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Use commands in buttons.
3+
*/
4+
5+
module.exports = {
6+
7+
id: 'commandButton',
8+
defaultConfig: {
9+
regExpEvents: true
10+
},
11+
12+
plugin(bot, pluginConfig) {
13+
14+
const {regExpEvents} = pluginConfig;
15+
16+
bot.on('callbackQuery', (msg, props) => {
17+
18+
let isAnswered = false;
19+
let promise = Promise.resolve();
20+
21+
const cmd = msg.data;
22+
23+
if (cmd.charAt(0) == '/') {
24+
answerCallback();
25+
promise = bot.event(cmd, msg, props);
26+
}
27+
28+
if (regExpEvents) {
29+
for (let eventName of bot.eventList.keys()) {
30+
if (eventName instanceof RegExp) {
31+
const match = cmd.match(eventName);
32+
if (match) {
33+
answerCallback();
34+
props.match = match;
35+
promise = promise.then(() => bot.event(eventName, msg, props));
36+
}
37+
}
38+
}
39+
}
40+
41+
return promise;
42+
43+
function answerCallback() {
44+
if (!isAnswered) {
45+
bot.answerCallbackQuery(msg.id);
46+
isAnswered = true;
47+
}
48+
}
49+
50+
});
51+
52+
}
53+
54+
};

plugins/floodProtection.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Simple flood protection plugin.
3+
Note: Received Telegram message time accuracy is one second.
4+
*/
5+
6+
const userList = {};
7+
8+
// Export bot module
9+
module.exports = {
10+
11+
id: 'floodProtection',
12+
defaultConfig: {
13+
interval: 1,
14+
message: 'Too many messages, relax!'
15+
},
16+
17+
plugin(bot, pluginConfig) {
18+
19+
const interval = Number(pluginConfig.interval) || 1;
20+
const text = pluginConfig.message;
21+
22+
bot.mod('message', (data) => {
23+
24+
const msg = data.message;
25+
const id = msg.from.id;
26+
const user = userList[id];
27+
const now = new Date(msg.date);
28+
29+
if (user) {
30+
31+
const diff = now - user.lastTime;
32+
user.lastTime = now;
33+
34+
if (diff <= interval) {
35+
36+
if (!user.flood) {
37+
if (text) bot.sendMessage(id, text);
38+
user.flood = true;
39+
}
40+
41+
data.message = {};
42+
43+
} else {
44+
user.flood = false;
45+
}
46+
47+
} else {
48+
userList[id] = {lastTime: now};
49+
}
50+
51+
return data;
52+
53+
});
54+
55+
}
56+
};

plugins/ignoreMessagesBeforeStart.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const startDate = {};
2+
3+
module.exports = {
4+
5+
id: 'ignoreMessagesBeforeStart',
6+
7+
defaultConfig: {
8+
start: true
9+
},
10+
11+
plugin(bot, config) {
12+
13+
bot.on(config.start ? ['start', '/start'] : 'start', (msg) => {
14+
let userId = 'main_bot';
15+
if (msg && msg.from) {
16+
userId = msg.from.id;
17+
}
18+
startDate[userId] = Date.now();
19+
});
20+
21+
bot.mod('message', (data) => {
22+
const {date, from} = data.message;
23+
if (from && (date * 1000 < startDate[from.id] || date * 1000 < startDate.main_bot)) {
24+
data.message = {};
25+
}
26+
27+
return data;
28+
});
29+
}
30+
};

plugins/namedButtons.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Named buttons which triggers bot commands.
3+
*/
4+
5+
module.exports = {
6+
7+
id: 'namedButtons',
8+
defaultConfig: {
9+
buttons: {
10+
// myButton: {
11+
// label: '😄 My Button Name',
12+
// command: '/myBotCommand'
13+
// }
14+
}
15+
},
16+
17+
plugin(bot, pluginConfig) {
18+
19+
const buttons = pluginConfig.buttons || {};
20+
21+
bot.on('text', (msg, props) => {
22+
const text = msg.text;
23+
for (let buttonId in buttons) {
24+
const button = buttons[buttonId];
25+
if (button.label === text) {
26+
return bot.event(button.command, msg, props);
27+
}
28+
}
29+
30+
});
31+
32+
}
33+
34+
};

plugins/permissions.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module.exports = {
2+
3+
id: 'permissions',
4+
defaultConfig: {
5+
allowedUserIds: [],
6+
allowedChannels: [],
7+
message: '⛔ you are not authorized'
8+
},
9+
10+
plugin(bot, pluginConfig) {
11+
12+
bot.mod('message', (data) => {
13+
if (data.message.chat) {
14+
if (data.message.chat.type === 'channel') {
15+
const chatId = data.message.chat.id;
16+
if (!pluginConfig.allowedChannels.includes(chatId)) {
17+
data.message = {};
18+
bot.sendMessage(chatId, pluginConfig.message);
19+
}
20+
} else {
21+
const userId = data.message.from.id;
22+
if (!pluginConfig.allowedUserIds.includes(userId)) {
23+
data.message = {};
24+
bot.sendMessage(userId, pluginConfig.message);
25+
}
26+
}
27+
}
28+
29+
return data;
30+
});
31+
}
32+
};

plugins/regExpMessage.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Adds RegExp support to text event messages.
3+
*/
4+
5+
module.exports = {
6+
7+
id: 'regExpMessage',
8+
9+
plugin(bot) {
10+
11+
bot.mod('text', (data) => {
12+
const {message, props} = data;
13+
const text = message.text;
14+
15+
let promise = Promise.resolve();
16+
17+
for (let eventType of bot.eventList.keys()) {
18+
if (eventType instanceof RegExp) {
19+
const match = text.match(eventType);
20+
if (match) {
21+
props.match = match;
22+
promise = promise.then(() => bot.event(eventType, message, props));
23+
}
24+
}
25+
}
26+
27+
data.promise = promise;
28+
29+
return data;
30+
});
31+
32+
}
33+
34+
};

0 commit comments

Comments
 (0)