Skip to main content

Actions

The ballerinax/telegram package exposes the following client:

ClientPurpose
ClientMessaging, chat management, callback/inline query answers, file metadata/download, and webhook management on the Telegram Bot API.

Client​

Configuration​

FieldTypeDefaultDescription
accessTokenstringRequiredThe bot access token issued by @BotFather; embedded in every request's resource path.
httpVersionhttp:HttpVersionhttp:HTTP_2_0The HTTP version understood by the client.
timeoutdecimal30The maximum time to wait (in seconds) for a response.
forwardedstring"disable"The choice of setting forwarded/x-forwarded header.
poolConfighttp:PoolConfiguration()Configurations associated with request pooling.
cachehttp:CacheConfigHTTP caching-related configurations.

Initializing the client​

import ballerinax/telegram;

configurable string accessToken = ?;

telegram:Client telegramClient = check new ({accessToken});

Operations​

Messaging​

sendMessage

Sends a text message to a chat.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.
textstringYesThe message text.
optionsSendMessageOptionsNoOptional fields such as parse_mode, reply_markup, and disable_notification.

Returns: Message|Error

Sample code:

telegram:Message sent = check telegramClient->sendMessage(chatId, "Hello from Ballerina!");

Sample response:

{
"message_id": 42,
"date": 1735689600,
"chat": {"id": 123456789, "type": "private"},
"text": "Hello from Ballerina!"
}
sendPhoto

Sends a photo by URL or as uploaded bytes.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.
photostring|byte[]YesA photo URL, file_id, or raw bytes to upload.
optionsSendPhotoOptionsNoOptional fields such as caption.

Returns: Message|Error

Sample code:

telegram:Message photo = check telegramClient->sendPhoto(chatId, "https://example.com/photo.jpg");
sendVideo

Sends a video by URL or as uploaded bytes.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.
videostring|byte[]YesA video URL, file_id, or raw bytes to upload.
optionsSendVideoOptionsNoOptional fields such as duration, width, height, and caption.

Returns: Message|Error

Sample code:

telegram:Message sent = check telegramClient->sendVideo(chatId, "https://example.com/video.mp4");
sendAudio

Sends an audio file.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.
audiostring|byte[]YesAn audio URL, file_id, or raw bytes to upload.
optionsSendAudioOptionsNoOptional fields such as duration, performer, and title.

Returns: Message|Error

Sample code:

telegram:Message sent = check telegramClient->sendAudio(chatId, "https://example.com/audio.mp3");
sendDocument

Sends a general file.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.
documentstring|byte[]YesA document URL, file_id, or raw bytes to upload.
optionsSendDocumentOptionsNoOptional fields such as caption and disable_content_type_detection.

Returns: Message|Error

Sample code:

telegram:Message sent = check telegramClient->sendDocument(chatId, "https://example.com/report.pdf");
sendAnimation

Sends an animation (GIF or soundless MP4).

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.
animationstring|byte[]YesAn animation URL, file_id, or raw bytes to upload.
optionsSendAnimationOptionsNoOptional fields such as duration, width, height, and caption.

Returns: Message|Error

Sample code:

telegram:Message sent = check telegramClient->sendAnimation(chatId, "https://example.com/clip.gif");
sendSticker

Sends a sticker. Telegram stickers have no caption.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.
stickerstring|byte[]YesA sticker URL, file_id, or raw bytes to upload.
optionsSendStickerOptionsNoOptional fields such as emoji (uploads only).

Returns: Message|Error

Sample code:

telegram:Message sent = check telegramClient->sendSticker(chatId, stickerFileId);
sendLocation

Sends a point on the map.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.
latitudedecimalYesThe location's latitude.
longitudedecimalYesThe location's longitude.
optionsSendLocationOptionsNoOptional fields such as live_period and heading, for live locations.

Returns: Message|Error

Sample code:

telegram:Message sent = check telegramClient->sendLocation(chatId, 37.7749, -122.4194);
sendMediaGroup

Sends a group of photos, videos, documents, or audio files as an album.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.
mediaInputMedia[]YesThe media items to send, 2-10 items; file_id/URL strings only (raw-byte uploads via Telegram's attach:// convention are not yet supported).
optionsSendMediaGroupOptionsNoOptional fields such as disable_notification.

Returns: Message[]|Error

Sample code:

telegram:Message[] sent = check telegramClient->sendMediaGroup(chatId, [
{'type: "photo", media: "https://example.com/one.jpg"},
{'type: "photo", media: "https://example.com/two.jpg"}
]);
sendChatAction

Shows a short-lived chat action indicator (for example, "typing...") to chat members.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.
actionChatActionYesThe action to show; expires after ~5 seconds, or on the next sent message.

Returns: Error?

Sample code:

_ = check telegramClient->sendChatAction(chatId, "typing");
sendMessageDraft

Streams a partial message to the same message bubble, identified by draftId. Successive calls with the same draftId update the same message bubble. The draft is an ephemeral, 30-second preview; call sendMessage with the complete text to persist it in the chat. Introduced in Bot API 9.3.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric ID of a private chat (as an int). Drafts are supported only in private chats, not groups, supergroups, or channels, and Telegram rejects an @username string here.
draftIdintYesA non-zero ID; successive calls with the same ID update the same message bubble.
optionsSendMessageDraftOptionsNoOptional fields such as text and parse_mode.

Returns: Message|Error

Sample code:

telegram:Message draft = check telegramClient->sendMessageDraft(chatId, draftId, text = "Thinking...");
sendRichMessage

Sends a message with structured rich-text formatting, authored in Markdown or HTML. Introduced in Bot API 10.1.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.
richMessageRichMessageYesThe rich message content: {markdown: string} or {html: string}.
optionsSendRichMessageOptionsNoOptional fields such as disable_notification and reply_markup.

Returns: Message|Error

Sample code:

telegram:Message sent = check telegramClient->sendRichMessage(chatId, {markdown: "**Bold** and _italic_ text"});
sendRichMessageDraft

Streams a partial rich message to the same message bubble, identified by draftId. The draft is an ephemeral, 30-second preview; call sendRichMessage with the complete content to persist it in the chat. Introduced in Bot API 10.1.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric ID of a private chat (as an int). Drafts are supported only in private chats, not groups, supergroups, or channels, and Telegram rejects an @username string here.
draftIdintYesA non-zero ID; successive calls with the same ID update the same message bubble.
richMessageRichMessageYesThe rich message content: {markdown: string} or {html: string}.
messageThreadIdint?NoThe forum topic to post the draft in, if any.

Returns: Message|Error

Sample code:

telegram:Message draft = check telegramClient->sendRichMessageDraft(chatId, draftId, {markdown: "Thinking..."});
sendApprovalMessage

Sends a prompt with approve/decline inline-keyboard buttons. A client-side convenience wrapper around sendMessage; observe which button was pressed via the Listener's onCallbackQuery handler, matching on approve.callback_data/decline.callback_data.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.
textstringYesThe prompt text.
approveApprovalButtonYesThe approve button's label and callback_data.
declineApprovalButton?NoThe decline button's label and callback_data, if a decline option is wanted.
optionsApprovalMessageOptionsNoOptional fields such as disable_notification.

Returns: Message|Error

Sample code:

telegram:Message sent = check telegramClient->sendApprovalMessage(chatId, "Approve this request?",
{text: "Approve", callback_data: "approve:123"}, {text: "Decline", callback_data: "decline:123"});

Message management​

deleteMessage

Deletes a message.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.
messageIdintYesThe message's ID.

Returns: Error?

Sample code:

_ = check telegramClient->deleteMessage(chatId, messageId);
editMessageText

Edits a text message the bot previously sent, or the text of an inline message.

Parameters:

NameTypeRequiredDescription
textstringYesThe new message text.
chatIdint|string?NoThe target chat's ID or @username; required together with messageId.
messageIdint?NoThe message's ID; required together with chatId.
inlineMessageIdstring?NoAn inline message's ID; mutually exclusive with chatId/messageId.
optionsEditMessageTextOptionsNoOptional fields such as parse_mode and reply_markup.

Returns: Message|Error?

Sample code:

telegram:Message|() edited = check telegramClient->editMessageText("Updated text", chatId, messageId);
pinChatMessage

Pins a message in a chat.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.
messageIdintYesThe message's ID.
disableNotificationbooleanNoWhether to pin silently, without notifying chat members. Defaults to false.

Returns: Error?

Sample code:

_ = check telegramClient->pinChatMessage(chatId, messageId);
unpinChatMessage

Unpins a message in a chat.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.
messageIdint?NoThe pinned message's ID; unpins the most recent pinned message if omitted.

Returns: Error?

Sample code:

_ = check telegramClient->unpinChatMessage(chatId);

Chat management​

getChat

Gets up-to-date information about a chat.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.

Returns: ChatFullInfo|Error

Sample code:

telegram:ChatFullInfo chat = check telegramClient->getChat(chatId);

Sample response:

{
"id": 123456789,
"type": "private",
"first_name": "Jane"
}
getChatAdministrators

Gets the list of administrators of a chat.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.

Returns: ChatMember[]|Error

Sample code:

telegram:ChatMember[] admins = check telegramClient->getChatAdministrators(chatId);
getChatMember

Gets information about one member of a chat.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.
userIdintYesThe target user's ID.

Returns: ChatMember|Error

Sample code:

telegram:ChatMember member = check telegramClient->getChatMember(chatId, userId);
leaveChat

Makes the bot leave a chat.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.

Returns: Error?

Sample code:

_ = check telegramClient->leaveChat(chatId);
setChatDescription

Sets a group, supergroup, or channel's description.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.
descriptionstringYesThe new description, 0-255 characters.

Returns: Error?

Sample code:

_ = check telegramClient->setChatDescription(chatId, "Support channel for Acme customers");
setChatTitle

Sets a chat's title.

Parameters:

NameTypeRequiredDescription
chatIdint|stringYesThe numeric chat ID, or an @username for channels.
titlestringYesThe new title, 1-128 characters.

Returns: Error?

Sample code:

_ = check telegramClient->setChatTitle(chatId, "Acme Support");

Query answers​

answerCallbackQuery

Answers a callback query raised by pressing an inline keyboard button.

Parameters:

NameTypeRequiredDescription
callbackQueryIdstringYesThe id of the CallbackQuery to answer.
optionsAnswerCallbackQueryOptionsNoOptional fields such as text and show_alert.

Returns: Error?

Sample code:

_ = check telegramClient->answerCallbackQuery(callbackQuery.id, text = "Got it!");
answerInlineQuery

Answers an inline query with a list of results.

Parameters:

NameTypeRequiredDescription
inlineQueryIdstringYesThe id of the InlineQuery to answer.
resultsInlineQueryResult[]YesThe results to display to the user.

Returns: Error?

Sample code:

_ = check telegramClient->answerInlineQuery(inlineQuery.id, results);

Files​

getFile

Gets metadata, including the download path, for a file.

Parameters:

NameTypeRequiredDescription
fileIdstringYesThe file identifier from a message attachment.

Returns: File|Error

Sample code:

telegram:File file = check telegramClient->getFile(fileId);
downloadFile

Downloads a file's bytes given its file ID.

Parameters:

NameTypeRequiredDescription
fileIdstringYesThe file identifier from a message attachment.

Returns: byte[]|Error

Sample code:

byte[] fileBytes = check telegramClient->downloadFile(fileId);

Webhook management​

setWebhook

Registers a webhook URL to receive updates.

Parameters:

NameTypeRequiredDescription
urlstringYesThe public HTTPS URL to deliver updates to.
optionsSetWebhookOptionsNoOptional fields such as secret_token.

Returns: Error?

Sample code:

_ = check telegramClient->setWebhook("https://my-app.example.com/", secret_token = secretToken);
deleteWebhook

Removes the currently registered webhook.

Parameters:

NameTypeRequiredDescription
dropPendingUpdatesbooleanNoWhether to discard updates that were queued while the webhook was set. Defaults to false.

Returns: Error?

Sample code:

_ = check telegramClient->deleteWebhook();
getWebhookInfo

Gets information about the currently registered webhook, useful for debugging.

Returns: WebhookInfo|Error

Sample code:

telegram:WebhookInfo info = check telegramClient->getWebhookInfo();

Sample response:

{
"url": "https://my-app.example.com/",
"has_custom_certificate": false,
"pending_update_count": 0
}

What's next​

  • Trigger Reference: react to messages and queries using the webhook listener.
  • Example: complete example integrations for the Telegram connector and trigger.
  • Setup Guide: create a bot and obtain a token.