Always running commands
Sometimes code execution is always required.

Master command

This command executed only when there are no others commands or on updates for Telegram bot.
Just use * in command name.
Please see all updates list here​
Example.
Command *
1
// you can track any message here
2
if(message){
3
Bot.sendMessage("Sorry, bot don't have this command: " + message);
4
return
5
}
6
​
7
// you can see all updated data by:
8
// Bot.inspect(request);
9
​
10
if(request.edit_date){
11
// user edited message
12
Bot.sendMessage("Text edited to:" + request.new_text);
13
// Please note:
14
// we have request.new_text not request.text here
15
// for backward compatibility
16
// request.text will be nil!
17
}
18
​
19
// chat title changed
20
if(request.new_chat_title){
21
Bot.sendMessage("New chat title is:" + request.new_chat_title);
22
}
23
​
24
// another possible updates in:
25
// https://core.telegram.org/bots/api#message
Copied!

BeforeAll and AfterAll commands

Code of this commands executed always before (and after) all others commands codes.
Example. You need add important alert in all commands. You can create only one BeforeAll command with code Bot.sendMessage("Important alert")
For BeforeAll command use @ in command name
For AfterAll command use @@ in command name
Please note. Only BJS for BeforeAll and AfterAll commands runned. No any answer and keyboard here.
You can share functions, variables and etc with BeforeAll and AfterAll commands. It is effective for common code parts.
1
// code for @ BeforeAll command
2
function myName(){
3
return "Peter"
4
}
Copied!
1
// code for /test command
2
Bot.sendMessage(
3
myName() // result will be "Peter"
4
)
5
​
6
// myName is defined in BeforeAll command
Copied!
Please note. If you need *, @, @@ as command names you can use it in aliases

Return methods.

return in BeforeAll command works also for all commands
If you have return in any command AfterAll commands do not executed

Example: Making ban system with BeforeAll command

In command BeforeAll: with @ name
1
badUsers = Bot.getProperty("badUsers", { list: {} })
2
​
3
if(badUsers.list[user.telegramid]){
4
Bot.sendMessage("You are blocked!");
5
return // this is worked for all command
6
// because it is in BeforeAll command
7
}
Copied!
In command /block:
1
tgID = 1111111; // any tgID for ban. You can pass it via message with wait for reply
2
badUsers = Bot.getProperty("badUsers", { list: {} });
3
badUsers.list[tgID] = true;
4
​
5
// for unban:
6
// badUsers.list[tgID] = false;
7
​
8
Bot.setProperty("badUsers", badUsers, "json");
9
​
10
Bot.sendMessage("User with TG id: " + tgID + " banned");
11
​
12
// You can also use hard block
13
// It is save your iterations:
14
// Bot.blockChat(chat.id);
15
​
16
// But with this BeforeAll will be also not working
Copied!
Last modified 23d ago