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

Commit d61d7f8

Browse files
committed
Better management of the /help default command
1 parent 45cc9f3 commit d61d7f8

File tree

6 files changed

+52
-28
lines changed

6 files changed

+52
-28
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Nodeogram is a simple yet complete Node.JS module for Telegram bots.
77
* **Promises**. The entire library is promise-based
88
* **Event based**. Nodeogram provides an event-based handling of updates, allowing you to interact more easily with messages, commands, callback and inline queries.
99

10+
Nodeogram is an open-source project available on `GitHub <https://github.com/ALCC01/nodeogram>`_ and `npm <https://www.npmjs.com/package/nodeogram>`_.
11+
1012
## Getting started
1113

1214
First, you'll need to install the module
@@ -25,7 +27,7 @@ bot.init();
2527
bot.on('message', message => message.reply("Hello World!"));
2628
```
2729

28-
And you are done.
30+
And you are done. You can see more extensive examples in the [library overview](https://dev.albertocoscia.me/nodeogram/quickstart.html)
2931

3032
## Documentation
3133
Please refer to the [Nodeogram documentation](https://dev.albertocoscia.me/nodeogram/)

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Once the package is installed, you can start working on your bot.
3333
bot.init();
3434
bot.on('message', message => message.reply("Hello World!"));
3535
36-
And you are done. You can more extensive examples in the :doc:`quickstart`
36+
And you are done. You can see more extensive examples in the :doc:`quickstart`
3737

3838
=======
3939
License

docs/objects/Bot.rst

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ This object represents a Telegram bot.
1919

2020
.. code-block:: javascript
2121
22-
{
23-
// File to which profiles should be saved
24-
profiles_path: __dirname +'/profiles.json',
25-
// Whether or not users should be saved
26-
saveUsers: true;
27-
// Whether or not chats should be saved
28-
saveChats: true
29-
}
22+
{
23+
// File to which profiles should be saved
24+
profiles_path: __dirname +'/profiles.json',
25+
// Whether or not users should be saved
26+
saveUsers: true,
27+
// Whether or not chats should be saved
28+
saveChats: true,
29+
// Whether or not the /help command should be registered by default
30+
enableHelp: true
31+
}
3032
3133
.. _init:
3234
.. js:function:: init()
@@ -39,11 +41,12 @@ This object represents a Telegram bot.
3941
shouldn't be called manually. The first call made by the bot will be with a -1 offset that will erase any backlog updates.
4042
Updates older than 2 seconds are also ignored. Updates are retrieved 100 at a time, with a 30 seconds timeout.
4143

42-
.. js:function:: command(command, description, callback)
44+
.. js:function:: command(command, description, hidden, callback)
4345

4446
:param string command: The command, without the opening /
45-
:param string description: <String> The command's description, will be used for the /help message
46-
:param function callback: <Function> This function will be called whenever the command is triggered with an array of arguments ``args`` along with the :doc:`Message` ``message`` responsible for triggering the command
47+
:param string description: The command's description, will be used for the default /help message
48+
:param boolean hidden: Set this to true in order to hide this command from the default /help message
49+
:param function callback: This function will be called whenever the command is triggered with an array of arguments ``args`` along with the :doc:`Message` ``message`` responsible for triggering the command
4750

4851
Registers a command handler for the specified ``command``, with the provided ``description`` and ``callback``, also
4952
adding the command to the /help message.

docs/quickstart.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,13 @@ the arguments provided.
8282
.. code-block:: javascript
8383
:linenos:
8484
85-
bot.command('echo', 'Echoes your voice', (args, message) => {
85+
bot.command('echo', 'Echoes your voice', false, (args, message) => {
8686
if (args[0] != '') message.reply(args.join(' '), {});
8787
});
8888
8989
This registers a new handler for the command ``/echo`` with the description *Echoes your voice* and acts as defined in
90-
the callback. Oh, did I mention that it also adds it automagically to the ``/help`` command?
90+
the callback. Oh, did I mention that it also adds it automagically to the ``/help`` command? If you don't want your users
91+
to know about the command, just make it a secret between you and the bot setting the third argument to ``true``.
9192

9293
****************
9394
Service messages

docs/style.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Nodeogram does not introduce intrusive error handling and as a result you should
2222
``catch`` function.
2323

2424
Nodeogram's functions can fail at any time - querying the API, attempting to send a message to a chat the
25-
bot was kicked out of, etc - and not handling errors properly might result in unexpected behaviours or worse - getting
25+
bot was kicked out, etc - and not handling errors properly might result in unexpected behaviours or worse - getting
2626
your bot or IP address tempbanned from Telegram's server could really ruin your day.
2727

2828
==============

lib/Bot.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ function Bot(token, options) {
2626
this.emitter = new events.EventEmitter();
2727
this.saveUsers = true;
2828
this.saveChats = true;
29+
this.enableHelp = true;
2930
extend(this, options);
3031

3132
this.handleAPIError = (err) => {
32-
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 : ""}`)}
33+
if (err.response) {
34+
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 : ""}`)
35+
}
3336
else console.log(err)
3437
};
3538

@@ -121,8 +124,8 @@ function Bot(token, options) {
121124

122125
};
123126

124-
this.command = (command, description, callback) => {
125-
this.commands.push({command: command, description: description, callback: callback});
127+
this.command = (command, description, hidden, callback) => {
128+
this.commands.push({command: command, description: description, callback: callback, hidden: hidden});
126129
};
127130

128131
this.getChat = (chat_id) => {
@@ -434,7 +437,9 @@ function Bot(token, options) {
434437
profiles.chats[message.chat.id].bot = undefined;
435438
edited = true;
436439
}
437-
if (edited) fs.writeFile(this.profiles_path, JSON.stringify(profiles), (err) => {if (err) console.log(err)})
440+
if (edited) fs.writeFile(this.profiles_path, JSON.stringify(profiles), (err) => {
441+
if (err) console.log(err)
442+
})
438443
};
439444

440445
this.broadcast = (filter, callback) => {
@@ -460,16 +465,29 @@ function Bot(token, options) {
460465
];
461466
if (this.profiles_path) {
462467
this.messageHandlers.push(this.handleProfiles);
463-
global.profiles = {users: {}, chats:{}};
464-
try {global.profiles = require(this.profiles_path)} catch (err) {}
468+
global.profiles = {users: {}, chats: {}};
469+
try {
470+
global.profiles = require(this.profiles_path)
471+
} catch (err) {
472+
}
465473
}
466-
this.command('help', 'Display help for this bot', (args, message) => {
467-
var text = 'Commands:\n';
468-
this.commands.forEach((entry) => {
469-
text += `\n/${entry.command} - ${entry.description}`
474+
if (this.enableHelp) {
475+
this.command('help', 'Display help for this bot', false, (args, message) => {
476+
var text = 'Commands:\n';
477+
var total = 0;
478+
this.commands.forEach((entry) => {
479+
if (!entry.hidden) {
480+
text += `\n/${entry.command} - ${entry.description}`;
481+
total++;
482+
}
483+
});
484+
if (total == 1) {
485+
// Look like only the help command is registered
486+
text = 'There are no commands for this bot!';
487+
}
488+
message.reply(text, {});
470489
});
471-
message.reply(text, {});
472-
});
490+
}
473491
this.getUpdates();
474492
};
475493

0 commit comments

Comments
 (0)