doc/telegram.link.html
<!DOCTYPE html>
<html>
<head>
<title>telegram.link.js</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>telegram.link.js</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<pre><code>telegram.link
Copyright <span class="hljs-number">2014</span> Enrico Stara <span class="hljs-string">'enrico.stara@gmail.com'</span>
Released under the MIT License
http:<span class="hljs-comment">//telegram.link</span>
</code></pre>
</div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<hr>
</div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>Dependencies:</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">var</span> tl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'telegram-tl-node'</span>);
<span class="hljs-keyword">var</span> mt = <span class="hljs-built_in">require</span>(<span class="hljs-string">'telegram-mt-node'</span>);
<span class="hljs-keyword">var</span> api = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./api'</span>);
<span class="hljs-keyword">var</span> utility = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./utility'</span>);
<span class="hljs-keyword">var</span> SequenceNumber = mt.SequenceNumber;
<span class="hljs-keyword">var</span> logger = <span class="hljs-built_in">require</span>(<span class="hljs-string">'get-log'</span>)(<span class="hljs-string">'telegram-link'</span>);</pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Constants</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">var</span> NULL_SERVER_SALT = <span class="hljs-string">'0x0000000000000000'</span>;</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<hr>
</div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>Print the library version.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">require</span>(<span class="hljs-string">'colors'</span>);
<span class="hljs-keyword">var</span> printed = <span class="hljs-literal">false</span>;
<span class="hljs-built_in">module</span>.exports = exports = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">signature</span>) </span>{
<span class="hljs-keyword">if</span> (!printed) {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'\n'</span> + (signature || <span class="hljs-built_in">require</span>(<span class="hljs-string">'./static'</span>).signature.cyan));
<span class="hljs-keyword">var</span> caption = signature ?
<span class="hljs-string">'powered by telegram.link'</span> :
(<span class="hljs-string">'v.'</span> + <span class="hljs-built_in">require</span>(<span class="hljs-string">'../package.json'</span>).version);
<span class="hljs-built_in">console</span>.log(caption.gray + <span class="hljs-string">'\n'</span>);
printed = <span class="hljs-literal">true</span>;
}
<span class="hljs-keyword">return</span> <span class="hljs-built_in">module</span>.exports;
};</pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<hr>
</div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>This module is the entry-point of the <strong>Telegram.link</strong> library and provides the highest-level <strong>interface</strong>
to communicate with the <strong>Telegram</strong> data-center. When you write your <em>telegram-like-app</em> you
should deal only with this module.</p>
</div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>You can import the module as follows:</p>
</div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<pre><code> <span class="hljs-keyword">var</span> telegramLink = <span class="hljs-built_in">require</span>(<span class="hljs-string">'telegram.link'</span>)();
</code></pre>
</div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<hr>
<p><strong>Function:</strong> telegramLink.<strong>createClient(appId, primaryDC, [options], [connectionListener])</strong></p>
</div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<p>Creates the Client and connects to the supplied primary Telegram data center address.</p>
</div>
</li>
<li id="section-13">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<p>When the Client connection is established the <code>connect</code> event will be emitted.</p>
</div>
</li>
<li id="section-14">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-14">¶</a>
</div>
<p>Usage example:</p>
</div>
</li>
<li id="section-15">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-15">¶</a>
</div>
<pre><code> <span class="hljs-keyword">var</span> client = telegramLink.createClient(
{
id: <span class="hljs-number">12345</span>,
hash: <span class="hljs-string">'q1w2e3r4t5y6u7i8o9p0'</span>,
version: <span class="hljs-string">'x.y.z'</span>,
lang: <span class="hljs-string">'xx'</span>
[, authKey]
[, connectionType: <span class="hljs-string">'TCP'</span>]
},
primaryDC
[, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">ex</span>)</span>{
<span class="hljs-keyword">if</span>(!ex) { <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Connected to Telegram!'</span>); }
}]
);
</code></pre>
</div>
</li>
<li id="section-16">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-16">¶</a>
</div>
<ul>
<li><strong>app</strong>: are all the application info:<ul>
<li><strong>id</strong> and <strong>hash</strong> <em>(required)</em>: you can get it your own APP ID at <a href="https://core.telegram.org/myapp">https://core.telegram.org/myapp</a></li>
<li><strong>version</strong> <em>(required)</em>: semver of your application, see <a href="http://semver.org">http://semver.org</a></li>
<li><strong>langCode</strong> <em>(required)</em>: device/user language, see ISO 639-1</li>
<li><strong>deviceModel</strong> <em>(required)</em>: the device model or the user agent</li>
<li><strong>systemVersion</strong> <em>(required)</em>: the operative system version</li>
<li><strong>authKey</strong> <em>(optional)</em>: the authentication key, already persisted by your app, can be supplied here. The <code>authKeyCreate</code>
event will be emitted when the connection will be established, after the the <code>connect</code> event</li>
<li><strong>connectionType</strong> <em>(optional)</em>: the value could be <code>HTTP</code> or <code>TCP</code>, HTTP is by default</li>
</ul>
</li>
<li><strong>dataCenter</strong> <em>(required)</em>: the data center address, you could use <code>telegramLink.TEST_PRIMARY_DC</code> or
<code>telegramLink.PROD_PRIMARY_DC</code></li>
<li><strong>connectionListener</strong> <em>(optional)</em>: a callback function, it will be registered as listener on <code>connect</code> event</li>
</ul>
</div>
</li>
<li id="section-17">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-17">¶</a>
</div>
<p>The code:</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createClient</span>(<span class="hljs-params">app, dataCenter, callback</span>) </span>{
<span class="hljs-keyword">var</span> client = <span class="hljs-keyword">new</span> Client(app, dataCenter);
<span class="hljs-keyword">if</span> (callback) {
client.once(<span class="hljs-string">'connect'</span>, callback);
}
connect.apply(client);
<span class="hljs-keyword">return</span> client;
}</pre></div></div>
</li>
<li id="section-18">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-18">¶</a>
</div>
<hr>
<p><strong>Function:</strong> telegramLink.<strong>retrieveAuthKey(authKeyBuffer, authKeyPassword)</strong></p>
</div>
</li>
<li id="section-19">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-19">¶</a>
</div>
<p>Decrypts the <code>authKeyBuffer</code> using the <code>authKeyPassword</code>, returns the authKey instance or null if the password is wrong.</p>
</div>
</li>
<li id="section-20">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-20">¶</a>
</div>
<p>The code:</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">retrieveAuthKey</span>(<span class="hljs-params">authKeyBuffer, authKeyPassword</span>) </span>{
<span class="hljs-keyword">return</span> mt.auth.AuthKey.decryptAuthKey(authKeyBuffer, authKeyPassword);
}</pre></div></div>
</li>
<li id="section-21">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-21">¶</a>
</div>
<hr>
<p><strong>Class:</strong> telegramLink.<strong>Client</strong></p>
</div>
</li>
<li id="section-22">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-22">¶</a>
</div>
<p>Represents the connection client to Telegram and implements the Telegram API.</p>
</div>
</li>
<li id="section-23">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-23">¶</a>
</div>
<p>It must be created by the <code>createClient()</code> method above and the following constructor
cannot be called directly <em>(private)</em>.</p>
</div>
</li>
<li id="section-24">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-24">¶</a>
</div>
<p>The code :</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Client</span>(<span class="hljs-params">app, dataCenter</span>) </span>{
<span class="hljs-keyword">this</span>._app = app;
<span class="hljs-keyword">if</span> (<span class="hljs-string">'TCP'</span> === app.connectionType) {
<span class="hljs-keyword">this</span>._connection = <span class="hljs-keyword">new</span> mt.net.TcpConnection(dataCenter);
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">this</span>._connection = <span class="hljs-keyword">new</span> mt.net.HttpConnection(dataCenter);
}
<span class="hljs-keyword">if</span> (app.authKey) {
<span class="hljs-keyword">this</span>._channel = createEncryptedChannel(<span class="hljs-keyword">this</span>._connection, app, app.authKey, NULL_SERVER_SALT);
}</pre></div></div>
</li>
<li id="section-25">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-25">¶</a>
</div>
<p>User authorization</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">this</span>.auth = <span class="hljs-keyword">new</span> (<span class="hljs-built_in">require</span>(<span class="hljs-string">'./api/auth'</span>))(<span class="hljs-keyword">this</span>);</pre></div></div>
</li>
<li id="section-26">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-26">¶</a>
</div>
<p>User contacts</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">this</span>.contacts = <span class="hljs-keyword">new</span> (<span class="hljs-built_in">require</span>(<span class="hljs-string">'./api/contacts'</span>))(<span class="hljs-keyword">this</span>);</pre></div></div>
</li>
<li id="section-27">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-27">¶</a>
</div>
<p>Session updates</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">this</span>.updates = <span class="hljs-keyword">new</span> (<span class="hljs-built_in">require</span>(<span class="hljs-string">'./api/updates'</span>))(<span class="hljs-keyword">this</span>);</pre></div></div>
</li>
<li id="section-28">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-28">¶</a>
</div>
<p>Chat messages</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">this</span>.messages = <span class="hljs-keyword">new</span> (<span class="hljs-built_in">require</span>(<span class="hljs-string">'./api/messages'</span>))(<span class="hljs-keyword">this</span>);</pre></div></div>
</li>
<li id="section-29">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-29">¶</a>
</div>
<p>Notifications and settings</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">this</span>.account = <span class="hljs-keyword">new</span> (<span class="hljs-built_in">require</span>(<span class="hljs-string">'./api/account'</span>))(<span class="hljs-keyword">this</span>);
}</pre></div></div>
</li>
<li id="section-30">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-30">¶</a>
</div>
<p>Extend the <code>events.EventEmitter</code> class</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>).inherits(Client, <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>).EventEmitter);</pre></div></div>
</li>
<li id="section-31">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-31">¶</a>
</div>
<hr>
<p><em>createEncryptedChannel()</em></p>
</div>
</li>
<li id="section-32">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-32">¶</a>
</div>
<p>Creates the encrypted channel used to call all the Telegram API methods <em>(private)</em>.</p>
</div>
</li>
<li id="section-33">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-33">¶</a>
</div>
<p>The code:</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createEncryptedChannel</span>(<span class="hljs-params">connection, app, authKey, serverSalt</span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> mt.net.EncryptedRpcChannel(
connection, {
authKey: authKey,
serverSalt: serverSalt,
sessionId: mt.utility.createNonce(<span class="hljs-number">8</span>),
sequenceNumber: <span class="hljs-keyword">new</span> SequenceNumber()
}, app
);
}</pre></div></div>
</li>
<li id="section-34">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-34">¶</a>
</div>
<hr>
<p>client.<strong>createAuthKey([callback])</strong></p>
</div>
</li>
<li id="section-35">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-35">¶</a>
</div>
<p>Exchanges the authorization key with Telegram, see <a href="https://core.telegram.org/mtproto/auth_key">https://core.telegram.org/mtproto/auth_key</a></p>
</div>
</li>
<li id="section-36">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-36">¶</a>
</div>
<p>This method is asynchronous and the client must be already connected before calling this function.
When the authKey is created, the <code>authKeyCreate</code> event will be emitted.</p>
</div>
</li>
<li id="section-37">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-37">¶</a>
</div>
<p>The last parameter callback will be added as a listener for the <code>authKeyCreate</code> event, the callback
could receive the following arguments:</p>
<ul>
<li><strong>error</strong>: the error if an exception occurs, null otherwise.</li>
<li><strong>authKey</strong>: the authentication key exchanged with Telegram.</li>
</ul>
</div>
</li>
<li id="section-38">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-38">¶</a>
</div>
<p>The code:</p>
</div>
<div class="content"><div class='highlight'><pre>Client.prototype.createAuthKey = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">callback</span>) </span>{
<span class="hljs-keyword">if</span> (callback) {
<span class="hljs-keyword">this</span>.once(<span class="hljs-string">'authKeyCreate'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">auth</span>) </span>{
<span class="hljs-keyword">this</span>._channel = createEncryptedChannel(<span class="hljs-keyword">this</span>._connection, <span class="hljs-keyword">this</span>._app, auth.key, auth.serverSalt);
callback(auth);
});
}
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>._connection && <span class="hljs-keyword">this</span>._connection.isConnected()) {
mt.auth.createAuthKey(
utility.createEventEmitterCallback(<span class="hljs-string">'authKeyCreate'</span>, <span class="hljs-keyword">this</span>),
<span class="hljs-keyword">new</span> mt.net.RpcChannel(<span class="hljs-keyword">this</span>._connection));
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">var</span> msg = <span class="hljs-string">'Client is not yet connected! Wait until it\'s connected before call this method!'</span>;
logger.error(msg);
<span class="hljs-keyword">this</span>.emit(<span class="hljs-string">'error'</span>, <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(msg));
}
};</pre></div></div>
</li>
<li id="section-39">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-39">¶</a>
</div>
<hr>
<p>client.<strong>getDataCenterList([callback])</strong></p>
</div>
</li>
<li id="section-40">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-40">¶</a>
</div>
<p>Gets the data center list, the current data center and the nearest one.</p>
</div>
</li>
<li id="section-41">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-41">¶</a>
</div>
<p>The code:</p>
</div>
<div class="content"><div class='highlight'><pre>Client.prototype.getDataCenters = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">callback</span>) </span>{
<span class="hljs-keyword">if</span> (callback) {
<span class="hljs-keyword">this</span>.once(<span class="hljs-string">'dataCenter'</span>, callback);
}
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.isReady(<span class="hljs-literal">true</span>)) {
<span class="hljs-keyword">var</span> self = <span class="hljs-keyword">this</span>;
<span class="hljs-keyword">try</span> {
api.service.help.getConfig({
props: {},
channel: self._channel,
callback: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">ex, config</span>) </span>{
<span class="hljs-keyword">if</span> (ex) {
self.emit(<span class="hljs-string">'error'</span>, ex);
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">var</span> dcs = {
toPrintable: tl.utility.toPrintable
};
<span class="hljs-keyword">var</span> dcList = config.dc_options.list;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i < dcList.length; i++) {
<span class="hljs-keyword">var</span> dc = dcList[i];
dcs[<span class="hljs-string">'DC_'</span> + dc.id] = {
host: dc.ip_address,
port: dc.port,
toPrintable: tl.utility.toPrintable
};
}
api.service.help.getNearestDc({
props: {},
channel: self._channel,
callback: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">ex, nearestDc</span>) </span>{
<span class="hljs-keyword">if</span> (ex) {
logger.error(<span class="hljs-string">'error: '</span>, ex);
self.emit(<span class="hljs-string">'error'</span>, ex);
} <span class="hljs-keyword">else</span> {
dcs.current = <span class="hljs-string">'DC_'</span> + nearestDc.this_dc;
dcs.nearest = <span class="hljs-string">'DC_'</span> + nearestDc.nearest_dc;
self.emit(<span class="hljs-string">'dataCenter'</span>, dcs);
}
}
});
}
}
});
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-keyword">this</span>.emit(<span class="hljs-string">'error'</span>, err);
}
}
};</pre></div></div>
</li>
<li id="section-42">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-42">¶</a>
</div>
<hr>
<p>client.<strong>httpPoll(callback, [maxWait], [waitAfter], [maxDelay])</strong></p>
</div>
</li>
<li id="section-43">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-43">¶</a>
</div>
<p>HTTP long poll service.</p>
</div>
</li>
<li id="section-44">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-44">¶</a>
</div>
<p>The code:</p>
</div>
<div class="content"><div class='highlight'><pre>Client.prototype.httpPoll = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">callback, maxWait, waitAfter, maxDelay</span>) </span>{
<span class="hljs-keyword">if</span> (callback) {
<span class="hljs-keyword">this</span>.once(<span class="hljs-string">'httpPoll'</span>, callback);
}
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.isReady(<span class="hljs-literal">true</span>) && <span class="hljs-keyword">this</span>._connection <span class="hljs-keyword">instanceof</span> mt.net.HttpConnection) {
<span class="hljs-keyword">var</span> self = <span class="hljs-keyword">this</span>;
maxWait = maxWait || <span class="hljs-number">30000</span>;
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'call http long poll each %sms'</span>, maxWait);
<span class="hljs-keyword">try</span> {
mt.service.http_wait({
props: {
max_delay: maxDelay || <span class="hljs-number">0</span>,
wait_after: waitAfter || <span class="hljs-number">0</span>,
max_wait: maxWait
},
channel: self._channel,
callback: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">ex, result</span>) </span>{
<span class="hljs-keyword">if</span> (ex) {
self.emit(<span class="hljs-string">'error'</span>, ex);
} <span class="hljs-keyword">else</span> {
self.emit(<span class="hljs-string">'httpPoll'</span>, result);
<span class="hljs-keyword">if</span>(self._httpPollLoop) {
self.httpPoll(callback, maxWait, waitAfter, maxDelay);
}
}
}
});
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-keyword">this</span>.emit(<span class="hljs-string">'error'</span>, err);
}
}
};</pre></div></div>
</li>
<li id="section-45">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-45">¶</a>
</div>
<hr>
<p>client.<strong>startHttpPollLoop(callback, [maxWait], [waitAfter], [maxDelay])</strong></p>
</div>
</li>
<li id="section-46">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-46">¶</a>
</div>
<p>Start the HTTP long poll service loop.</p>
</div>
</li>
<li id="section-47">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-47">¶</a>
</div>
<p>The code:</p>
</div>
<div class="content"><div class='highlight'><pre>Client.prototype.startHttpPollLoop = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">callback, maxWait, waitAfter, maxDelay</span>) </span>{
<span class="hljs-keyword">this</span>._httpPollLoop = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">this</span>.httpPoll(callback, maxWait, waitAfter, maxDelay);
};</pre></div></div>
</li>
<li id="section-48">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-48">¶</a>
</div>
<hr>
<p>client.<strong>stopHttpPollLoop()</strong></p>
</div>
</li>
<li id="section-49">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-49">¶</a>
</div>
<p>Stop the HTTP long poll service loop.</p>
</div>
</li>
<li id="section-50">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-50">¶</a>
</div>
<p>The code:</p>
</div>
<div class="content"><div class='highlight'><pre>Client.prototype.stopHttpPollLoop = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>._httpPollLoop = <span class="hljs-literal">false</span>;
};</pre></div></div>
</li>
<li id="section-51">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-51">¶</a>
</div>
<hr>
<p>client.<strong>registerOnUpdates(callback)</strong></p>
</div>
</li>
<li id="section-52">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-52">¶</a>
</div>
<p>Register the function and call back on any update events.</p>
</div>
</li>
<li id="section-53">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-53">¶</a>
</div>
<p>The code:</p>
</div>
<div class="content"><div class='highlight'><pre>Client.prototype.registerOnUpdates = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">callback</span>) </span>{
<span class="hljs-keyword">var</span> emitter = <span class="hljs-keyword">this</span>._channel.getParser();
emitter.on(<span class="hljs-string">'api.type.UpdatesTooLong'</span>, callback);
emitter.on(<span class="hljs-string">'api.type.UpdateShortMessage'</span>, callback);
emitter.on(<span class="hljs-string">'api.type.UpdateShortChatMessage'</span>, callback);
emitter.on(<span class="hljs-string">'api.type.UpdateShort'</span>, callback);
emitter.on(<span class="hljs-string">'api.type.UpdatesCombined'</span>, callback);
emitter.on(<span class="hljs-string">'api.type.Updates'</span>, callback);
};</pre></div></div>
</li>
<li id="section-54">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-54">¶</a>
</div>
<hr>
<p>client.<strong>unregisterOnUpdates(callback)</strong></p>
</div>
</li>
<li id="section-55">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-55">¶</a>
</div>
<p>Un-register from the update events.</p>
</div>
</li>
<li id="section-56">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-56">¶</a>
</div>
<p>The code:</p>
</div>
<div class="content"><div class='highlight'><pre>Client.prototype.unregisterOnUpdates = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">callback</span>) </span>{
<span class="hljs-keyword">var</span> emitter = <span class="hljs-keyword">this</span>._channel.getParser();
emitter.removeListener(<span class="hljs-string">'api.type.UpdatesTooLong'</span>, callback);
emitter.removeListener(<span class="hljs-string">'api.type.UpdateShortMessage'</span>, callback);
emitter.removeListener(<span class="hljs-string">'api.type.UpdateShortChatMessage'</span>, callback);
emitter.removeListener(<span class="hljs-string">'api.type.UpdateShort'</span>, callback);
emitter.removeListener(<span class="hljs-string">'api.type.UpdatesCombined'</span>, callback);
emitter.removeListener(<span class="hljs-string">'api.type.Updates'</span>, callback);
};</pre></div></div>
</li>
<li id="section-57">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-57">¶</a>
</div>
<hr>
<p>client.<strong>isReady()</strong></p>
</div>
</li>
<li id="section-58">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-58">¶</a>
</div>
<p>Checks if the client is ready to communicate with Telegram and immediately emits the error-event if required.</p>
</div>
</li>
<li id="section-59">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-59">¶</a>
</div>
<p>The code:</p>
</div>
<div class="content"><div class='highlight'><pre>Client.prototype.isReady = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">emitError</span>) </span>{
<span class="hljs-keyword">var</span> isReady = <span class="hljs-keyword">this</span>._channel ? <span class="hljs-literal">true</span> : <span class="hljs-literal">false</span>;
<span class="hljs-keyword">if</span> (!isReady && emitError) {
<span class="hljs-keyword">var</span> msg = <span class="hljs-string">'Client is not yet ready!'</span>;
logger.error(msg);
<span class="hljs-keyword">this</span>.emit(<span class="hljs-string">'error'</span>, <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(msg));
}
<span class="hljs-keyword">return</span> isReady;
};</pre></div></div>
</li>
<li id="section-60">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-60">¶</a>
</div>
<hr>
<p>client.<strong>end([callback])</strong></p>
</div>
</li>
<li id="section-61">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-61">¶</a>
</div>
<p>Closes the communication with the DataCenter,</p>
</div>
</li>
<li id="section-62">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-62">¶</a>
</div>
<p>The last parameter <strong>callback</strong> will be added as a listener for the <code>end</code> event.</p>
</div>
</li>
<li id="section-63">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-63">¶</a>
</div>
<p>The code:</p>
</div>
<div class="content"><div class='highlight'><pre>Client.prototype.end = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">callback</span>) </span>{
<span class="hljs-keyword">if</span> (callback) {
<span class="hljs-keyword">this</span>.once(<span class="hljs-string">'end'</span>, callback);
}
<span class="hljs-keyword">this</span>._connection.close(utility.createEventEmitterCallback(<span class="hljs-string">'end'</span>, <span class="hljs-keyword">this</span>));
};</pre></div></div>
</li>
<li id="section-64">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-64">¶</a>
</div>
<hr>
<p><em>connect()</em></p>
</div>
</li>
<li id="section-65">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-65">¶</a>
</div>
<p>Establishes the connection with the data center <em>(private)</em>.</p>
</div>
</li>
<li id="section-66">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-66">¶</a>
</div>
<p>The code:</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">connect</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>._connection.connect(utility.createEventEmitterCallback(<span class="hljs-string">'connect'</span>, <span class="hljs-keyword">this</span>));
}</pre></div></div>
</li>
<li id="section-67">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-67">¶</a>
</div>
<hr>
<p><strong>Event: ‘connect’</strong>
Emitted when the client connection is successfully established. See <strong>createClient()</strong>.</p>
</div>
</li>
<li id="section-68">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-68">¶</a>
</div>
<p><strong>Event: ‘authKeyCreate’</strong>
Emitted when the authKey is successfully created. See client.<strong>createAuthKey()</strong>.</p>
</div>
</li>
<li id="section-69">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-69">¶</a>
</div>
<p><strong>Event: ‘dataCenter’</strong>
Emitted when the data-center map is available. See client.<strong>getDataCenters()</strong>.</p>
</div>
</li>
<li id="section-70">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-70">¶</a>
</div>
<p><strong>Event: ‘end’</strong>
Emitted when the data center close the connection. See client.<strong>end()</strong>.</p>
</div>
</li>
<li id="section-71">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-71">¶</a>
</div>
<p><strong>Event: ‘error’</strong>
Emitted when an error occurs.</p>
</div>
</li>
<li id="section-72">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-72">¶</a>
</div>
<hr>
</div>
</li>
<li id="section-73">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-73">¶</a>
</div>
<p>Export the internals.</p>
</div>
<div class="content"><div class='highlight'><pre>exports.createClient = createClient;
exports.retrieveAuthKey = retrieveAuthKey;
<span class="hljs-keyword">var</span> staticInfo = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./static'</span>);
exports.TEST_PRIMARY_DC = staticInfo.telegram.test.primaryDataCenter;
exports.PROD_PRIMARY_DC = staticInfo.telegram.prod.primaryDataCenter;</pre></div></div>
</li>
<li id="section-74">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-74">¶</a>
</div>
<p>Export all the API types. (no methods)</p>
</div>
<div class="content"><div class='highlight'><pre>exports.type = api.type;</pre></div></div>
</li>
</ul>
</div>
</body>
</html>