Api functions
Api functions it all functions from https://core.telegram.org/bots/api​
You can use it with BJS.

Example 1. Send audio to current chat

1
Api.sendAudio({
2
audio: "https://www.bensound.org/bensound-music/bensound-funnysong.mp3"
3
});
4
​
Copied!
send audio to other chat:
1
Api.sendAudio({
2
chat_id: 5515411,
3
audio: "https://www.bensound.org/bensound-music/bensound-funnysong.mp3"
4
});
5
​
Copied!
​
You can pass allowed parameters. For example for sendAudio it can be title and disable_notification
1
Api.sendAudio({
2
audio: "https://www.bensound.org/bensound-music/bensound-funnysong.mp3"
3
title: "test audio",
4
disable_notification: true
5
});
Copied!

Example 2. Send photo with inline keyboard

1
// see all parameters in https://core.telegram.org/bots/api#sendphoto
2
Api.sendPhoto({
3
photo: "https://cataas.com/cat", // it is picture!
4
caption: "Test photo",
5
​
6
reply_markup: { inline_keyboard: [
7
// line 1
8
[
9
// open the link on button pressing
10
{ text: "button1", url: "http://example.com" },
11
// run command /onButton2 on button pressing
12
{ text: "button2", callback_data: "/onButton2" }
13
],
14
// line 2
15
[
16
// see all params in
17
// https://core.telegram.org/bots/api#inlinekeyboardbutton
18
{ text: "button3", callback_data: "/onButton3" }
19
]
20
]}
21
});
Copied!

Get methods

You can call Api get methods (and others methods too). Need pass on_result key.
For example get all user's profile photos:

Command /get

1
Api.getUserProfilePhotos({
2
user_id: user.telegramid,
3
4
// this command will be executed after getting photos
5
on_result: "onGetProfilePhotos"
6
});
Copied!

​

Command onGetProfilePhotos

1
// you can inspect result:
2
// Bot.inspect(options)
3
​
4
if(!options.ok){
5
return Bot.sendMessage("Error!");
6
}
7
​
8
if(options.result.total_count==0){
9
return Bot.sendMessage("You have no photos in profile")
10
}
11
​
12
let photos = options.result.photos;
13
for(let i in photos){
14
Api.sendPhoto( { photo: photos[i][0].file_id } );
15
}
16
​
Copied!

Error handling

It is possible to capture error with on_error param
1
Api.sendAudio({
2
audio: "https://www.bensound.org/bensound-music/bensound-funnysong.mp3",
3
on_error: "/on_error"
4
});
Copied!
In command on_error:
1
Bot.sendMessage("We have error with sending audio");
2
Bot.inspect(options)
Copied!
Last modified 2yr ago