Global Classes: Api
TBL provides several predefined classes. The most important one is Api, which works as a direct wrapper for the official Telegram Bot API.
Api methods are case insensitive. You can use both sendMessage or sendmessage.
1. Simple Usage
The simplest way to use Api is to call a method directly. No await is needed. The message will be sent in the background.
// Send a simple message
Api.sendMessage({ text: "Hello user!" })
// Send a photo
Api.sendPhoto({ photo: "https://example.com/cat.jpg", caption: "Cute cat" })
2. Using on_run Callback
You can pass on_run to any Api call to run another command when the call finishes. Useful if you want to continue logic in a separate command.
// Get bot info and continue in another command
Api.getMe({ on_run: "afterGetMe" })
// Inside "afterGetMe" command
if (options.ok) {
Api.sendMessage({ text: "Bot username: @" + options.result.username })
}
3. Using Async / Await
For sequential logic, you can use async/await. This lets you store results in variables and act on them immediately.
let me = await Api.getMe()
if (me.ok) {
Api.sendMessage({ text: "My bot id is " + me.result.id })
}
let sent = await Api.sendMessage({ text: "Hello again!" })
Bot.inspect(sent)
4. Chained Methods on Api.xxx Response
Every response object returned from Bot.sendMessage(), Api.sendMessage(), etc. now supports chained methods. These let you quickly react, edit, reply, pin, forward, or delete the message.
// Send a message and chain methods
let data = await Api.sendMessage({text: "Hello user!"})
await data.pin() // Pin the message
await data.react("👍") // Add reaction
await data.editText("Updated text") // Edit message text
await data.reply("Replied Message") // Reply to message
Supported Chained Methods
- editText(text, {...options})
- editCaption(caption, {...options})
- editMedia(media, {...options})
- editReplyMarkup(rm, {...options})
- delete()
- pin(dn = false)
- unpin()
- react(emoji, big = false)
📚 Source
This tutorial is based on the official TeleBotHost TBL Documentation. Visit their site for the most up-to-date information and advanced guides.