How to...
It is Bot JavaScript. It's an ordinary Java with some inserts.
Usually do not need something complicated for developing bots. You can read a couple of articles about JS: https://www.w3schools.com/js/js_syntax.asp or https://en.wikipedia.org/wiki/JavaScript_syntax
You can use bot answer and
Bot.sendMessage("ANY message").
or use two functions: BJS code:
Bot.sendMessage("ANY message");
Bot.sendMessage("Other any message");
Telegram have markdown for text formating.
samples:
\n - new line (multi-line text also allowed)
*bold text*
_ italics _
[text](URL)
`inline fixed-width code`
```text
pre-formatted fixed-width code block
So if you have incorrect markdown - you have this warning. Sample text with incorrect markdown:
bot_name
price 1*
"user`s" - wrong. Use "user's"!
BJS code:
var buttons = [
{title: "Go to Google", url: "https://google.com"},
{title: "Call command for Button1", command: "/touch Button1" },
{title: "Call command for Button2", command: "/touch Button2" }
];
Bot.sendInlineKeyboard(buttons, "Please make a choice. After that, another command `/touch` will be started with parameters");
buttons
- it is array. It contains buttons. Each button is object with title
(required), url
or command
.Button must have
url
or command
. url
- any link.command
- this command will be executed after button pressing. Can contain parameters through a space. Command
can not be more then 64 bytes.You need pass options parameter with
is_reply
Bot.sendMessage("It is reply message", {is_reply: true} );
For any message in chat use:
reply_to_message_id
Bot.sendMessage("It is reply message", {reply_to_message_id: request.message_id } );
Also you can retry with keyboard or inline keyboard too
// retry for last message
Bot.sendMessage("It is reply message", {is_reply: true } );
// or for any message
Bot.sendMessage("It is reply message", {reply_to_message_id: request.message_id } );
Bot.sendInlineKeyboard(
[ {title: "google", url: "http://google.com" }, {title: "other command", commnad: "/othercommand"} ],
"Please make a choice.",
{reply_to_message_id: request.message_id }
)
You can retry for any message from chat.
You can see it with
inspect
function: Bot.sendMessage( inspect(request) );
// or
Bot.inspect(request) // if have issue with markdown
BJS code:
function canRun(){
var last_run_at = User.getProperty("last_run_at");
if(!last_run_at){ return true }
var minutes = (Date.now() - last_run_at) /1000/60;
var minutes_in_day = 24*60
if(minutes < minutes_in_day){
Bot.sendMessage("Please return later. ")
return
}
return true;
}
if(!canRun()){ return }
User.setProperty("last_run_at", Date.now(), "integer");
// your code here:
// ...
Unfortunately, this is not supported by the Telegram API. But you can send link to chat: answer:
[Open](http://example.com)
You can use a group for commands. Then such commands can be started only by those users who are in this group. You can assign users to a group through BJS with password verification.
Example Bot:
password?
User:
12345
Bot:
Welcome, member!
In this example, the user must enter the correct password. After that, the group
Members
is setted and user can execute all commands of this group. If the password is not correct, a error message is displayed.Bot: answer:
password?
need_reply: true
BJS code:
if(message=="12345"){
User.addToGroup('Members');
Bot.sendMessage("Welcome, member!");
}else{
Bot.sendMessage("Password incorrect");
}
1. You can store Last Active time for user in bot's property:
command
tracking
this is an invisible command for users. It is run from other commands only
BJS code:
if(chat.chat_type=="private"){
// track only private chats
total_users = Bot.getProperty("total_users");
if(!total_users){ total_users = 0 }
Bot.setProperty("total_users", total_users+1, "integer");
var propLastActiveName = "user" + String(total_users) + "_last_active_at";
var propChatIdName = "chat" + String(i) + "_id";
Bot.setProperty(propLastActiveName, (new Date), "datetime");
Bot.setProperty(propChatIdName, chat.chatid, "string");
}
2. In others commands you need call
tracking
command Bjs code: Bot.runCommand("tracking");
// your any code here
Please note. This code is needed in all commands of your bot.
3. Automatically message command. Set auto retry time for it: 24 hours
BJS:
total_users = Bot.getProperty("total_users");
for(var i=0; i<total_users; i++){
var propLastActiveName = "user" + String(i) + "_last_active_at";
var last_active_at = Bot.getProperty(propLastActiveName);
var duration = (new Date) - last_active_at;
var duration_in_minutes = duration / 1000 / 60
if(duration_in_minutes>60*24){
// not active more than 24 hours
var propChatIdName = "chat" + String(i) + "_id";
var chatId = Bot.getProperty(propChatIdName);
Bot.sendMessageToChatWithId(chatId, "Hello! How are you?");
}
}
You can use Coinmarketcap API. For example page https://api.coinmarketcap.com/v2/ticker/1/ have information about Bitcoin. We need load it with BJS.
BJS:
->(https://api.coinmarketcap.com/v2/ticker/1/)
var result = JSON.parse(content);
var BTC_USD_Price = result.quotes.USD.price;
Bot.sendMessage("Current Bitcoin price: " + String(BTC_USD_Price) + " $");

You must update the keyboard every time the value changes. To do this, send the keyboard with the command. Most likely, this should be done in several commands.
BJS:
var balance = User.getProperty("balance"); // or your code here
Bot.sendKeyboard(String(balance) + ",\nHelp, Contacts" );
Use ResLib for any resources
You need create command "⚡Balance:" (without space) or
"⚡"
if you have space beetween "⚡" and "Balance"So after button pressing:
- text "⚡Balance: X BTC⚡" will be sended to chat
- command "Balance:" will be executed
- params "X BTC⚡" will be passed to BJS
Bot can send message to this user in private. So user need to start this bot in private chat in first.
Then on group chat:
Bot.sendMessageToChatWithId(user.telegramid, "BOT ANSWER")
Also it is possible show alert message for user in group chat with answerCallbackQuery after inline button pressing.
User must attach his location to chat.
Need command with "Wait for answer" option (or you can use Master command and catch location)
BJS:
// you can inspect all data
// Bot.inspect(request);
let location = request.location
if(!location){
Bot.sendMessage("Please send location");
return
}
Bot.sendMessage(
"Your location is:\n longitude " +
location.longitude +
"\n latitude: " +
location.latitude
)
Example - command
/askName
have wait for reply. Need to cancel it.Add keyboard to this command: keyboard "Cancel" (also can be "Back")
BJS:
// exit on Cancel or Back button
if((message=="Cancel")||(message=="Back")){
return // exit
}
// get name here
name = message;
Bot.sendMessage("Hello, " + name);
BJS:
Api.answerCallbackQuery({
callback_query_id: request.id,
text: "My alert",
show_alert: true // or false - for alert on top
})
Command:
/isJoined
chanell = "@MyChanell"
Api.getChatMember({
chat_id: chanell,
user_id: user.telegramid,
on_result :"/onCheckJoin"
})
Command
/onCheckJoin
let status = options.result.status;
var isJoined = (
(status == "member")||
(status == "administrator")||
(status == "creator")
)
if(isJoined){
Bot.sendMessage("You are chanell member!");
}else{
Bot.sendMessage("You are NOT chanell member!");
}
Last modified 1yr ago