telegram-crawler/data/web/blogfork.telegram.org/api/bots/ids.html
2024-09-17 22:21:45 +00:00

154 lines
9.7 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html class="">
<head>
<meta charset="utf-8">
<title>Bot API dialog IDs</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta property="description" content="How to convert MTProto peer IDs to bot API dialog IDs and vice versa.">
<meta property="og:title" content="Bot API dialog IDs">
<meta property="og:image" content="">
<meta property="og:description" content="How to convert MTProto peer IDs to bot API dialog IDs and vice versa.">
<link rel="icon" type="image/svg+xml" href="/img/website_icon.svg?4">
<link rel="apple-touch-icon" sizes="180x180" href="/img/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/img/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/img/favicon-16x16.png">
<link rel="alternate icon" href="/img/favicon.ico" type="image/x-icon" />
<link href="/css/bootstrap.min.css?3" rel="stylesheet">
<link href="/css/telegram.css?240" rel="stylesheet" media="screen">
<style>
</style>
</head>
<body class="preload">
<div class="dev_page_wrap">
<div class="dev_page_head navbar navbar-static-top navbar-tg">
<div class="navbar-inner">
<div class="container clearfix">
<ul class="nav navbar-nav navbar-right hidden-xs"><li class="navbar-twitter"><a href="https://twitter.com/telegram" target="_blank" data-track="Follow/Twitter" onclick="trackDlClick(this, event)"><i class="icon icon-twitter"></i><span> Twitter</span></a></li></ul>
<ul class="nav navbar-nav">
<li><a href="//telegram.org/">Home</a></li>
<li class="hidden-xs"><a href="//telegram.org/faq">FAQ</a></li>
<li class="hidden-xs"><a href="//telegram.org/apps">Apps</a></li>
<li class="active"><a href="/api">API</a></li>
<li class=""><a href="/mtproto">Protocol</a></li>
<li class=""><a href="/schema">Schema</a></li>
</ul>
</div>
</div>
</div>
<div class="container clearfix">
<div class="dev_page">
<div id="dev_page_content_wrap" class=" ">
<div class="dev_page_bread_crumbs"><ul class="breadcrumb clearfix"><li><a href="/api" >API</a></li><i class="icon icon-breadcrumb-divider"></i><li><a href="/api/bots%2Fids" >Bot API dialog IDs</a></li></ul></div>
<h1 id="dev_page_title">Bot API dialog IDs</h1>
<div id="dev_page_content"><!-- scroll_nav -->
<p>The <code>id</code> field of <a href="/api/peers">peers »</a> (i.e. users, chats, channels, secret chats) contain four distinct sequences of unique 64-bit IDs used to identify a specific user, chat, channel or secret chat. </p>
<p>The ID sequences of users, chats, channels and secret chats <strong>overlap</strong>, so it is a good idea to transform the peer IDs to bot API dialog IDs as specified below. </p>
<p>A bot API dialog ID is a single, unique 64-bit peer ID sequence derived from the user, chat, channel and secret chat ID sequences, maintaining uniqueness across all of them.<br>
Bot API dialog IDs are already used in the <a href="/bots/api">bot API</a> and in the underlying <a href="https://github.com/tdlib/td">tdlib</a> library to identify peers. </p>
<p>As specified above, a bot API dialog ID may have more than 32 significant bits and some programming languages may have difficulty/silent defects in interpreting it. But, thanks to the ranges of the underlying MTProto IDs (specified below), it has at most 52 significant bits, so a 64-bit integer or even a double-precision float type are safe for storing this identifier. </p>
<p>More specifically, a bot API dialog ID ranges from <code>-2002147483648</code> to <code>1099511627775</code>. </p>
<p>It's a good idea to transform peer IDs to bot dialog API IDs even if you do decide to use separate databases to <a href="/api/peers">store info about peers</a>, as it will make IDs more visually recognizable both for you and your users, as well as guarantee compatibility with the bot API, and allow your client to easily identify the type of a peer just by using its ID, thanks to the range checks specified below.</p>
<p>Example implementation: <a href="https://github.com/tdlib/td/blob/cb164927417f22811c74cd8678ed4a5ab7cb80ba/td/telegram/DialogId.cpp">tdlib (bot API)</a>, <a href="https://github.com/danog/tg-dialog-id">MadelineProto</a>. </p>
<h3><a class="anchor" href="#user-ids" id="user-ids" name="user-ids"><i class="anchor-icon"></i></a>User IDs</h3>
<p><a href="/constructor/user">User</a> IDs in the MTProto API range from <code>1</code> to <code>0xffffffffff</code> (inclusive). </p>
<p>MTProto user IDs are equal to bot API user dialog IDs and vice versa, you don't have to do anything to convert them, except validate that they fall within the range specified above. </p>
<pre><code>$botApiUserId = $userId;
$userId = $botApiUserId;</code></pre>
<h3><a class="anchor" href="#chat-ids" id="chat-ids" name="chat-ids"><i class="anchor-icon"></i></a>Chat IDs</h3>
<p><a href="/constructor/chat">Chat</a> IDs in the MTProto API range from <code>1</code> to <code>999999999999</code> (inclusive). </p>
<p>To convert MTProto chat IDs to bot API chat dialog IDs, make them negative (and vice versa). </p>
<p>Before conversion, always validate that they fall within the range specified above (appropriately transformed for bot API chat dialog IDs, i.e. <code>-999999999999</code> to <code>-1</code> inclusively). </p>
<pre><code>$botApiChatId = -$chatId;
$chatId = -$botApiChatId;</code></pre>
<h3><a class="anchor" href="#supergroup-channel-ids" id="supergroup-channel-ids" name="supergroup-channel-ids"><i class="anchor-icon"></i></a>Supergroup/channel IDs</h3>
<p><a href="/constructor/channel">Supergroup/channel</a> IDs share the same sequence in the MTProto API, and they range from <code>1</code> to <code>997852516352</code> (inclusive). </p>
<p>To convert MTProto channel IDs to bot API channel dialog IDs, add <code>1000000000000</code> and make them negative (and vice versa). </p>
<p>Before conversion, always validate that they fall within the range specified above (appropriately transformed for bot API channel dialog IDs, i.e. <code>-1997852516352</code> to <code>-1000000000001</code> inclusively). </p>
<pre><code>$botApiChannelId = -(1000000000000 + $channelId);
$channelId = -$botApiChannelId - 1000000000000;</code></pre>
<h3><a class="anchor" href="#secret-chat-ids" id="secret-chat-ids" name="secret-chat-ids"><i class="anchor-icon"></i></a>Secret chat IDs</h3>
<p><a href="/api/end-to-end">Secret chat IDs</a> in the MTProto API range from <code>-2147483648</code> to <code>2147483647</code> (inclusive, treat the secret chat ID as a signed little-endian 32-bit integer). </p>
<p>To convert MTProto chat IDs to bot API chat secret chat IDs, subtract <code>2000000000000</code>. </p>
<p>Before conversion, always validate that they fall within the range specified above (appropriately transformed for bot API secret chat dialog IDs, i.e. <code>-2002147483648</code> to <code>-1997852516353</code> inclusively). </p>
<pre><code>$botApiSecretChatId = $secretChatId - 2000000000000;
$secretChatId = $botApiSecretChatId + 2000000000000;</code></pre>
<p>Note: while the official instance of the bot API does not support secret chats, the underlying <a href="https://github.com/tdlib/td/">tdlib</a> library does support them, and uses the format mentioned above for secret chat IDs.</p></div>
</div>
</div>
</div>
<div class="footer_wrap">
<div class="footer_columns_wrap footer_desktop">
<div class="footer_column footer_column_telegram">
<h5>Telegram</h5>
<div class="footer_telegram_description"></div>
Telegram is a cloud-based mobile and desktop messaging app with a focus on security and speed.
</div>
<div class="footer_column">
<h5><a href="//telegram.org/faq">About</a></h5>
<ul>
<li><a href="//telegram.org/faq">FAQ</a></li>
<li><a href="//telegram.org/privacy">Privacy</a></li>
<li><a href="//telegram.org/press">Press</a></li>
</ul>
</div>
<div class="footer_column">
<h5><a href="//telegram.org/apps#mobile-apps">Mobile Apps</a></h5>
<ul>
<li><a href="//telegram.org/dl/ios">iPhone/iPad</a></li>
<li><a href="//telegram.org/android">Android</a></li>
<li><a href="//telegram.org/dl/web">Mobile Web</a></li>
</ul>
</div>
<div class="footer_column">
<h5><a href="//telegram.org/apps#desktop-apps">Desktop Apps</a></h5>
<ul>
<li><a href="//desktop.telegram.org/">PC/Mac/Linux</a></li>
<li><a href="//macos.telegram.org/">macOS</a></li>
<li><a href="//telegram.org/dl/web">Web-browser</a></li>
</ul>
</div>
<div class="footer_column footer_column_platform">
<h5><a href="/">Platform</a></h5>
<ul>
<li><a href="/api">API</a></li>
<li><a href="//translations.telegram.org/">Translations</a></li>
<li><a href="//instantview.telegram.org/">Instant View</a></li>
</ul>
</div>
</div>
<div class="footer_columns_wrap footer_mobile">
<div class="footer_column">
<h5><a href="//telegram.org/faq">About</a></h5>
</div>
<div class="footer_column">
<h5><a href="//telegram.org/blog">Blog</a></h5>
</div>
<div class="footer_column">
<h5><a href="//telegram.org/apps">Apps</a></h5>
</div>
<div class="footer_column">
<h5><a href="/">Platform</a></h5>
</div>
<div class="footer_column">
<h5><a href="//telegram.org/press">Press</a></h5>
</div>
</div>
</div>
</div>
<script src="/js/main.js?47"></script>
<script src="/js/jquery.min.js?1"></script>
<script src="/js/bootstrap.min.js?1"></script>
<script>window.initDevPageNav&&initDevPageNav();
backToTopInit("Go up");
removePreloadInit();
</script>
</body>
</html>