A comprehensive guide to building, using, and debugging custom JavaScript libraries in the TBL (Tele Bot Language) system. TBL supports both synchronous and asynchronous libraries and is designed to make Telegram bot scripting powerful, modular, and safe.
TBL is a flexible scripting framework used for creating Telegram bots. It allows defining custom libraries (Libs) which are isolated modules that can include variables, methods, and logic using standard JavaScript as well as TBL's custom scripting support.
- β Supports both sync & async libraries
- β
Full access to Telegram Bot API via
Api - β Errors and timeouts handled safely
- β Up to 10-second execution timeout for safety
- β
Built-in support for TBL Language:
User,Bot,chat, etc.
Each library must be placed in the /Libs directory as a .js file:
/Libs
βββ channelChecker.js
βββ mathUtils.js
βββ welcomeLib.js
// β
Correct
module.exports = {
method1: function() {},
method2: async function() {}
};
// β Incorrect - won't be loaded
exports = { method1: () => {} };TBL supports its own runtime language and built-in globals:
| Keyword | Meaning |
|---|---|
user |
Current user object |
chat |
Current chat object |
Bot |
Bot methods like Bot.sendMessage(...) |
Api |
Raw Telegram Bot API methods |
User, Global, bot |
Shortcuts to runtime contexts |
And more supported! You can use TBL variables, methods, and native functions within your libs.
You can define both sync and async methods in your library:
// Libs/mylib.js
module.exports = {
syncMethod: function(a, b) {
return a + b;
},
asyncMethod: async function(userId) {
return await Api.getChat({ chat_id: userId });
}
};Usage:
const sum = Libs.mylib.syncMethod(1, 2);
const chatInfo = await Libs.mylib.asyncMethod(user.id);// β WRONG
let data = Api.getChat({ chat_id: user.id });
// β
RIGHT
let data = await Api.getChat({ chat_id: user.id });// β WRONG
Api.sendMessage({ text: "Hi" }).then(...);
// β
RIGHT
await Api.sendMessage({ text: "Hi" });// β WRONG
function myFunc() {}
module.exports = myFunc;
// β
RIGHT
module.exports = {
myFunc: function() {}
};// β WRONG
return user.first_name; // might not exist
// β
RIGHT
module.exports = {
getName: function(user) {
return user?.first_name || "Guest";
}
};// β WRONG
async function send() {
let msg = await Api.sendMessage({ chat_id: 123, text: "hi" });
// forgot to return
}
// β
RIGHT
async function send() {
return await Api.sendMessage({ chat_id: 123, text: "hi" });
}module.exports = {
greet: function(name) {
if (typeof name !== 'string') return "Invalid name";
return "Hello, " + name;
}
};module.exports = {
safeSend: async function(chatId, text) {
try {
await Api.sendMessage({ chat_id: chatId, text });
} catch (e) {
Bot.sendMessage("Failed to send");
}
}
};module.exports = {
debugLog: function(data) {
Bot.inspect(data);
}
};// Libs/math.js
module.exports = {
add: (a, b) => a + b,
isEven: (n) => n % 2 === 0
};Usage:
let val = Libs.math.add(5, 10); // 15// Libs/greet.js
module.exports = {
welcome: async function(userId) {
return await Api.sendMessage({
chat_id: userId,
text: "π Welcome!"
});
}
};Usage:
await Libs.greet.welcome(user.id);| Type | Supported? | Notes |
|---|---|---|
| Async libs | β | Use await Libs.name.method() |
| Sync libs | β | Direct call: Libs.name.fn() |
| Return Data | β | Always return from async |
| Promise | β | only on async function |
then/catch |
β | Not supported in TBL |
module.exports = {
check: async function(userId, channels) {
// ...check logic
},
quick: async function(userId, channels) {
const res = await this.check(userId, channels);
return res.all_joined;
},
getBtn: function(channels) {
return channels.map(c => [{ text: `Join ${c}`, url: `https://t.me/${c.replace("@", "")}` }]);
}
};Use like:
await Libs.channel.check(user.id, ['@ch1', '@ch2']);TBL Libs are powerful, modular, and safe. Whether you're building a sync math helper or an async membership checker, follow the rules and format strictly.
If you hit errors:
- β
Check your
module.exports - β
Use
awaitproperly - β
Avoid chaining with
.then
Happy bot building! π€
We welcome contributions to the TBL Libs collection!
If youβve built a useful utility or helper library for Telegram bot development using TBL (Telegram Bot Language), you can share it with the community.
-
Create Your Library
- Write your lib in a new
.jsfile under/Libs/ - Export your functions properly via
module.exports = { ... }
- Write your lib in a new
-
Follow the Rules
- One file per library
- No external dependenciesβuse only TBL APIs and JavaScript
- Name the file and methods clearly
- Add brief inline comments if needed
-
Test Before Push
- Ensure your lib loads and works using
Libs.myLib.method() - Async methods must be used with
await
- Ensure your lib loads and works using
-
Submit a Pull Request
- Fork the repository
- Add your
.jsfile under/Libs - Submit a Pull Request with:
- A clear description
- Library purpose
- Optional usage notes
β¨ Letβs grow the TBL ecosystem together!
Submit your PRs and help other developers build faster.