Velegram

June 18, 2026 ยท View on GitHub

A V language wrapper for TDLib (Telegram Database Library).

Velegram provides a structured, type-safe API interface to communicate with TDLib, allowing you to develop both userbots and bot accounts in the V language.


Requirements

  • V Language (latest stable version)
  • TDLib compiled and installed on your system (shared library libtdjson must be accessible)
  • Telegram API credentials (api_id and api_hash) from my.telegram.org

Architecture Overview

Velegram handles communication with TDLib through two main execution models:

  1. Asynchronous (Fire-and-Forget): Most sending and action commands (e.g., send, reply, delete) send a query to TDLib and return immediately. The results or errors are received asynchronously through the main event loop in your on_response callback.
  2. Synchronous (Request-Response matching): Functions prefixed with fetch_ (defined in reader.v) use TDLib's @extra field tracking. They send a query, block the thread up to a specified timeout, filter out unrelated incoming updates, match the corresponding response ID, and return the parsed V struct directly.

Quick Start

Create a main.v file and configure your credentials:

module main

import tdlib
import handlers
import structs
import messages

fn on_message(client voidptr, msg structs.TextMessage) {
	if msg.is_outgoing {
		return
	}
	println('[New Message] ${msg.sender_id}: ${msg.text}')
	
	if msg.text == '/ping' {
		messages.reply(client, msg.chat_id, msg.id, 'pong')
	}
}

fn on_response(client voidptr, resp_type string, raw string) {
	// Handles asynchronous responses from TDLib
}

fn main() {
	api_id := 123456
	api_hash := 'your_api_hash'
	bot_token := 'your_bot_token' // Leave empty for user-account login

	client := tdlib.new_client() or {
		eprintln(err)
		return
	}

	handlers.run(client, 1.0, api_id, api_hash, bot_token, on_message, on_response) or {
		eprintln(err)
	}

	tdlib.client_destroy(client)
}

API Documentation

Message Sending

FunctionDescription
messages.sendSends a plain text message
messages.send_boldSends text formatted in bold
messages.send_italicSends text formatted in italic
messages.send_underlineSends text formatted with an underline
messages.send_strikeSends text formatted with a strikethrough
messages.send_codeSends inline monospace code
messages.send_code_blockSends a code block with language syntax highlighting
messages.send_spoilerSends spoiler-tagged hidden text
messages.send_quoteSends a blockquote block
messages.send_link_textSends text with an embedded hyperlink
messages.send_formattedSends text with a custom list of text entities
messages.send_silentSends a message without triggering a push notification
messages.send_scheduledSchedules a message to be sent at a specific Unix timestamp
messages.send_once_onlineSends a message the next time the recipient comes online
messages.send_no_previewSends a text message with disabled link previews

Media & Files

FunctionDescription
messages.send_photoSends a local photo with a caption
messages.send_documentSends a local document/file with a caption
messages.send_voiceSends a local .ogg voice message with duration and caption
messages.send_videoSends a local video file with a caption
messages.send_stickerSends a local sticker file (.webp / .tgs)
messages.send_locationSends geographical coordinates
messages.send_contactSends a contact card
messages.send_venueSends a specific venue/location details
messages.download_fileInitiates downloading a file from Telegram servers

Replies & Actions

FunctionDescription
messages.replyReplies to a specific message
messages.reply_formattedReplies with custom text entities
messages.reply_silentReplies without sending sound notifications
messages.reply_boldReplies with bold text
messages.add_reactionAdds an emoji reaction to a message
messages.remove_reactionRemoves an emoji reaction from a message

Edit / Delete / Forward

FunctionDescription
messages.editEdits the text of an existing message
messages.edit_formattedEdits message text and applies custom entities
messages.edit_boldEdits message text to bold
messages.delete_oneDeletes a single message
messages.deleteDeletes a list of messages
messages.forward_oneForwards a single message to another chat
messages.forwardForwards multiple messages to another chat
messages.forward_silentForwards messages silently
messages.forward_copySends a copy of messages without forwarding attribution
messages.copy_oneCopies a single message to a target chat

Chat & Account Management

FunctionDescription
messages.resolve_chatResolves a public username (e.g. @channel)
messages.open_chatNotifies TDLib that a chat is being opened
messages.close_chatNotifies TDLib that a chat is being closed
messages.load_chatsPre-loads your Telegram chat list
messages.join_chatJoins a group/channel by its ID
messages.leave_chatLeaves a group/channel
messages.join_by_linkJoins a private chat using an invite link
messages.set_chat_titleUpdates chat title
messages.set_chat_descriptionUpdates chat description
messages.mute_chatMutes notifications of a chat indefinitely
messages.unmute_chatUnmutes notifications of a chat
messages.archive_chatMoves a chat to the archived folder
messages.unarchive_chatRestores an archived chat to the main list
messages.get_common_chatsGets common groups with a specific user
messages.report_chatReports a chat for spam or abuse

Member Moderation & Contacts

FunctionDescription
messages.add_memberAdds a user to a group chat
messages.ban_memberBans and removes a user from a chat
messages.unban_memberLifts a ban on a user in a chat
messages.promote_adminPromotes a member to an administrator with full rights
messages.demote_adminDemotes an administrator back to a regular member
messages.restrict_memberRestricts a user's permissions until a specific date
messages.unrestrict_memberRestores a restricted user's normal permissions
messages.block_userAdds a user to your personal block list
messages.unblock_userRemoves a user from your block list
messages.block_chatBlocks a chat's sender
messages.add_contactImports a new contact
messages.delete_contactRemoves an existing contact
messages.get_contactsFetches your personal contact list

Forums & Topics

FunctionDescription
messages.send_to_topicSends a message to a specific topic ID
messages.reply_in_topicReplies to a message inside a topic
messages.send_commentFetches a discussion thread for channel comments
messages.send_comment_textSends a comment directly to a discussion thread
messages.create_topicCreates a new forum topic
messages.edit_topicRenames an existing topic
messages.close_topicCloses a topic (disables sending messages)
messages.open_topicReopens a closed topic
messages.delete_topicDeletes a topic and all its messages
messages.pin_topicPins a topic to the top of the forum
messages.unpin_topicUnpins a topic

Synchronous Fetching (Via @extra Tracking)

These functions wait for TDLib's sequential events, match the unique transaction ID (@extra), and return parsed structs or optional types synchronously:

FunctionReturn TypeDescription
messages.fetch_history[]structs.TextMessageFetches chat history
messages.fetch_history_topic[]structs.TextMessageFetches history from a forum topic
messages.fetch_message?structs.TextMessageFetches a single parsed message
messages.fetch_message_raw?structs.TextMessageFetches a single message without opening chat
messages.fetch_search[]structs.TextMessageSearches messages inside a specific chat
messages.fetch_search_global[]structs.TextMessageSearches messages globally
messages.fetch_chat_info?structs.ChatInfoFetches general details about a chat
messages.fetch_chat_by_username?structs.ChatInfoResolves username and fetches chat details
messages.fetch_link?structs.MessageLinkResponseGets an HTTPS public link of a message
messages.fetch_comments[]structs.TextMessageFetches comments of a channel post
messages.fetch_topics[]structs.ForumTopicFetches a list of topics in a forum
messages.fetch_user_info?structs.UserInfoFetches a user's profile information
messages.fetch_private_chat?structs.ChatInfoCreates a private chat and returns its info
messages.fetch_supergroup_info?structs.SupergroupInfoFetches full information of a supergroup
messages.fetch_supergroup_members[]structs.ChatMemberFetches recent members of a supergroup
messages.fetch_me?structs.UserInfoFetches the current logged-in account info
messages.fetch_invite_link_info?stringResolves chat invitation link details
messages.fetch_chat_members_search[]structs.ChatMemberSearches chat members based on a query
messages.fetch_sticker_set?stringFetches information about a sticker set
messages.fetch_contacts?stringFetches and returns contacts raw response

Testing Commands

When running the project with main.v, you can send these commands in any chat to test the library's features:

/ping              replies with "pong"
/delete            deletes the sent command message
/silent            sends a silent test message
/nopreview         sends link without rich preview
/typing            displays "typing" chat action
/bold              sends text in bold
/italic            sends text in italic
/underline         sends text with underline
/strike            sends text with strikethrough
/code              sends inline monospace text
/codeblock         sends formatted code block
/spoiler           sends hidden spoiler text
/quote             sends blocked quote text
/link              sends text hyperlink
/scheduled         sends a scheduled message (+1 min)
/draft             sets active draft in current chat
/cleardraft        clears current draft
/read              marks current message as read
/history           requests last 20 messages (async)
/mute              mutes notifications for current chat
/unmute            unmutes notifications for current chat
/archive           archives current chat
/unarchive         restores chat from archive
/info              retrieves and displays current chat details
/sginfo            retrieves supergroup info
/members           retrieves first 50 supergroup members
/topics            retrieves first 50 forum topics
/getlink           gets HTTP link of replied message
/forward           forwards replied message
/copy              copies replied message without author
/pin               pins replied message
/unpin             unpins replied message
/unpinall          unpins all pinned messages in chat
/photo             sends local photo (requires data/test.jpg)
/doc               sends local PDF (requires data/test.pdf)
/voice             sends local voice file (requires data/test.ogg)
/video             sends local video file (requires data/test.mp4)
/sticker           sends local WebP sticker (requires data/test.webp)
/location          sends test coordinates
/contact           sends dummy contact card
/venue             sends test venue details
/react             adds ๐Ÿ‘ reaction to replied message
/unreact           removes ๐Ÿ‘ reaction from replied message
/fetch             synchronously fetches and displays replied message text
/fetchchat         synchronously fetches current chat title
/createtopic [N]   creates a forum topic named N
/closetopic [ID]   closes forum topic with ID
/opentopic [ID]    reopens forum topic with ID
/download [ID]     downloads file by its local ID
/history [C] [N]   fetches last N messages from chat ID C
/resolve [U]       resolves public username U
/getmsg [C] [M]    reads specific message M from chat C
/search [Q]        searches Q in current chat
/gsearch [Q]       searches Q globally
/user [ID]         reads profile of user ID
/send [C] [T]      sends text T to chat ID C
/sendto [U] [T]    resolves username U and sends text T
/topic [ID] [T]    sends text T to topic ID
/join [U/L]        joins chat via username U or invite link L
/leave [C]         leaves chat with ID C
/ban [ID]          bans user ID from current chat
/unban [ID]        unbans user ID in current chat
/promote [ID]      promotes user ID to administrator
/demote [ID]       demotes admin user ID
/block [ID]        adds user ID to personal blocklist
/unblock [ID]      removes user ID from blocklist

License

License: MIT