Skip to content
This repository was archived by the owner on Aug 25, 2023. It is now read-only.

Commit 89ef958

Browse files
committed
Added profiles storage and Bot.broadcast
1 parent 44f8263 commit 89ef958

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

docs/objects/Bot.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@ This object represents a Telegram bot.
77
The Bot object and its attributes should remain unchanged after it has been initialized.
88

99

10-
.. js:class:: Bot(token)
10+
.. js:class:: Bot(token[, options])
1111

1212
:param string token: A valid token for the Telegram Bot API
13+
:param object options: *Optional*
1314
:returns: A Bot object
1415

1516
Creates a new Bot object and fetches basic information about it (aysncronous call to Telegram's ``getMe`` method).
17+
Valid ``options`` are:
18+
19+
.. code-block:: javascript
20+
21+
{
22+
profiles_path: __dirname +'/profiles.json'
23+
}
1624

1725
.. _init:
1826
.. js:function:: init()
@@ -215,4 +223,9 @@ This object represents a Telegram bot.
215223
:param string action: Must be one of the following: ``typing``, ``upload_photo``, ``record_video``, ``upload_video``, ``record_audio``, ``upload_audio``, ``upload_document``, ``find_location``
216224
:returns: A promise that resolves to the response (according to Telegram, true on success).
217225

226+
.. js:function:: broadcast(filter, callback)
227+
228+
:param string filter: Must be one of the following: ``private``, ``group``, ``supergroup``, ``channel`` or ``all``. Can also be a function that accepts one argument (a :doc:`Chat` object without functions) and returns a boolean value
229+
:param function callback: A function to which is provided a single argument, a promise that resolves to a :doc:`Chat` object.
218230

231+
Iterates a function through all of chats stored in the profiles storage.

lib/Bot.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
const request = require('superagent-promise')(require('superagent'), Promise),
3+
extend = require('util')._extend,
34
Chat = require('./Chat.js'),
45
User = require('./User.js'),
56
Message = require('./Message.js'),
@@ -13,7 +14,7 @@ const request = require('superagent-promise')(require('superagent'), Promise),
1314
events = require('events'),
1415
endpoint = 'https://api.telegram.org';
1516

16-
function Bot(token) {
17+
function Bot(token, options) {
1718
this.token = token;
1819
this.lastUpdate = -1;
1920
this.limit = 100;
@@ -23,6 +24,7 @@ function Bot(token) {
2324
this.messageHandlers = [];
2425
this.me = {};
2526
this.emitter = new events.EventEmitter();
27+
extend(this, options);
2628

2729
this.handleAPIError = (err) => {
2830
if (err.response) { console.log(`An error occurred while querying the Telegram API - ${err.response.text ? JSON.parse(err.response.text).error_code : ""} ${err.response.text ? JSON.parse(err.response.text).description : ""}`)}
@@ -414,12 +416,47 @@ function Bot(token) {
414416
this.emitter.emit('message', message);
415417
};
416418

419+
this.handleProfiles = (message) => {
420+
var edited = false;
421+
if (!profiles.users[message.from.id]) {
422+
profiles.users[message.from.id] = message.from;
423+
profiles.users[message.from.id].bot = undefined;
424+
edited = true;
425+
}
426+
if (!profiles.chats[message.chat.id]) {
427+
profiles.chats[message.chat.id] = message.chat;
428+
profiles.chats[message.chat.id].bot = undefined;
429+
edited = true;
430+
}
431+
if (edited) fs.writeFile(this.profiles_path, JSON.stringify(profiles), (err) => {console.log(err)})
432+
};
433+
434+
this.broadcast = (filter, callback) => {
435+
if (!this.profiles_path) throw "Profiles path is not defined";
436+
if (profiles.chats) {
437+
for (var chat in profiles.chats) {
438+
if (profiles.chats.hasOwnProperty(chat)) {
439+
chat = profiles.chats[chat];
440+
if (typeof filter == 'function') {
441+
if (filter(chat)) callback(this.getChat(chat.id));
442+
}
443+
else if (chat.type == filter || filter == 'all') callback(this.getChat(chat.id))
444+
}
445+
}
446+
}
447+
};
448+
417449
this.init = () => {
418450
if (!this.token) throw "Invalid token!";
419451
this.messageHandlers = [
420452
this.handleCommands,
421453
this.handleMessage
422454
];
455+
if (this.profiles_path) {
456+
this.messageHandlers.push(this.handleProfiles);
457+
global.profiles = {users: {}, chats:{}};
458+
try {global.profiles = require(this.profiles_path)} catch (err) {}
459+
}
423460
this.command('help', 'Display help for this bot', (args, message) => {
424461
var text = 'Commands:\n';
425462
this.commands.forEach((entry) => {

0 commit comments

Comments
 (0)