Bot API
Bot API is a high-level HTTP interface to the Telegram API that makes it easy to develop bots. Under the hood, Bot API uses TDLib (which in turn uses MTProto API).
While the Bot API can only be used to work with bot accounts, the MTProto API can be used to work with both bot and user accounts. If you need to work with MTProto, we recommend you the mtcute library.
Calling the Bot API
You can call Telegram Bot API methods via bot.api
or via the shorthand methods of contexts (for example, context.send
is sendMessage
shorthand)
const response = await bot.api.sendMessage({
//
//
chat_id: "@gramio_forum",
text: "some text",
});
bot.on("message", (context) =>
context.send("This message will be sent to the current chat")
);
Suppressing errors
It can be convenient to handle an error on the spot without using try/catch blocks. That's why the suppress
argument was created, which you can use in any API method.
const response = await bot.api.sendMessage({
//
//
suppress: true,
chat_id: "@not_found",
text: "Suppressed method",
});
if (response instanceof TelegramError)
console.error("sendMessage returns an error...");
else console.log("Message has been sent successfully");
Types
GramIO re-exports @gramio/types (Code-generated and Auto-published Telegram Bot API types).
import type { APIMethodParams, APIMethodReturn } from "gramio";
type SendMessageParams = APIMethodParams<"sendMessage">;
//
type GetMeReturn = APIMethodReturn<"getMe">;
//
For example you can use it in your custom function.
function myCustomSend(params: APIMethodParams<"sendMessage">) {
params;
}
Types for suppressed method
import type {
SuppressedAPIMethodParams,
SuppressedAPIMethodReturn,
} from "gramio";
type SendMessageParams = SuppressedAPIMethodParams<"sendMessage">;
//
type GetMeReturn = SuppressedAPIMethodReturn<"getMe">;
//