This commit is contained in:
InsanusMokrassar 2023-06-10 07:53:25 +00:00
parent 7457aea297
commit d67f0e483a
28 changed files with 859 additions and 873 deletions

View File

@ -1284,33 +1284,33 @@
See <a href="string-format.html">String format</a> for more info about the crontab-line syntax</p> See <a href="string-format.html">String format</a> for more info about the crontab-line syntax</p>
</div> </div>
<p>This way will be very useful for cases when you need to configure something via external configuration (from file on startup or via some parameter from requests, for example):</p> <p>This way will be very useful for cases when you need to configure something via external configuration (from file on startup or via some parameter from requests, for example):</p>
<pre><code class="language-kotlin">val schedule = &quot;5 * * * *&quot; <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">schedule</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;5 * * * *&quot;</span>
val scheduler = buildSchedule(schedule) </span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="kd">val</span><span class="w"> </span><span class="nv">scheduler</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buildSchedule</span><span class="p">(</span><span class="n">schedule</span><span class="p">)</span>
</span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a>
scheduler.asFlow().onEach { </span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="n">scheduler</span><span class="p">.</span><span class="na">asFlow</span><span class="p">().</span><span class="na">onEach</span><span class="w"> </span><span class="p">{</span>
// this block will be called every minute at 5 seconds </span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="w"> </span><span class="c1">// this block will be called every minute at 5 seconds</span>
}.launchIn(someCoroutineScope) </span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a><span class="p">}.</span><span class="na">launchIn</span><span class="p">(</span><span class="n">someCoroutineScope</span><span class="p">)</span>
</code></pre> </span></code></pre></div>
<h4 id="lambda-way">Lambda way<a class="headerlink" href="#lambda-way" title="Permanent link">&para;</a></h4> <h4 id="lambda-way">Lambda way<a class="headerlink" href="#lambda-way" title="Permanent link">&para;</a></h4>
<p>In case of usage builder (lets call it <code>lambda way</code>), you will be able to configure scheduler in more type-safe way:</p> <p>In case of usage builder (lets call it <code>lambda way</code>), you will be able to configure scheduler in more type-safe way:</p>
<pre><code class="language-kotlin">val scheduler = buildSchedule { <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">scheduler</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buildSchedule</span><span class="w"> </span><span class="p">{</span>
seconds { </span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a><span class="w"> </span><span class="n">seconds</span><span class="w"> </span><span class="p">{</span>
at(5) </span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="w"> </span><span class="n">at</span><span class="p">(</span><span class="m">5</span><span class="p">)</span>
} </span><span id="__span-1-4"><a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-1-5"><a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="p">}</span>
</span><span id="__span-1-6"><a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a>
scheduler.asFlow().onEach { </span><span id="__span-1-7"><a id="__codelineno-1-7" name="__codelineno-1-7" href="#__codelineno-1-7"></a><span class="n">scheduler</span><span class="p">.</span><span class="na">asFlow</span><span class="p">().</span><span class="na">onEach</span><span class="w"> </span><span class="p">{</span>
// this block will be called every minute at 5 seconds </span><span id="__span-1-8"><a id="__codelineno-1-8" name="__codelineno-1-8" href="#__codelineno-1-8"></a><span class="w"> </span><span class="c1">// this block will be called every minute at 5 seconds</span>
}.launchIn(someCoroutineScope) </span><span id="__span-1-9"><a id="__codelineno-1-9" name="__codelineno-1-9" href="#__codelineno-1-9"></a><span class="p">}.</span><span class="na">launchIn</span><span class="p">(</span><span class="n">someCoroutineScope</span><span class="p">)</span>
</code></pre> </span></code></pre></div>
<h3 id="custom-scheduler">Custom scheduler<a class="headerlink" href="#custom-scheduler" title="Permanent link">&para;</a></h3> <h3 id="custom-scheduler">Custom scheduler<a class="headerlink" href="#custom-scheduler" title="Permanent link">&para;</a></h3>
<p>You are always able to use your own realisation of scheduler. For example:</p> <p>You are always able to use your own realisation of scheduler. For example:</p>
<pre><code class="language-kotlin">class RandomScheduler : KronScheduler { <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="kd">class</span><span class="w"> </span><span class="nc">RandomScheduler</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">KronScheduler</span><span class="w"> </span><span class="p">{</span>
override suspend fun next(relatively: DateTime): DateTime { </span><span id="__span-2-2"><a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a><span class="w"> </span><span class="kd">override</span><span class="w"> </span><span class="kd">suspend</span><span class="w"> </span><span class="kd">fun</span><span class="w"> </span><span class="nf">next</span><span class="p">(</span><span class="n">relatively</span><span class="p">:</span><span class="w"> </span><span class="n">DateTime</span><span class="p">):</span><span class="w"> </span><span class="n">DateTime</span><span class="w"> </span><span class="p">{</span>
return relatively + DateTimeSpan(seconds = Random.nextInt() % 60) </span><span id="__span-2-3"><a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">relatively</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">DateTimeSpan</span><span class="p">(</span><span class="n">seconds</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Random</span><span class="p">.</span><span class="na">nextInt</span><span class="p">()</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="m">60</span><span class="p">)</span>
} </span><span id="__span-2-4"><a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-2-5"><a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<p>In the example above we have created <code>RandomScheduler</code>, which will return random next time in range <code>0-60</code> seconds since <code>relatively</code> argument.</p> <p>In the example above we have created <code>RandomScheduler</code>, which will return random next time in range <code>0-60</code> seconds since <code>relatively</code> argument.</p>

View File

@ -1403,22 +1403,22 @@
</tbody> </tbody>
</table> </table>
<p>Example with almost same description:</p> <p>Example with almost same description:</p>
<pre><code>/-------------------- (0-59) ············ Seconds <div class="language-text highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a>/-------------------- (0-59) ············ Seconds
| /------------------ (0-59) ············ Minutes </span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a>| /------------------ (0-59) ············ Minutes
| | /---------------- (0-23) ············ Hours </span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a>| | /---------------- (0-23) ············ Hours
| | | /-------------- (0-30) ············ Days of months </span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a>| | | /-------------- (0-30) ············ Days of months
| | | | /------------ (0-11) ············ Months </span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a>| | | | /------------ (0-11) ············ Months
| | | | | /---------- (optional, any int) Year </span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a>| | | | | /---------- (optional, any int) Year
| | | | | | /-------- (optional) ········ Timezone offset </span><span id="__span-0-7"><a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a>| | | | | | /-------- (optional) ········ Timezone offset
| | | | | | | /----- (optional, 0-6) ··· Week days </span><span id="__span-0-8"><a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a>| | | | | | | /----- (optional, 0-6) ··· Week days
| | | | | | | | /-- (optional, 0-999) · Milliseconds (0 by default) </span><span id="__span-0-9"><a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a>| | | | | | | | /-- (optional, 0-999) · Milliseconds (0 by default)
* * * * * * 0o *w 0ms </span><span id="__span-0-10"><a id="__codelineno-0-10" name="__codelineno-0-10" href="#__codelineno-0-10"></a>* * * * * * 0o *w 0ms
</code></pre> </span></code></pre></div>
<p>Years, timezone, week days and milliseconds are optional settings. Next snippets are equal:</p> <p>Years, timezone, week days and milliseconds are optional settings. Next snippets are equal:</p>
<pre><code>*/15 * * * * <div class="language-text highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a>*/15 * * * *
*/15 * * * * * // with year </span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a>*/15 * * * * * // with year
*/15 * * * * * 0ms // with year and milliseconds </span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a>*/15 * * * * * 0ms // with year and milliseconds
</code></pre> </span></code></pre></div>
<h3 id="supported-syntax">Supported syntax<a class="headerlink" href="#supported-syntax" title="Permanent link">&para;</a></h3> <h3 id="supported-syntax">Supported syntax<a class="headerlink" href="#supported-syntax" title="Permanent link">&para;</a></h3>
<p>Currently the library support next syntax for date/time elements:</p> <p>Currently the library support next syntax for date/time elements:</p>
<ul> <ul>
@ -1432,31 +1432,31 @@
</ul> </ul>
<h4 id="ranges">Ranges<a class="headerlink" href="#ranges" title="Permanent link">&para;</a></h4> <h4 id="ranges">Ranges<a class="headerlink" href="#ranges" title="Permanent link">&para;</a></h4>
<p>Ranges are working like common <code>rangeTo</code> (or <code>..</code>) in kotlin:</p> <p>Ranges are working like common <code>rangeTo</code> (or <code>..</code>) in kotlin:</p>
<pre><code>0-5 * * * * <div class="language-text highlight"><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a>0-5 * * * *
</code></pre> </span></code></pre></div>
<p>In the example above scheduler will trigger every second from the beginning of the minute up to fifth second of minute.</p> <p>In the example above scheduler will trigger every second from the beginning of the minute up to fifth second of minute.</p>
<h4 id="startstep">Start/Step<a class="headerlink" href="#startstep" title="Permanent link">&para;</a></h4> <h4 id="startstep">Start/Step<a class="headerlink" href="#startstep" title="Permanent link">&para;</a></h4>
<p>Start/step is a little bit more complicated syntax. It means <code>start from the first element, repeat triggering every second element</code>. Examples:</p> <p>Start/step is a little bit more complicated syntax. It means <code>start from the first element, repeat triggering every second element</code>. Examples:</p>
<pre><code>5/15 * * * * <div class="language-text highlight"><pre><span></span><code><span id="__span-3-1"><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a>5/15 * * * *
</code></pre> </span></code></pre></div>
<p>Means that each minute starting from fifth second it will repeat triggering every fifteenth second: <code>5, 20, 35, 50</code>.</p> <p>Means that each minute starting from fifth second it will repeat triggering every fifteenth second: <code>5, 20, 35, 50</code>.</p>
<h4 id="every">Every<a class="headerlink" href="#every" title="Permanent link">&para;</a></h4> <h4 id="every">Every<a class="headerlink" href="#every" title="Permanent link">&para;</a></h4>
<p>Every is more simple syntax and could be explained as a shortcut for <code>0/{int}</code>. Example:</p> <p>Every is more simple syntax and could be explained as a shortcut for <code>0/{int}</code>. Example:</p>
<pre><code>*/15 * * * * <div class="language-text highlight"><pre><span></span><code><span id="__span-4-1"><a id="__codelineno-4-1" name="__codelineno-4-1" href="#__codelineno-4-1"></a>*/15 * * * *
</code></pre> </span></code></pre></div>
<p>Means that each minute it will repeat triggering every fifteenth second: <code>0, 15, 30, 45</code>.</p> <p>Means that each minute it will repeat triggering every fifteenth second: <code>0, 15, 30, 45</code>.</p>
<h4 id="just-at-the-time">Just at the time<a class="headerlink" href="#just-at-the-time" title="Permanent link">&para;</a></h4> <h4 id="just-at-the-time">Just at the time<a class="headerlink" href="#just-at-the-time" title="Permanent link">&para;</a></h4>
<p>The most simple syntax. It means, that scheduler will call triggering every time when element was reached:</p> <p>The most simple syntax. It means, that scheduler will call triggering every time when element was reached:</p>
<pre><code>15 * * * * <div class="language-text highlight"><pre><span></span><code><span id="__span-5-1"><a id="__codelineno-5-1" name="__codelineno-5-1" href="#__codelineno-5-1"></a>15 * * * *
</code></pre> </span></code></pre></div>
<p>Means that each minute scheduler will call triggering at the fifteenth second.</p> <p>Means that each minute scheduler will call triggering at the fifteenth second.</p>
<h4 id="listing">Listing<a class="headerlink" href="#listing" title="Permanent link">&para;</a></h4> <h4 id="listing">Listing<a class="headerlink" href="#listing" title="Permanent link">&para;</a></h4>
<p>All the previous elements can be combined with listing. Lets just see several examples:</p> <p>All the previous elements can be combined with listing. Lets just see several examples:</p>
<pre><code>0,10 * * * * <div class="language-text highlight"><pre><span></span><code><span id="__span-6-1"><a id="__codelineno-6-1" name="__codelineno-6-1" href="#__codelineno-6-1"></a>0,10 * * * *
</code></pre> </span></code></pre></div>
<p>Will trigger every minute at the <code>0</code> and <code>10</code> seconds (see <a href="#just-at-the-time">Just at the time</a>)</p> <p>Will trigger every minute at the <code>0</code> and <code>10</code> seconds (see <a href="#just-at-the-time">Just at the time</a>)</p>
<pre><code>0-5,10 * * * * <div class="language-text highlight"><pre><span></span><code><span id="__span-7-1"><a id="__codelineno-7-1" name="__codelineno-7-1" href="#__codelineno-7-1"></a>0-5,10 * * * *
</code></pre> </span></code></pre></div>
<p>Will trigger every minute from <code>0</code> to <code>5</code> seconds and at the <code>10</code> seconds (see <a href="#ranges">Ranges</a>)</p> <p>Will trigger every minute from <code>0</code> to <code>5</code> seconds and at the <code>10</code> seconds (see <a href="#ranges">Ranges</a>)</p>
<h3 id="examples">Examples<a class="headerlink" href="#examples" title="Permanent link">&para;</a></h3> <h3 id="examples">Examples<a class="headerlink" href="#examples" title="Permanent link">&para;</a></h3>
<ul> <ul>

View File

@ -1249,8 +1249,8 @@
<p>First of all, this library will be useful for long uptime applications which have some tasks to do from time to time.</p> <p>First of all, this library will be useful for long uptime applications which have some tasks to do from time to time.</p>
<h4 id="how-to-use-crontab-like-syntax">How to use crontab-like syntax?<a class="headerlink" href="#how-to-use-crontab-like-syntax" title="Permanent link">&para;</a></h4> <h4 id="how-to-use-crontab-like-syntax">How to use crontab-like syntax?<a class="headerlink" href="#how-to-use-crontab-like-syntax" title="Permanent link">&para;</a></h4>
<p>In two words, you should call <code>buildSchedule</code> or <code>createSimpleScheduler</code>:</p> <p>In two words, you should call <code>buildSchedule</code> or <code>createSimpleScheduler</code>:</p>
<pre><code class="language-kotlin">buildSchedule(&quot;5 * * * *&quot;).asFlow().collect { /* do something */ } <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="n">buildSchedule</span><span class="p">(</span><span class="s">&quot;5 * * * *&quot;</span><span class="p">).</span><span class="na">asFlow</span><span class="p">().</span><span class="na">collect</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* do something */</span><span class="w"> </span><span class="p">}</span>
</code></pre> </span></code></pre></div>
<p>You can read more about syntax in <a href="../describing/string-format.html">String format</a> section.</p> <p>You can read more about syntax in <a href="../describing/string-format.html">String format</a> section.</p>

View File

@ -1294,15 +1294,15 @@
You always able to create your own scheduler. In this section will be presented different ways and examples around standard <code>CronDateTimeScheduler</code> builders <code>buildSchedule</code>. You can read about schedulers in <a href="../describing/krontabscheduler.html">KrontabScheduler</a></p> You always able to create your own scheduler. In this section will be presented different ways and examples around standard <code>CronDateTimeScheduler</code> builders <code>buildSchedule</code>. You can read about schedulers in <a href="../describing/krontabscheduler.html">KrontabScheduler</a></p>
</div> </div>
<p>Currently, <code>buildSchedule</code> is the recommended start point for every scheduler. Usually, it is look like:</p> <p>Currently, <code>buildSchedule</code> is the recommended start point for every scheduler. Usually, it is look like:</p>
<pre><code class="language-kotlin">val scheduler = buildSchedule(&quot;5 * * * *&quot;) <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">scheduler</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buildSchedule</span><span class="p">(</span><span class="s">&quot;5 * * * *&quot;</span><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>Or:</p> <p>Or:</p>
<pre><code class="language-kotlin">val scheduler = buildSchedule { <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">scheduler</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buildSchedule</span><span class="w"> </span><span class="p">{</span>
seconds { </span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a><span class="w"> </span><span class="n">seconds</span><span class="w"> </span><span class="p">{</span>
at(5) </span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="w"> </span><span class="n">at</span><span class="p">(</span><span class="m">5</span><span class="p">)</span>
} </span><span id="__span-1-4"><a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-1-5"><a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<p>On the top of any <code>KronScheduler</code> currently there are several groups of extensions:</p> <p>On the top of any <code>KronScheduler</code> currently there are several groups of extensions:</p>
<ul> <ul>
<li>Executes</li> <li>Executes</li>

View File

@ -1300,20 +1300,20 @@
<h3 id="notice-about-repository">Notice about repository<a class="headerlink" href="#notice-about-repository" title="Permanent link">&para;</a></h3> <h3 id="notice-about-repository">Notice about repository<a class="headerlink" href="#notice-about-repository" title="Permanent link">&para;</a></h3>
<p>To use this library, you will need to include <code>MavenCentral</code> repository in you project</p> <p>To use this library, you will need to include <code>MavenCentral</code> repository in you project</p>
<h6 id="buildgradle">build.gradle<a class="headerlink" href="#buildgradle" title="Permanent link">&para;</a></h6> <h6 id="buildgradle">build.gradle<a class="headerlink" href="#buildgradle" title="Permanent link">&para;</a></h6>
<pre><code class="language-groovy">mavenCentral() <div class="language-groovy highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="n">mavenCentral</span><span class="o">()</span>
</code></pre> </span></code></pre></div>
<h3 id="dependencies">Dependencies<a class="headerlink" href="#dependencies" title="Permanent link">&para;</a></h3> <h3 id="dependencies">Dependencies<a class="headerlink" href="#dependencies" title="Permanent link">&para;</a></h3>
<p>Next snippets must be placed into your <code>dependencies</code> part of <code>build.gradle</code> (for gradle) or <code>pom.xml</code> (for maven).</p> <p>Next snippets must be placed into your <code>dependencies</code> part of <code>build.gradle</code> (for gradle) or <code>pom.xml</code> (for maven).</p>
<h4 id="gradle">Gradle<a class="headerlink" href="#gradle" title="Permanent link">&para;</a></h4> <h4 id="gradle">Gradle<a class="headerlink" href="#gradle" title="Permanent link">&para;</a></h4>
<pre><code class="language-groovy">implementation &quot;dev.inmo:krontab:$krontab_version&quot; <div class="language-groovy highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="n">implementation</span><span class="w"> </span><span class="s2">&quot;dev.inmo:krontab:$krontab_version&quot;</span>
</code></pre> </span></code></pre></div>
<h4 id="maven">Maven<a class="headerlink" href="#maven" title="Permanent link">&para;</a></h4> <h4 id="maven">Maven<a class="headerlink" href="#maven" title="Permanent link">&para;</a></h4>
<pre><code class="language-xml">&lt;dependency&gt; <div class="language-xml highlight"><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="nt">&lt;dependency&gt;</span>
&lt;groupId&gt;dev.inmo&lt;/groupId&gt; </span><span id="__span-2-2"><a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a><span class="w"> </span><span class="nt">&lt;groupId&gt;</span>dev.inmo<span class="nt">&lt;/groupId&gt;</span>
&lt;artifactId&gt;krontab&lt;/artifactId&gt; </span><span id="__span-2-3"><a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="w"> </span><span class="nt">&lt;artifactId&gt;</span>krontab<span class="nt">&lt;/artifactId&gt;</span>
&lt;version&gt;${krontab_version}&lt;/version&gt; </span><span id="__span-2-4"><a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="w"> </span><span class="nt">&lt;version&gt;</span>${krontab_version}<span class="nt">&lt;/version&gt;</span>
&lt;/dependency&gt; </span><span id="__span-2-5"><a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a><span class="nt">&lt;/dependency&gt;</span>
</code></pre> </span></code></pre></div>

View File

@ -1242,89 +1242,89 @@ On this page all the messages will be just simple <code>String</code>, but you m
<li>Tag, Message, Throwable (Optional)</li> <li>Tag, Message, Throwable (Optional)</li>
</ul> </ul>
<p>So, when you want to log some expected exception, there are three common ways to do it:</p> <p>So, when you want to log some expected exception, there are three common ways to do it:</p>
<pre><code class="language-kotlin">val logger = KSLog.default <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">logger</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">KSLog</span><span class="p">.</span><span class="na">default</span>
</span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a>
// with callback </span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="c1">// with callback</span>
logger.info(tag, throwable) { </span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="n">logger</span><span class="p">.</span><span class="na">info</span><span class="p">(</span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
&quot;Some your message for this event&quot; </span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="w"> </span><span class="s">&quot;Some your message for this event&quot;</span>
} </span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a><span class="p">}</span>
</span><span id="__span-0-7"><a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a>
// with suspendable callback </span><span id="__span-0-8"><a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="c1">// with suspendable callback</span>
logger.infoS(tag, throwable) { </span><span id="__span-0-9"><a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a><span class="n">logger</span><span class="p">.</span><span class="na">infoS</span><span class="p">(</span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
withContext(Dispatchers.Default) { </span><span id="__span-0-10"><a id="__codelineno-0-10" name="__codelineno-0-10" href="#__codelineno-0-10"></a><span class="w"> </span><span class="n">withContext</span><span class="p">(</span><span class="n">Dispatchers</span><span class="p">.</span><span class="na">Default</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
&quot;Some your message for this event&quot; </span><span id="__span-0-11"><a id="__codelineno-0-11" name="__codelineno-0-11" href="#__codelineno-0-11"></a><span class="w"> </span><span class="s">&quot;Some your message for this event&quot;</span>
} </span><span id="__span-0-12"><a id="__codelineno-0-12" name="__codelineno-0-12" href="#__codelineno-0-12"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-0-13"><a id="__codelineno-0-13" name="__codelineno-0-13" href="#__codelineno-0-13"></a><span class="p">}</span>
</span><span id="__span-0-14"><a id="__codelineno-0-14" name="__codelineno-0-14" href="#__codelineno-0-14"></a>
// Just with message </span><span id="__span-0-15"><a id="__codelineno-0-15" name="__codelineno-0-15" href="#__codelineno-0-15"></a><span class="c1">// Just with message</span>
logger.info(&quot;Some your message for this event&quot;, throwable) </span><span id="__span-0-16"><a id="__codelineno-0-16" name="__codelineno-0-16" href="#__codelineno-0-16"></a><span class="n">logger</span><span class="p">.</span><span class="na">info</span><span class="p">(</span><span class="s">&quot;Some your message for this event&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span>
</span><span id="__span-0-17"><a id="__codelineno-0-17" name="__codelineno-0-17" href="#__codelineno-0-17"></a>
// With message and tag as strings </span><span id="__span-0-18"><a id="__codelineno-0-18" name="__codelineno-0-18" href="#__codelineno-0-18"></a><span class="c1">// With message and tag as strings</span>
logger.info(tag, &quot;Some your message for this event&quot;, throwable) </span><span id="__span-0-19"><a id="__codelineno-0-19" name="__codelineno-0-19" href="#__codelineno-0-19"></a><span class="n">logger</span><span class="p">.</span><span class="na">info</span><span class="p">(</span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Some your message for this event&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>Of course, any of this calls can be shortenned:</p> <p>Of course, any of this calls can be shortenned:</p>
<pre><code class="language-kotlin">val logger = KSLog.default <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">logger</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">KSLog</span><span class="p">.</span><span class="na">default</span>
</span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a>
// with callback </span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="c1">// with callback</span>
logger.i(tag, throwable) { </span><span id="__span-1-4"><a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="n">logger</span><span class="p">.</span><span class="na">i</span><span class="p">(</span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
&quot;Some your message for this event&quot; </span><span id="__span-1-5"><a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="w"> </span><span class="s">&quot;Some your message for this event&quot;</span>
} </span><span id="__span-1-6"><a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a><span class="p">}</span>
</span><span id="__span-1-7"><a id="__codelineno-1-7" name="__codelineno-1-7" href="#__codelineno-1-7"></a>
// with suspendable callback </span><span id="__span-1-8"><a id="__codelineno-1-8" name="__codelineno-1-8" href="#__codelineno-1-8"></a><span class="c1">// with suspendable callback</span>
logger.iS(tag, throwable) { </span><span id="__span-1-9"><a id="__codelineno-1-9" name="__codelineno-1-9" href="#__codelineno-1-9"></a><span class="n">logger</span><span class="p">.</span><span class="na">iS</span><span class="p">(</span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
withContext(Dispatchers.Default) { </span><span id="__span-1-10"><a id="__codelineno-1-10" name="__codelineno-1-10" href="#__codelineno-1-10"></a><span class="w"> </span><span class="n">withContext</span><span class="p">(</span><span class="n">Dispatchers</span><span class="p">.</span><span class="na">Default</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
&quot;Some your message for this event&quot; </span><span id="__span-1-11"><a id="__codelineno-1-11" name="__codelineno-1-11" href="#__codelineno-1-11"></a><span class="w"> </span><span class="s">&quot;Some your message for this event&quot;</span>
} </span><span id="__span-1-12"><a id="__codelineno-1-12" name="__codelineno-1-12" href="#__codelineno-1-12"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-1-13"><a id="__codelineno-1-13" name="__codelineno-1-13" href="#__codelineno-1-13"></a><span class="p">}</span>
</span><span id="__span-1-14"><a id="__codelineno-1-14" name="__codelineno-1-14" href="#__codelineno-1-14"></a>
// Just with message </span><span id="__span-1-15"><a id="__codelineno-1-15" name="__codelineno-1-15" href="#__codelineno-1-15"></a><span class="c1">// Just with message</span>
logger.i(&quot;Some your message for this event&quot;, throwable) </span><span id="__span-1-16"><a id="__codelineno-1-16" name="__codelineno-1-16" href="#__codelineno-1-16"></a><span class="n">logger</span><span class="p">.</span><span class="na">i</span><span class="p">(</span><span class="s">&quot;Some your message for this event&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span>
</span><span id="__span-1-17"><a id="__codelineno-1-17" name="__codelineno-1-17" href="#__codelineno-1-17"></a>
// With message and tag as strings </span><span id="__span-1-18"><a id="__codelineno-1-18" name="__codelineno-1-18" href="#__codelineno-1-18"></a><span class="c1">// With message and tag as strings</span>
logger.i(tag, &quot;Some your message for this event&quot;, throwable) </span><span id="__span-1-19"><a id="__codelineno-1-19" name="__codelineno-1-19" href="#__codelineno-1-19"></a><span class="n">logger</span><span class="p">.</span><span class="na">i</span><span class="p">(</span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Some your message for this event&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>There is special shortcat - for base <code>performLog</code>. In that case the only change is that you will require to pass the <code>LogLevel</code> more obviously:</p> <p>There is special shortcat - for base <code>performLog</code>. In that case the only change is that you will require to pass the <code>LogLevel</code> more obviously:</p>
<pre><code class="language-kotlin">val logger = KSLog.default <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">logger</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">KSLog</span><span class="p">.</span><span class="na">default</span>
</span><span id="__span-2-2"><a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a>
// with callback </span><span id="__span-2-3"><a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="c1">// with callback</span>
logger.log(LogLevel.INFO, tag, throwable) { </span><span id="__span-2-4"><a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="n">logger</span><span class="p">.</span><span class="na">log</span><span class="p">(</span><span class="n">LogLevel</span><span class="p">.</span><span class="na">INFO</span><span class="p">,</span><span class="w"> </span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
&quot;Some your message for this event&quot; </span><span id="__span-2-5"><a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a><span class="w"> </span><span class="s">&quot;Some your message for this event&quot;</span>
} </span><span id="__span-2-6"><a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a><span class="p">}</span>
</span><span id="__span-2-7"><a id="__codelineno-2-7" name="__codelineno-2-7" href="#__codelineno-2-7"></a>
// with suspendable callback </span><span id="__span-2-8"><a id="__codelineno-2-8" name="__codelineno-2-8" href="#__codelineno-2-8"></a><span class="c1">// with suspendable callback</span>
logger.logS(LogLevel.INFO, tag, throwable) { </span><span id="__span-2-9"><a id="__codelineno-2-9" name="__codelineno-2-9" href="#__codelineno-2-9"></a><span class="n">logger</span><span class="p">.</span><span class="na">logS</span><span class="p">(</span><span class="n">LogLevel</span><span class="p">.</span><span class="na">INFO</span><span class="p">,</span><span class="w"> </span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
withContext(Dispatchers.Default) { </span><span id="__span-2-10"><a id="__codelineno-2-10" name="__codelineno-2-10" href="#__codelineno-2-10"></a><span class="w"> </span><span class="n">withContext</span><span class="p">(</span><span class="n">Dispatchers</span><span class="p">.</span><span class="na">Default</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
&quot;Some your message for this event&quot; </span><span id="__span-2-11"><a id="__codelineno-2-11" name="__codelineno-2-11" href="#__codelineno-2-11"></a><span class="w"> </span><span class="s">&quot;Some your message for this event&quot;</span>
} </span><span id="__span-2-12"><a id="__codelineno-2-12" name="__codelineno-2-12" href="#__codelineno-2-12"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-2-13"><a id="__codelineno-2-13" name="__codelineno-2-13" href="#__codelineno-2-13"></a><span class="p">}</span>
</span><span id="__span-2-14"><a id="__codelineno-2-14" name="__codelineno-2-14" href="#__codelineno-2-14"></a>
// Just with message </span><span id="__span-2-15"><a id="__codelineno-2-15" name="__codelineno-2-15" href="#__codelineno-2-15"></a><span class="c1">// Just with message</span>
logger.log(LogLevel.INFO, &quot;Some your message for this event&quot;, throwable) </span><span id="__span-2-16"><a id="__codelineno-2-16" name="__codelineno-2-16" href="#__codelineno-2-16"></a><span class="n">logger</span><span class="p">.</span><span class="na">log</span><span class="p">(</span><span class="n">LogLevel</span><span class="p">.</span><span class="na">INFO</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Some your message for this event&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span>
</span><span id="__span-2-17"><a id="__codelineno-2-17" name="__codelineno-2-17" href="#__codelineno-2-17"></a>
// With message and tag as strings </span><span id="__span-2-18"><a id="__codelineno-2-18" name="__codelineno-2-18" href="#__codelineno-2-18"></a><span class="c1">// With message and tag as strings</span>
logger.log(LogLevel.INFO, tag, &quot;Some your message for this event&quot;, throwable) </span><span id="__span-2-19"><a id="__codelineno-2-19" name="__codelineno-2-19" href="#__codelineno-2-19"></a><span class="n">logger</span><span class="p">.</span><span class="na">log</span><span class="p">(</span><span class="n">LogLevel</span><span class="p">.</span><span class="na">INFO</span><span class="p">,</span><span class="w"> </span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Some your message for this event&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>OR</p> <p>OR</p>
<pre><code class="language-kotlin">val logger = KSLog.default <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-3-1"><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">logger</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">KSLog</span><span class="p">.</span><span class="na">default</span>
</span><span id="__span-3-2"><a id="__codelineno-3-2" name="__codelineno-3-2" href="#__codelineno-3-2"></a>
// with callback </span><span id="__span-3-3"><a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a><span class="c1">// with callback</span>
logger.l(LogLevel.INFO, tag, throwable) { </span><span id="__span-3-4"><a id="__codelineno-3-4" name="__codelineno-3-4" href="#__codelineno-3-4"></a><span class="n">logger</span><span class="p">.</span><span class="na">l</span><span class="p">(</span><span class="n">LogLevel</span><span class="p">.</span><span class="na">INFO</span><span class="p">,</span><span class="w"> </span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
&quot;Some your message for this event&quot; </span><span id="__span-3-5"><a id="__codelineno-3-5" name="__codelineno-3-5" href="#__codelineno-3-5"></a><span class="w"> </span><span class="s">&quot;Some your message for this event&quot;</span>
} </span><span id="__span-3-6"><a id="__codelineno-3-6" name="__codelineno-3-6" href="#__codelineno-3-6"></a><span class="p">}</span>
</span><span id="__span-3-7"><a id="__codelineno-3-7" name="__codelineno-3-7" href="#__codelineno-3-7"></a>
// with suspendable callback </span><span id="__span-3-8"><a id="__codelineno-3-8" name="__codelineno-3-8" href="#__codelineno-3-8"></a><span class="c1">// with suspendable callback</span>
logger.lS(LogLevel.INFO, tag, throwable) { </span><span id="__span-3-9"><a id="__codelineno-3-9" name="__codelineno-3-9" href="#__codelineno-3-9"></a><span class="n">logger</span><span class="p">.</span><span class="na">lS</span><span class="p">(</span><span class="n">LogLevel</span><span class="p">.</span><span class="na">INFO</span><span class="p">,</span><span class="w"> </span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
withContext(Dispatchers.Default) { </span><span id="__span-3-10"><a id="__codelineno-3-10" name="__codelineno-3-10" href="#__codelineno-3-10"></a><span class="w"> </span><span class="n">withContext</span><span class="p">(</span><span class="n">Dispatchers</span><span class="p">.</span><span class="na">Default</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
&quot;Some your message for this event&quot; </span><span id="__span-3-11"><a id="__codelineno-3-11" name="__codelineno-3-11" href="#__codelineno-3-11"></a><span class="w"> </span><span class="s">&quot;Some your message for this event&quot;</span>
} </span><span id="__span-3-12"><a id="__codelineno-3-12" name="__codelineno-3-12" href="#__codelineno-3-12"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-3-13"><a id="__codelineno-3-13" name="__codelineno-3-13" href="#__codelineno-3-13"></a><span class="p">}</span>
</span><span id="__span-3-14"><a id="__codelineno-3-14" name="__codelineno-3-14" href="#__codelineno-3-14"></a>
// Just with message </span><span id="__span-3-15"><a id="__codelineno-3-15" name="__codelineno-3-15" href="#__codelineno-3-15"></a><span class="c1">// Just with message</span>
logger.l(LogLevel.INFO, &quot;Some your message for this event&quot;, throwable) </span><span id="__span-3-16"><a id="__codelineno-3-16" name="__codelineno-3-16" href="#__codelineno-3-16"></a><span class="n">logger</span><span class="p">.</span><span class="na">l</span><span class="p">(</span><span class="n">LogLevel</span><span class="p">.</span><span class="na">INFO</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Some your message for this event&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span>
</span><span id="__span-3-17"><a id="__codelineno-3-17" name="__codelineno-3-17" href="#__codelineno-3-17"></a>
// With message and tag as strings </span><span id="__span-3-18"><a id="__codelineno-3-18" name="__codelineno-3-18" href="#__codelineno-3-18"></a><span class="c1">// With message and tag as strings</span>
logger.l(LogLevel.INFO, tag, &quot;Some your message for this event&quot;, throwable) </span><span id="__span-3-19"><a id="__codelineno-3-19" name="__codelineno-3-19" href="#__codelineno-3-19"></a><span class="n">logger</span><span class="p">.</span><span class="na">l</span><span class="p">(</span><span class="n">LogLevel</span><span class="p">.</span><span class="na">INFO</span><span class="p">,</span><span class="w"> </span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Some your message for this event&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span>
</code></pre> </span></code></pre></div>

View File

@ -1394,30 +1394,30 @@
<h2 id="dependency-installation">Dependency installation<a class="headerlink" href="#dependency-installation" title="Permanent link">&para;</a></h2> <h2 id="dependency-installation">Dependency installation<a class="headerlink" href="#dependency-installation" title="Permanent link">&para;</a></h2>
<p><a href="https://maven-badges.herokuapp.com/maven-central/dev.inmo/kslog"><img alt="Maven Central" src="https://maven-badges.herokuapp.com/maven-central/dev.inmo/kslog/badge.svg" /></a></p> <p><a href="https://maven-badges.herokuapp.com/maven-central/dev.inmo/kslog"><img alt="Maven Central" src="https://maven-badges.herokuapp.com/maven-central/dev.inmo/kslog/badge.svg" /></a></p>
<h3 id="gradle-groovy">Gradle (Groovy)<a class="headerlink" href="#gradle-groovy" title="Permanent link">&para;</a></h3> <h3 id="gradle-groovy">Gradle (Groovy)<a class="headerlink" href="#gradle-groovy" title="Permanent link">&para;</a></h3>
<pre><code class="language-groovy">implementation &quot;dev.inmo:kslog:$kslog_version&quot; <div class="language-groovy highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="n">implementation</span><span class="w"> </span><span class="s2">&quot;dev.inmo:kslog:$kslog_version&quot;</span>
</code></pre> </span></code></pre></div>
<h3 id="gradle-kotlin-script">Gradle (Kotlin Script)<a class="headerlink" href="#gradle-kotlin-script" title="Permanent link">&para;</a></h3> <h3 id="gradle-kotlin-script">Gradle (Kotlin Script)<a class="headerlink" href="#gradle-kotlin-script" title="Permanent link">&para;</a></h3>
<pre><code class="language-kotlin">implementation(&quot;dev.inmo:kslog:$kslog_version&quot;) <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="n">implementation</span><span class="p">(</span><span class="s">&quot;dev.inmo:kslog:</span><span class="si">$</span><span class="n">kslog_version</span><span class="s">&quot;</span><span class="p">)</span>
</code></pre> </span></code></pre></div>
<h3 id="maven-pom">Maven (pom)<a class="headerlink" href="#maven-pom" title="Permanent link">&para;</a></h3> <h3 id="maven-pom">Maven (pom)<a class="headerlink" href="#maven-pom" title="Permanent link">&para;</a></h3>
<pre><code class="language-xml">&lt;dependency&gt; <div class="language-xml highlight"><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="nt">&lt;dependency&gt;</span>
&lt;groupId&gt;dev.inmo&lt;/groupId&gt; </span><span id="__span-2-2"><a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a><span class="w"> </span><span class="nt">&lt;groupId&gt;</span>dev.inmo<span class="nt">&lt;/groupId&gt;</span>
&lt;artifactId&gt;kslog&lt;/artifactId&gt; </span><span id="__span-2-3"><a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="w"> </span><span class="nt">&lt;artifactId&gt;</span>kslog<span class="nt">&lt;/artifactId&gt;</span>
&lt;version&gt;${kslog_version}&lt;/version&gt; </span><span id="__span-2-4"><a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="w"> </span><span class="nt">&lt;version&gt;</span>${kslog_version}<span class="nt">&lt;/version&gt;</span>
&lt;/dependency&gt; </span><span id="__span-2-5"><a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a><span class="nt">&lt;/dependency&gt;</span>
</code></pre> </span></code></pre></div>
<h2 id="setup-in-code">Setup in code<a class="headerlink" href="#setup-in-code" title="Permanent link">&para;</a></h2> <h2 id="setup-in-code">Setup in code<a class="headerlink" href="#setup-in-code" title="Permanent link">&para;</a></h2>
<p>The main point in setup in your code is to setup default logger:</p> <p>The main point in setup in your code is to setup default logger:</p>
<pre><code class="language-kotlin">KSLog.default = KSLog(&quot;defaultTag&quot;) <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-3-1"><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a><span class="n">KSLog</span><span class="p">.</span><span class="na">default</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">KSLog</span><span class="p">(</span><span class="s">&quot;defaultTag&quot;</span><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>You may use custom <code>messageFormatter</code> in any of <code>KSLog</code> factory to customize output of <code>KSLog</code> logging. For example:</p> <p>You may use custom <code>messageFormatter</code> in any of <code>KSLog</code> factory to customize output of <code>KSLog</code> logging. For example:</p>
<pre><code class="language-kotlin">KSLog( <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-4-1"><a id="__codelineno-4-1" name="__codelineno-4-1" href="#__codelineno-4-1"></a><span class="n">KSLog</span><span class="p">(</span>
&quot;loggingWithCustomFormat&quot;, </span><span id="__span-4-2"><a id="__codelineno-4-2" name="__codelineno-4-2" href="#__codelineno-4-2"></a><span class="w"> </span><span class="s">&quot;loggingWithCustomFormat&quot;</span><span class="p">,</span>
messageFormatter = { level, tag, message, throwable -&gt; </span><span id="__span-4-3"><a id="__codelineno-4-3" name="__codelineno-4-3" href="#__codelineno-4-3"></a><span class="w"> </span><span class="n">messageFormatter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">level</span><span class="p">,</span><span class="w"> </span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="n">message</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="w"> </span><span class="o">-&gt;</span>
println(&quot;[$level] $tag - $message: $throwable&quot;) </span><span id="__span-4-4"><a id="__codelineno-4-4" name="__codelineno-4-4" href="#__codelineno-4-4"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="s">&quot;[</span><span class="si">$</span><span class="n">level</span><span class="s">] </span><span class="si">$</span><span class="n">tag</span><span class="s"> - </span><span class="si">$</span><span class="n">message</span><span class="s">: </span><span class="si">$</span><span class="n">throwable</span><span class="s">&quot;</span><span class="p">)</span>
} </span><span id="__span-4-5"><a id="__codelineno-4-5" name="__codelineno-4-5" href="#__codelineno-4-5"></a><span class="w"> </span><span class="p">}</span>
) </span><span id="__span-4-6"><a id="__codelineno-4-6" name="__codelineno-4-6" href="#__codelineno-4-6"></a><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>Additionally you may use one of several different settings:</p> <p>Additionally you may use one of several different settings:</p>
<ul> <ul>
<li><code>minLoggingLevel</code> - minimal logging level for the log which will be logged. The order of log level is next:</li> <li><code>minLoggingLevel</code> - minimal logging level for the log which will be logged. The order of log level is next:</li>
@ -1431,11 +1431,11 @@
<li><code>firstLevel</code>,<code>secondLevel</code>,<code>otherLevels</code> - as <code>levels</code>, but <code>vararg</code> :)</li> <li><code>firstLevel</code>,<code>secondLevel</code>,<code>otherLevels</code> - as <code>levels</code>, but <code>vararg</code> :)</li>
</ul> </ul>
<p>In case you are passing <code>minLoggingLevel</code>, the <strong>level and more important levels</strong> will be passed to logs. For example, when you are settings up your logger as in next snippet:</p> <p>In case you are passing <code>minLoggingLevel</code>, the <strong>level and more important levels</strong> will be passed to logs. For example, when you are settings up your logger as in next snippet:</p>
<pre><code class="language-kotlin">val logger = KSLog( <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-5-1"><a id="__codelineno-5-1" name="__codelineno-5-1" href="#__codelineno-5-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">logger</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">KSLog</span><span class="p">(</span>
&quot;exampleTag&quot;, </span><span id="__span-5-2"><a id="__codelineno-5-2" name="__codelineno-5-2" href="#__codelineno-5-2"></a><span class="w"> </span><span class="s">&quot;exampleTag&quot;</span><span class="p">,</span>
minLoggingLevel = LogLevel.INFO </span><span id="__span-5-3"><a id="__codelineno-5-3" name="__codelineno-5-3" href="#__codelineno-5-3"></a><span class="w"> </span><span class="n">minLoggingLevel</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">LogLevel</span><span class="p">.</span><span class="na">INFO</span>
) </span><span id="__span-5-4"><a id="__codelineno-5-4" name="__codelineno-5-4" href="#__codelineno-5-4"></a><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>The next levels will be logged with <code>logger</code>:</p> <p>The next levels will be logged with <code>logger</code>:</p>
<ul> <ul>
<li><code>INFO</code></li> <li><code>INFO</code></li>
@ -1446,55 +1446,55 @@
<h2 id="special-loggers">Special loggers<a class="headerlink" href="#special-loggers" title="Permanent link">&para;</a></h2> <h2 id="special-loggers">Special loggers<a class="headerlink" href="#special-loggers" title="Permanent link">&para;</a></h2>
<h3 id="callbackkslog">CallbackKSLog<a class="headerlink" href="#callbackkslog" title="Permanent link">&para;</a></h3> <h3 id="callbackkslog">CallbackKSLog<a class="headerlink" href="#callbackkslog" title="Permanent link">&para;</a></h3>
<p>It is logger which will call incoming <code>performLogCallback</code> on each logging. This logger can be create simply with one callback:</p> <p>It is logger which will call incoming <code>performLogCallback</code> on each logging. This logger can be create simply with one callback:</p>
<pre><code class="language-kotlin">KSLog { level, tag, message, throwable -&gt; <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-6-1"><a id="__codelineno-6-1" name="__codelineno-6-1" href="#__codelineno-6-1"></a><span class="n">KSLog</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">level</span><span class="p">,</span><span class="w"> </span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="n">message</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="w"> </span><span class="o">-&gt;</span>
println(&quot;[$level] $tag - $message: $throwable&quot;) </span><span id="__span-6-2"><a id="__codelineno-6-2" name="__codelineno-6-2" href="#__codelineno-6-2"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="s">&quot;[</span><span class="si">$</span><span class="n">level</span><span class="s">] </span><span class="si">$</span><span class="n">tag</span><span class="s"> - </span><span class="si">$</span><span class="n">message</span><span class="s">: </span><span class="si">$</span><span class="n">throwable</span><span class="s">&quot;</span><span class="p">)</span>
} </span><span id="__span-6-3"><a id="__codelineno-6-3" name="__codelineno-6-3" href="#__codelineno-6-3"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<h3 id="taglogger">TagLogger<a class="headerlink" href="#taglogger" title="Permanent link">&para;</a></h3> <h3 id="taglogger">TagLogger<a class="headerlink" href="#taglogger" title="Permanent link">&para;</a></h3>
<p>It is simple value class which can be used for zero-cost usage of some tag and calling for <code>KSLog.default</code>. For example, if you will create tag logger with next code:</p> <p>It is simple value class which can be used for zero-cost usage of some tag and calling for <code>KSLog.default</code>. For example, if you will create tag logger with next code:</p>
<pre><code class="language-kotlin">val logger = TagLogger(&quot;tagLoggerTag&quot;) <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-7-1"><a id="__codelineno-7-1" name="__codelineno-7-1" href="#__codelineno-7-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">logger</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">TagLogger</span><span class="p">(</span><span class="s">&quot;tagLoggerTag&quot;</span><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>The <code>logger</code> will call <code>KSLog.default</code> with the tag <code>tagLoggerTag</code> on each calling of logging.</p> <p>The <code>logger</code> will call <code>KSLog.default</code> with the tag <code>tagLoggerTag</code> on each calling of logging.</p>
<h3 id="filterkslog">FilterKSLog<a class="headerlink" href="#filterkslog" title="Permanent link">&para;</a></h3> <h3 id="filterkslog">FilterKSLog<a class="headerlink" href="#filterkslog" title="Permanent link">&para;</a></h3>
<p>This pretty simple logger will call its <code>fallbackLogger</code> only in cases when incoming <code>messageFilter</code> will return true for logging:</p> <p>This pretty simple logger will call its <code>fallbackLogger</code> only in cases when incoming <code>messageFilter</code> will return true for logging:</p>
<pre><code class="language-kotlin">val baseLogger = KSLog(&quot;base&quot;) // log everything with the tag `base` if not set other <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-8-1"><a id="__codelineno-8-1" name="__codelineno-8-1" href="#__codelineno-8-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">baseLogger</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">KSLog</span><span class="p">(</span><span class="s">&quot;base&quot;</span><span class="p">)</span><span class="w"> </span><span class="c1">// log everything with the tag `base` if not set other</span>
val filtered = baseLogger.filtered { _, t, _ -&gt; </span><span id="__span-8-2"><a id="__codelineno-8-2" name="__codelineno-8-2" href="#__codelineno-8-2"></a><span class="kd">val</span><span class="w"> </span><span class="nv">filtered</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">baseLogger</span><span class="p">.</span><span class="na">filtered</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">t</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">-&gt;</span>
t == &quot;base&quot; </span><span id="__span-8-3"><a id="__codelineno-8-3" name="__codelineno-8-3" href="#__codelineno-8-3"></a><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s">&quot;base&quot;</span>
} </span><span id="__span-8-4"><a id="__codelineno-8-4" name="__codelineno-8-4" href="#__codelineno-8-4"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<p>In the example above <code>baseLogger</code> will perform logs in two ways: when it has been called directly or when we call log performing with the tag <code>"base"</code> or <code>null</code>. Besides, you can see there extension <code>filtered</code> which allow to create <code>FilterKSLog</code> logger with simple lambda.</p> <p>In the example above <code>baseLogger</code> will perform logs in two ways: when it has been called directly or when we call log performing with the tag <code>"base"</code> or <code>null</code>. Besides, you can see there extension <code>filtered</code> which allow to create <code>FilterKSLog</code> logger with simple lambda.</p>
<h3 id="typedkslog">TypedKSLog<a class="headerlink" href="#typedkslog" title="Permanent link">&para;</a></h3> <h3 id="typedkslog">TypedKSLog<a class="headerlink" href="#typedkslog" title="Permanent link">&para;</a></h3>
<p>This logger accepts map of types with the target loggers. You may build this logger with the special simple DSL:</p> <p>This logger accepts map of types with the target loggers. You may build this logger with the special simple DSL:</p>
<pre><code class="language-kotlin">val baseLogger = KSLog(&quot;base&quot;) // log everything with the tag `base` if not set other <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-9-1"><a id="__codelineno-9-1" name="__codelineno-9-1" href="#__codelineno-9-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">baseLogger</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">KSLog</span><span class="p">(</span><span class="s">&quot;base&quot;</span><span class="p">)</span><span class="w"> </span><span class="c1">// log everything with the tag `base` if not set other</span>
val typed = buildTypedLogger { </span><span id="__span-9-2"><a id="__codelineno-9-2" name="__codelineno-9-2" href="#__codelineno-9-2"></a><span class="kd">val</span><span class="w"> </span><span class="nv">typed</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">buildTypedLogger</span><span class="w"> </span><span class="p">{</span>
on&lt;Int&gt;(baseLogger) // log all ints to the baseLogger </span><span id="__span-9-3"><a id="__codelineno-9-3" name="__codelineno-9-3" href="#__codelineno-9-3"></a><span class="w"> </span><span class="n">on</span><span class="o">&lt;</span><span class="kt">Int</span><span class="o">&gt;</span><span class="p">(</span><span class="n">baseLogger</span><span class="p">)</span><span class="w"> </span><span class="c1">// log all ints to the baseLogger</span>
on&lt;Float&gt; { _, _, message, _ -&gt;// log all floats to the passed logger </span><span id="__span-9-4"><a id="__codelineno-9-4" name="__codelineno-9-4" href="#__codelineno-9-4"></a><span class="w"> </span><span class="n">on</span><span class="o">&lt;</span><span class="kt">Float</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">message</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">-&gt;</span><span class="c1">// log all floats to the passed logger</span>
println(message.toString()) // just print all floats </span><span id="__span-9-5"><a id="__codelineno-9-5" name="__codelineno-9-5" href="#__codelineno-9-5"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="n">message</span><span class="p">.</span><span class="na">toString</span><span class="p">())</span><span class="w"> </span><span class="c1">// just print all floats</span>
} </span><span id="__span-9-6"><a id="__codelineno-9-6" name="__codelineno-9-6" href="#__codelineno-9-6"></a><span class="w"> </span><span class="p">}</span>
default { level, tag, message, throwable -&gt; </span><span id="__span-9-7"><a id="__codelineno-9-7" name="__codelineno-9-7" href="#__codelineno-9-7"></a><span class="w"> </span><span class="n">default</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">level</span><span class="p">,</span><span class="w"> </span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="n">message</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="w"> </span><span class="o">-&gt;</span>
KSLog.performLog(level, tag, message, throwable) </span><span id="__span-9-8"><a id="__codelineno-9-8" name="__codelineno-9-8" href="#__codelineno-9-8"></a><span class="w"> </span><span class="n">KSLog</span><span class="p">.</span><span class="na">performLog</span><span class="p">(</span><span class="n">level</span><span class="p">,</span><span class="w"> </span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="n">message</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span>
} </span><span id="__span-9-9"><a id="__codelineno-9-9" name="__codelineno-9-9" href="#__codelineno-9-9"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-9-10"><a id="__codelineno-9-10" name="__codelineno-9-10" href="#__codelineno-9-10"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<h3 id="automatical-loggers">Automatical loggers<a class="headerlink" href="#automatical-loggers" title="Permanent link">&para;</a></h3> <h3 id="automatical-loggers">Automatical loggers<a class="headerlink" href="#automatical-loggers" title="Permanent link">&para;</a></h3>
<p>There are two things which can be useful in your code: <code>logger</code> and <code>logTag</code> extensions. <code>logTag</code> is the autocalculated by your object classname tag. <code>logger</code> extension can be used with applying to any object like in the next snippet:</p> <p>There are two things which can be useful in your code: <code>logger</code> and <code>logTag</code> extensions. <code>logTag</code> is the autocalculated by your object classname tag. <code>logger</code> extension can be used with applying to any object like in the next snippet:</p>
<pre><code class="language-kotlin">class SomeClass { <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-10-1"><a id="__codelineno-10-1" name="__codelineno-10-1" href="#__codelineno-10-1"></a><span class="kd">class</span><span class="w"> </span><span class="nc">SomeClass</span><span class="w"> </span><span class="p">{</span>
init { </span><span id="__span-10-2"><a id="__codelineno-10-2" name="__codelineno-10-2" href="#__codelineno-10-2"></a><span class="w"> </span><span class="k">init</span><span class="w"> </span><span class="p">{</span>
logger.i(&quot;inited&quot;) </span><span id="__span-10-3"><a id="__codelineno-10-3" name="__codelineno-10-3" href="#__codelineno-10-3"></a><span class="w"> </span><span class="n">logger</span><span class="p">.</span><span class="na">i</span><span class="p">(</span><span class="s">&quot;inited&quot;</span><span class="p">)</span>
} </span><span id="__span-10-4"><a id="__codelineno-10-4" name="__codelineno-10-4" href="#__codelineno-10-4"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-10-5"><a id="__codelineno-10-5" name="__codelineno-10-5" href="#__codelineno-10-5"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<p>The code above will trigger calling of logging in <code>KSLog.default</code> with level <code>LogLevel.INFO</code> using tag <code>SomeClass</code> and message <code>"inited"</code>. As you could have guessed, <code>logger</code> is using <code>TagLogger</code> with <code>logTag</code> underhood and the most expensive operation here is automatical calculation of <code>logTag</code>.</p> <p>The code above will trigger calling of logging in <code>KSLog.default</code> with level <code>LogLevel.INFO</code> using tag <code>SomeClass</code> and message <code>"inited"</code>. As you could have guessed, <code>logger</code> is using <code>TagLogger</code> with <code>logTag</code> underhood and the most expensive operation here is automatical calculation of <code>logTag</code>.</p>
<ul> <ul>
<li>Extension <code>logger</code></li> <li>Extension <code>logger</code></li>
</ul> </ul>
<h2 id="jvm-specific-setup">JVM specific setup<a class="headerlink" href="#jvm-specific-setup" title="Permanent link">&para;</a></h2> <h2 id="jvm-specific-setup">JVM specific setup<a class="headerlink" href="#jvm-specific-setup" title="Permanent link">&para;</a></h2>
<p>For JVM you may setup additionally use java loggers as the second parameter of <code>KSLog</code> factory. For example:</p> <p>For JVM you may setup additionally use java loggers as the second parameter of <code>KSLog</code> factory. For example:</p>
<pre><code class="language-kotlin">KSLog( <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-11-1"><a id="__codelineno-11-1" name="__codelineno-11-1" href="#__codelineno-11-1"></a><span class="n">KSLog</span><span class="p">(</span>
&quot;yourTag&quot; </span><span id="__span-11-2"><a id="__codelineno-11-2" name="__codelineno-11-2" href="#__codelineno-11-2"></a><span class="w"> </span><span class="s">&quot;yourTag&quot;</span>
Logger.getLogger(&quot;YourJavaLoggerName&quot;) </span><span id="__span-11-3"><a id="__codelineno-11-3" name="__codelineno-11-3" href="#__codelineno-11-3"></a><span class="w"> </span><span class="n">Logger</span><span class="p">.</span><span class="na">getLogger</span><span class="p">(</span><span class="s">&quot;YourJavaLoggerName&quot;</span><span class="p">)</span>
) </span><span id="__span-11-4"><a id="__codelineno-11-4" name="__codelineno-11-4" href="#__codelineno-11-4"></a><span class="p">)</span>
</code></pre> </span></code></pre></div>

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@ -1189,55 +1189,55 @@
</tbody> </tbody>
</table> </table>
<p>Low-level way to create keyboard looks like in the next snippet:</p> <p>Low-level way to create keyboard looks like in the next snippet:</p>
<pre><code class="language-kotlin">ReplyKeyboardMarkup( <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="n">ReplyKeyboardMarkup</span><span class="p">(</span>
matrix { </span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="n">matrix</span><span class="w"> </span><span class="p">{</span>
row { </span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="w"> </span><span class="n">row</span><span class="w"> </span><span class="p">{</span>
add(SimpleKeyboardButton(&quot;Simple text&quot;)) </span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="w"> </span><span class="n">add</span><span class="p">(</span><span class="n">SimpleKeyboardButton</span><span class="p">(</span><span class="s">&quot;Simple text&quot;</span><span class="p">))</span>
// ... </span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="w"> </span><span class="c1">// ...</span>
} </span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a><span class="w"> </span><span class="p">}</span>
// ... </span><span id="__span-0-7"><a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="w"> </span><span class="c1">// ...</span>
} </span><span id="__span-0-8"><a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="w"> </span><span class="p">}</span>
) </span><span id="__span-0-9"><a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>In case you wish to create inline keyboard, it will look like the same as for reply keyboard. But there is another way. The next snippet will create the same keyboard as on the screenshots above:</p> <p>In case you wish to create inline keyboard, it will look like the same as for reply keyboard. But there is another way. The next snippet will create the same keyboard as on the screenshots above:</p>
<pre><code class="language-kotlin">// reply keyboard <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="c1">// reply keyboard</span>
replyKeyboard { </span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a><span class="n">replyKeyboard</span><span class="w"> </span><span class="p">{</span>
row { </span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="w"> </span><span class="n">row</span><span class="w"> </span><span class="p">{</span>
simpleButton(&quot;7&quot;) </span><span id="__span-1-4"><a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">&quot;7&quot;</span><span class="p">)</span>
simpleButton(&quot;8&quot;) </span><span id="__span-1-5"><a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">&quot;8&quot;</span><span class="p">)</span>
simpleButton(&quot;9&quot;) </span><span id="__span-1-6"><a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a><span class="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">&quot;9&quot;</span><span class="p">)</span>
simpleButton(&quot;*&quot;) </span><span id="__span-1-7"><a id="__codelineno-1-7" name="__codelineno-1-7" href="#__codelineno-1-7"></a><span class="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">&quot;*&quot;</span><span class="p">)</span>
} </span><span id="__span-1-8"><a id="__codelineno-1-8" name="__codelineno-1-8" href="#__codelineno-1-8"></a><span class="w"> </span><span class="p">}</span>
row { </span><span id="__span-1-9"><a id="__codelineno-1-9" name="__codelineno-1-9" href="#__codelineno-1-9"></a><span class="w"> </span><span class="n">row</span><span class="w"> </span><span class="p">{</span>
simpleButton(&quot;4&quot;) </span><span id="__span-1-10"><a id="__codelineno-1-10" name="__codelineno-1-10" href="#__codelineno-1-10"></a><span class="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">&quot;4&quot;</span><span class="p">)</span>
simpleButton(&quot;5&quot;) </span><span id="__span-1-11"><a id="__codelineno-1-11" name="__codelineno-1-11" href="#__codelineno-1-11"></a><span class="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">&quot;5&quot;</span><span class="p">)</span>
simpleButton(&quot;6&quot;) </span><span id="__span-1-12"><a id="__codelineno-1-12" name="__codelineno-1-12" href="#__codelineno-1-12"></a><span class="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">&quot;6&quot;</span><span class="p">)</span>
simpleButton(&quot;/&quot;) </span><span id="__span-1-13"><a id="__codelineno-1-13" name="__codelineno-1-13" href="#__codelineno-1-13"></a><span class="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">&quot;/&quot;</span><span class="p">)</span>
} </span><span id="__span-1-14"><a id="__codelineno-1-14" name="__codelineno-1-14" href="#__codelineno-1-14"></a><span class="w"> </span><span class="p">}</span>
row { </span><span id="__span-1-15"><a id="__codelineno-1-15" name="__codelineno-1-15" href="#__codelineno-1-15"></a><span class="w"> </span><span class="n">row</span><span class="w"> </span><span class="p">{</span>
simpleButton(&quot;1&quot;) </span><span id="__span-1-16"><a id="__codelineno-1-16" name="__codelineno-1-16" href="#__codelineno-1-16"></a><span class="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">&quot;1&quot;</span><span class="p">)</span>
simpleButton(&quot;2&quot;) </span><span id="__span-1-17"><a id="__codelineno-1-17" name="__codelineno-1-17" href="#__codelineno-1-17"></a><span class="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">&quot;2&quot;</span><span class="p">)</span>
simpleButton(&quot;3&quot;) </span><span id="__span-1-18"><a id="__codelineno-1-18" name="__codelineno-1-18" href="#__codelineno-1-18"></a><span class="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">&quot;3&quot;</span><span class="p">)</span>
simpleButton(&quot;-&quot;) </span><span id="__span-1-19"><a id="__codelineno-1-19" name="__codelineno-1-19" href="#__codelineno-1-19"></a><span class="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">&quot;-&quot;</span><span class="p">)</span>
} </span><span id="__span-1-20"><a id="__codelineno-1-20" name="__codelineno-1-20" href="#__codelineno-1-20"></a><span class="w"> </span><span class="p">}</span>
row { </span><span id="__span-1-21"><a id="__codelineno-1-21" name="__codelineno-1-21" href="#__codelineno-1-21"></a><span class="w"> </span><span class="n">row</span><span class="w"> </span><span class="p">{</span>
simpleButton(&quot;0&quot;) </span><span id="__span-1-22"><a id="__codelineno-1-22" name="__codelineno-1-22" href="#__codelineno-1-22"></a><span class="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">&quot;0&quot;</span><span class="p">)</span>
simpleButton(&quot;.&quot;) </span><span id="__span-1-23"><a id="__codelineno-1-23" name="__codelineno-1-23" href="#__codelineno-1-23"></a><span class="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">&quot;.&quot;</span><span class="p">)</span>
simpleButton(&quot;=&quot;) </span><span id="__span-1-24"><a id="__codelineno-1-24" name="__codelineno-1-24" href="#__codelineno-1-24"></a><span class="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">&quot;=&quot;</span><span class="p">)</span>
simpleButton(&quot;+&quot;) </span><span id="__span-1-25"><a id="__codelineno-1-25" name="__codelineno-1-25" href="#__codelineno-1-25"></a><span class="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">&quot;+&quot;</span><span class="p">)</span>
} </span><span id="__span-1-26"><a id="__codelineno-1-26" name="__codelineno-1-26" href="#__codelineno-1-26"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-1-27"><a id="__codelineno-1-27" name="__codelineno-1-27" href="#__codelineno-1-27"></a><span class="p">}</span>
</span><span id="__span-1-28"><a id="__codelineno-1-28" name="__codelineno-1-28" href="#__codelineno-1-28"></a>
// inline keyboard </span><span id="__span-1-29"><a id="__codelineno-1-29" name="__codelineno-1-29" href="#__codelineno-1-29"></a><span class="c1">// inline keyboard</span>
inlineKeyboard { </span><span id="__span-1-30"><a id="__codelineno-1-30" name="__codelineno-1-30" href="#__codelineno-1-30"></a><span class="n">inlineKeyboard</span><span class="w"> </span><span class="p">{</span>
row { </span><span id="__span-1-31"><a id="__codelineno-1-31" name="__codelineno-1-31" href="#__codelineno-1-31"></a><span class="w"> </span><span class="n">row</span><span class="w"> </span><span class="p">{</span>
dataButton(&quot;Get random music&quot;, &quot;random&quot;) </span><span id="__span-1-32"><a id="__codelineno-1-32" name="__codelineno-1-32" href="#__codelineno-1-32"></a><span class="w"> </span><span class="n">dataButton</span><span class="p">(</span><span class="s">&quot;Get random music&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;random&quot;</span><span class="p">)</span>
} </span><span id="__span-1-33"><a id="__codelineno-1-33" name="__codelineno-1-33" href="#__codelineno-1-33"></a><span class="w"> </span><span class="p">}</span>
row { </span><span id="__span-1-34"><a id="__codelineno-1-34" name="__codelineno-1-34" href="#__codelineno-1-34"></a><span class="w"> </span><span class="n">row</span><span class="w"> </span><span class="p">{</span>
urlButton(&quot;Send music to friends&quot;, &quot;https://some.link&quot;) </span><span id="__span-1-35"><a id="__codelineno-1-35" name="__codelineno-1-35" href="#__codelineno-1-35"></a><span class="w"> </span><span class="n">urlButton</span><span class="p">(</span><span class="s">&quot;Send music to friends&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;https://some.link&quot;</span><span class="p">)</span>
} </span><span id="__span-1-36"><a id="__codelineno-1-36" name="__codelineno-1-36" href="#__codelineno-1-36"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-1-37"><a id="__codelineno-1-37" name="__codelineno-1-37" href="#__codelineno-1-37"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>

View File

@ -1268,49 +1268,45 @@
<h2 id="handlelivelocation">handleLiveLocation<a class="headerlink" href="#handlelivelocation" title="Permanent link">&para;</a></h2> <h2 id="handlelivelocation">handleLiveLocation<a class="headerlink" href="#handlelivelocation" title="Permanent link">&para;</a></h2>
<p>This way of live locations handling is based on coroutines <a href="https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/">Flow</a> and allow you to pass some external <code>Flow</code> with <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.api/-edit-live-location-info/index.html">EditLiveLocationInfo</a>. So, workflow:</p> <p>This way of live locations handling is based on coroutines <a href="https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/">Flow</a> and allow you to pass some external <code>Flow</code> with <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.api/-edit-live-location-info/index.html">EditLiveLocationInfo</a>. So, workflow:</p>
<ul> <ul>
<li>Create your own flow of locations. For example:</li> <li>Create your own flow of locations. For example:
<div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="n">flow</span><span class="w"> </span><span class="p">{</span>
</span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span>
</span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">isActive</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
</span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">newInfo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">EditLiveLocationInfo</span><span class="p">(</span>
</span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="w"> </span><span class="n">latitude</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">.</span><span class="na">toDouble</span><span class="p">(),</span>
</span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a><span class="w"> </span><span class="n">longitude</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">.</span><span class="na">toDouble</span><span class="p">(),</span>
</span><span id="__span-0-7"><a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="w"> </span><span class="n">replyMarkup</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">flatInlineKeyboard</span><span class="w"> </span><span class="p">{</span>
</span><span id="__span-0-8"><a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="w"> </span><span class="n">dataButton</span><span class="p">(</span><span class="s">&quot;Cancel&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;cancel&quot;</span><span class="p">)</span>
</span><span id="__span-0-9"><a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a><span class="w"> </span><span class="p">}</span>
</span><span id="__span-0-10"><a id="__codelineno-0-10" name="__codelineno-0-10" href="#__codelineno-0-10"></a><span class="w"> </span><span class="p">)</span>
</span><span id="__span-0-11"><a id="__codelineno-0-11" name="__codelineno-0-11" href="#__codelineno-0-11"></a><span class="w"> </span><span class="n">emit</span><span class="p">(</span><span class="n">newInfo</span><span class="p">)</span>
</span><span id="__span-0-12"><a id="__codelineno-0-12" name="__codelineno-0-12" href="#__codelineno-0-12"></a><span class="w"> </span><span class="n">i</span><span class="o">++</span>
</span><span id="__span-0-13"><a id="__codelineno-0-13" name="__codelineno-0-13" href="#__codelineno-0-13"></a><span class="w"> </span><span class="n">delay</span><span class="p">(</span><span class="m">10000L</span><span class="p">)</span><span class="w"> </span><span class="c1">// 10 seconds</span>
</span><span id="__span-0-14"><a id="__codelineno-0-14" name="__codelineno-0-14" href="#__codelineno-0-14"></a><span class="w"> </span><span class="p">}</span>
</span><span id="__span-0-15"><a id="__codelineno-0-15" name="__codelineno-0-15" href="#__codelineno-0-15"></a><span class="p">}</span>
</span></code></pre></div></li>
<li>In case you needed, create your collector to store the message with live location:
<div class="language-kotlin highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">currentMessageState</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">MutableStateFlow</span><span class="o">&lt;</span><span class="n">ContentMessage</span><span class="o">&lt;</span><span class="n">LocationContent</span><span class="o">&gt;?&gt;</span><span class="p">(</span><span class="kc">null</span><span class="p">)</span>
</span></code></pre></div></li>
<li>Start handle live location. <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.api/handle-live-location.html">handleLiveLocation</a> works synchronosly (in current coroutine) and will ends only when your flow will ends. Thats why there are two ways to call it:
<div class="language-kotlin highlight"><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="n">handleLiveLocation</span><span class="p">(</span>
</span><span id="__span-2-2"><a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">chat</span><span class="p">.</span><span class="na">id</span><span class="p">,</span>
</span><span id="__span-2-3"><a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="w"> </span><span class="n">locationsFlow</span><span class="p">,</span>
</span><span id="__span-2-4"><a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="w"> </span><span class="n">sentMessageFlow</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">FlowCollector</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">currentMessageState</span><span class="p">.</span><span class="na">emit</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span><span class="w"> </span><span class="p">}</span>
</span><span id="__span-2-5"><a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a><span class="p">)</span>
</span><span id="__span-2-6"><a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a><span class="c1">// this code will be called after `locationsFlow` will ends</span>
</span></code></pre></div>
OR
<div class="language-kotlin highlight"><pre><span></span><code><span id="__span-3-1"><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a><span class="n">scope</span><span class="p">.</span><span class="na">launch</span><span class="w"> </span><span class="p">{</span>
</span><span id="__span-3-2"><a id="__codelineno-3-2" name="__codelineno-3-2" href="#__codelineno-3-2"></a><span class="w"> </span><span class="n">handleLiveLocation</span><span class="p">(</span>
</span><span id="__span-3-3"><a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">chat</span><span class="p">.</span><span class="na">id</span><span class="p">,</span>
</span><span id="__span-3-4"><a id="__codelineno-3-4" name="__codelineno-3-4" href="#__codelineno-3-4"></a><span class="w"> </span><span class="n">locationsFlow</span><span class="p">,</span>
</span><span id="__span-3-5"><a id="__codelineno-3-5" name="__codelineno-3-5" href="#__codelineno-3-5"></a><span class="w"> </span><span class="n">sentMessageFlow</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">FlowCollector</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">currentMessageState</span><span class="p">.</span><span class="na">emit</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span><span class="w"> </span><span class="p">}</span>
</span><span id="__span-3-6"><a id="__codelineno-3-6" name="__codelineno-3-6" href="#__codelineno-3-6"></a><span class="w"> </span><span class="p">)</span>
</span><span id="__span-3-7"><a id="__codelineno-3-7" name="__codelineno-3-7" href="#__codelineno-3-7"></a><span class="p">}</span>
</span><span id="__span-3-8"><a id="__codelineno-3-8" name="__codelineno-3-8" href="#__codelineno-3-8"></a><span class="c1">// this code will be called right after launch will be completed</span>
</span></code></pre></div></li>
</ul> </ul>
<pre><code class="language-kotlin">flow {
var i = 0
while (isActive) {
val newInfo = EditLiveLocationInfo(
latitude = i.toDouble(),
longitude = i.toDouble(),
replyMarkup = flatInlineKeyboard {
dataButton(&quot;Cancel&quot;, &quot;cancel&quot;)
}
)
emit(newInfo)
i++
delay(10000L) // 10 seconds
}
}
</code></pre>
<ul>
<li>In case you needed, create your collector to store the message with live location:</li>
</ul>
<pre><code class="language-kotlin">val currentMessageState = MutableStateFlow&lt;ContentMessage&lt;LocationContent&gt;?&gt;(null)
</code></pre>
<ul>
<li>Start handle live location. <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.api/handle-live-location.html">handleLiveLocation</a> works synchronosly (in current coroutine) and will ends only when your flow will ends. Thats why there are two ways to call it:</li>
</ul>
<pre><code class="language-kotlin">handleLiveLocation(
it.chat.id,
locationsFlow,
sentMessageFlow = FlowCollector { currentMessageState.emit(it) }
)
// this code will be called after `locationsFlow` will ends
</code></pre>
<p>OR</p>
<pre><code class="language-kotlin">scope.launch {
handleLiveLocation(
it.chat.id,
locationsFlow,
sentMessageFlow = FlowCollector { currentMessageState.emit(it) }
)
}
// this code will be called right after launch will be completed
</code></pre>
<p>See our <a href="https://github.com/InsanusMokrassar/TelegramBotAPI-examples/blob/master/LiveLocationsBot/src/main/kotlin/LiveLocationsBot.kt">example</a> to get more detailed sample</p> <p>See our <a href="https://github.com/InsanusMokrassar/TelegramBotAPI-examples/blob/master/LiveLocationsBot/src/main/kotlin/LiveLocationsBot.kt">example</a> to get more detailed sample</p>

View File

@ -1170,21 +1170,21 @@
<h1 id="text">Text<a class="headerlink" href="#text" title="Permanent link">&para;</a></h1> <h1 id="text">Text<a class="headerlink" href="#text" title="Permanent link">&para;</a></h1>
<p>For the text creating there are several tools. The most simple one is to concatenate several text sources to make list of text sources as a result:</p> <p>For the text creating there are several tools. The most simple one is to concatenate several text sources to make list of text sources as a result:</p>
<pre><code class="language-kotlin">val sources = &quot;Regular start of text &quot; + bold(&quot;with bold part&quot;) + italic(&quot;and italic ending&quot;) <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">sources</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;Regular start of text &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">bold</span><span class="p">(</span><span class="s">&quot;with bold part&quot;</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">italic</span><span class="p">(</span><span class="s">&quot;and italic ending&quot;</span><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>But there is a little bit more useful way: entities builder:</p> <p>But there is a little bit more useful way: entities builder:</p>
<pre><code class="language-kotlin">val items = (0 until 10).map { it.toString() } <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">items</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="m">0</span><span class="w"> </span><span class="n">until</span><span class="w"> </span><span class="m">10</span><span class="p">).</span><span class="na">map</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">toString</span><span class="p">()</span><span class="w"> </span><span class="p">}</span>
buildEntities(&quot; &quot;) {// optional &quot; &quot; auto separator which will be pasted between text sources </span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a><span class="n">buildEntities</span><span class="p">(</span><span class="s">&quot; &quot;</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="c1">// optional &quot; &quot; auto separator which will be pasted between text sources</span>
+&quot;It is regular start too&quot; + bold(&quot;it is bold as well&quot;) </span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="w"> </span><span class="o">+</span><span class="s">&quot;It is regular start too&quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">bold</span><span class="p">(</span><span class="s">&quot;it is bold as well&quot;</span><span class="p">)</span>
items.forEachIndexed { i, item -&gt; </span><span id="__span-1-4"><a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="w"> </span><span class="n">items</span><span class="p">.</span><span class="na">forEachIndexed</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">item</span><span class="w"> </span><span class="o">-&gt;</span>
if (i % 2) { </span><span id="__span-1-5"><a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="m">2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
italic(item) </span><span id="__span-1-6"><a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a><span class="w"> </span><span class="n">italic</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
} else { </span><span id="__span-1-7"><a id="__codelineno-1-7" name="__codelineno-1-7" href="#__codelineno-1-7"></a><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
strikethrough(item) </span><span id="__span-1-8"><a id="__codelineno-1-8" name="__codelineno-1-8" href="#__codelineno-1-8"></a><span class="w"> </span><span class="n">strikethrough</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
} </span><span id="__span-1-9"><a id="__codelineno-1-9" name="__codelineno-1-9" href="#__codelineno-1-9"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-1-10"><a id="__codelineno-1-10" name="__codelineno-1-10" href="#__codelineno-1-10"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-1-11"><a id="__codelineno-1-11" name="__codelineno-1-11" href="#__codelineno-1-11"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<p>In the code above we are creating an items list just for demonstrating that inside of buildEntities body we may use any operations for cunstructing our result list of <code>TextSource</code>s. As a result, will be created the list which will looks like in telegram as &ldquo;It is regular start too <strong>it is bold as well</strong> <em>0</em> ~~1~~ <em>2</em> ~~3~~ <em>4</em> ~~5~~ <em>6</em> ~~7~~ <em>8</em> ~~9~~&rdquo;.</p> <p>In the code above we are creating an items list just for demonstrating that inside of buildEntities body we may use any operations for cunstructing our result list of <code>TextSource</code>s. As a result, will be created the list which will looks like in telegram as &ldquo;It is regular start too <strong>it is bold as well</strong> <em>0</em> ~~1~~ <em>2</em> ~~3~~ <em>4</em> ~~5~~ <em>6</em> ~~7~~ <em>8</em> ~~9~~&rdquo;.</p>

View File

@ -1222,12 +1222,12 @@ A lot of examples with using of Telegram Bot API you can find in <a href="https:
</div> </div>
<h3 id="the-most-simple-bot">The most simple bot<a class="headerlink" href="#the-most-simple-bot" title="Permanent link">&para;</a></h3> <h3 id="the-most-simple-bot">The most simple bot<a class="headerlink" href="#the-most-simple-bot" title="Permanent link">&para;</a></h3>
<p>The most simple bot will just print information about itself. All source code you can find <a href="https://github.com/InsanusMokrassar/TelegramBotAPI-examples/tree/master/GetMeBot">in this repository</a>. Our interest here will be concentrated on the next example part:</p> <p>The most simple bot will just print information about itself. All source code you can find <a href="https://github.com/InsanusMokrassar/TelegramBotAPI-examples/tree/master/GetMeBot">in this repository</a>. Our interest here will be concentrated on the next example part:</p>
<pre><code class="language-kotlin">suspend fun main(vararg args: String) { <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="kd">suspend</span><span class="w"> </span><span class="kd">fun</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="k">vararg</span><span class="w"> </span><span class="n">args</span><span class="p">:</span><span class="w"> </span><span class="kt">String</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
val botToken = args.first() </span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">botToken</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">args</span><span class="p">.</span><span class="na">first</span><span class="p">()</span>
val bot = telegramBot(botToken) </span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">telegramBot</span><span class="p">(</span><span class="n">botToken</span><span class="p">)</span>
println(bot.getMe()) </span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="n">bot</span><span class="p">.</span><span class="na">getMe</span><span class="p">())</span>
} </span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<p>So, let&rsquo;s get understanding, about what is going on:</p> <p>So, let&rsquo;s get understanding, about what is going on:</p>
<ol> <ol>
<li><code>suspend fun main(vararg args: String)</code>:<ul> <li><code>suspend fun main(vararg args: String)</code>:<ul>
@ -1239,8 +1239,8 @@ A lot of examples with using of Telegram Bot API you can find in <a href="https:
<li><code>println(bot.getMe())</code>: here happens calling of <a href="https://tgbotapi.inmo.dev/docs/com.github.insanusmokrassar.-telegram-bot-a-p-i.extensions.api.bot/get-me.html">getMe</a> extension</li> <li><code>println(bot.getMe())</code>: here happens calling of <a href="https://tgbotapi.inmo.dev/docs/com.github.insanusmokrassar.-telegram-bot-a-p-i.extensions.api.bot/get-me.html">getMe</a> extension</li>
</ol> </ol>
<p>As a result, we will see in the command line something like</p> <p>As a result, we will see in the command line something like</p>
<pre><code class="language-shell">ExtendedBot(id=ChatId(chatId=123456789), username=Username(username=@first_test_ee17e8_bot), firstName=Your bot name, lastName=, canJoinGroups=false, canReadAllGroupMessages=false, supportsInlineQueries=false) <div class="language-shell highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a>ExtendedBot<span class="o">(</span><span class="nv">id</span><span class="o">=</span>ChatId<span class="o">(</span><span class="nv">chatId</span><span class="o">=</span><span class="m">123456789</span><span class="o">)</span>,<span class="w"> </span><span class="nv">username</span><span class="o">=</span>Username<span class="o">(</span><span class="nv">username</span><span class="o">=</span>@first_test_ee17e8_bot<span class="o">)</span>,<span class="w"> </span><span class="nv">firstName</span><span class="o">=</span>Your<span class="w"> </span>bot<span class="w"> </span>name,<span class="w"> </span><span class="nv">lastName</span><span class="o">=</span>,<span class="w"> </span><span class="nv">canJoinGroups</span><span class="o">=</span>false,<span class="w"> </span><span class="nv">canReadAllGroupMessages</span><span class="o">=</span>false,<span class="w"> </span><span class="nv">supportsInlineQueries</span><span class="o">=</span><span class="nb">false</span><span class="o">)</span>
</code></pre> </span></code></pre></div>

View File

@ -1516,38 +1516,34 @@
<h3 id="notice-about-repository">Notice about repository<a class="headerlink" href="#notice-about-repository" title="Permanent link">&para;</a></h3> <h3 id="notice-about-repository">Notice about repository<a class="headerlink" href="#notice-about-repository" title="Permanent link">&para;</a></h3>
<p>To use this library, you will need to include <code>Maven Central</code> repository in your project</p> <p>To use this library, you will need to include <code>Maven Central</code> repository in your project</p>
<h6 id="buildgradle">build.gradle<a class="headerlink" href="#buildgradle" title="Permanent link">&para;</a></h6> <h6 id="buildgradle">build.gradle<a class="headerlink" href="#buildgradle" title="Permanent link">&para;</a></h6>
<pre><code class="language-groovy">mavenCentral() <div class="language-groovy highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="n">mavenCentral</span><span class="o">()</span>
</code></pre> </span></code></pre></div>
<h6 id="pomxml">pom.xml<a class="headerlink" href="#pomxml" title="Permanent link">&para;</a></h6> <h6 id="pomxml">pom.xml<a class="headerlink" href="#pomxml" title="Permanent link">&para;</a></h6>
<pre><code class="language-xml">&lt;repository&gt; <div class="language-xml highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="nt">&lt;repository&gt;</span>
&lt;id&gt;central&lt;/id&gt; </span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a><span class="w"> </span><span class="nt">&lt;id&gt;</span>central<span class="nt">&lt;/id&gt;</span>
&lt;name&gt;mavenCentral&lt;/name&gt; </span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="w"> </span><span class="nt">&lt;name&gt;</span>mavenCentral<span class="nt">&lt;/name&gt;</span>
&lt;url&gt;https://repo1.maven.org/maven2&lt;/url&gt; </span><span id="__span-1-4"><a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="w"> </span><span class="nt">&lt;url&gt;</span>https://repo1.maven.org/maven2<span class="nt">&lt;/url&gt;</span>
&lt;/repository&gt; </span><span id="__span-1-5"><a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="nt">&lt;/repository&gt;</span>
</code></pre> </span></code></pre></div>
<h6 id="dev-channel">Dev channel<a class="headerlink" href="#dev-channel" title="Permanent link">&para;</a></h6> <h6 id="dev-channel">Dev channel<a class="headerlink" href="#dev-channel" title="Permanent link">&para;</a></h6>
<p>Besides, there is <a href="https://git.inmo.dev/InsanusMokrassar/-/packages/maven/dev.inmo-tgbotapi">developer versions repo</a>. To use it in your project, add the repo in <code>repositories</code> section:</p> <p>Besides, there is <a href="https://git.inmo.dev/InsanusMokrassar/-/packages/maven/dev.inmo-tgbotapi">developer versions repo</a>. To use it in your project, add the repo in <code>repositories</code> section:</p>
<details><summary>Gradle</summary> <details><summary>Gradle</summary>
<div class="language-groovy highlight"><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="n">maven</span><span class="w"> </span><span class="o">{</span>
<pre><code class="language-groovy">maven { </span><span id="__span-2-2"><a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a><span class="w"> </span><span class="n">url</span><span class="w"> </span><span class="s2">&quot;https://git.inmo.dev/api/packages/InsanusMokrassar/maven&quot;</span>
url &quot;https://git.inmo.dev/api/packages/InsanusMokrassar/maven&quot; </span><span id="__span-2-3"><a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="o">}</span>
} </span></code></pre></div>
</code></pre>
</details> </details>
<details><summary>Maven</summary> <details><summary>Maven</summary>
<div class="language-xml highlight"><pre><span></span><code><span id="__span-3-1"><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a><span class="nt">&lt;repository&gt;</span>
<pre><code class="language-xml">&lt;repository&gt; </span><span id="__span-3-2"><a id="__codelineno-3-2" name="__codelineno-3-2" href="#__codelineno-3-2"></a><span class="w"> </span><span class="nt">&lt;id&gt;</span>dev.inmo<span class="nt">&lt;/id&gt;</span>
&lt;id&gt;dev.inmo&lt;/id&gt; </span><span id="__span-3-3"><a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a><span class="w"> </span><span class="nt">&lt;name&gt;</span>InmoDev<span class="nt">&lt;/name&gt;</span>
&lt;name&gt;InmoDev&lt;/name&gt; </span><span id="__span-3-4"><a id="__codelineno-3-4" name="__codelineno-3-4" href="#__codelineno-3-4"></a><span class="w"> </span><span class="nt">&lt;url&gt;</span>https://git.inmo.dev/api/packages/InsanusMokrassar/maven<span class="nt">&lt;/url&gt;</span>
&lt;url&gt;https://git.inmo.dev/api/packages/InsanusMokrassar/maven&lt;/url&gt; </span><span id="__span-3-5"><a id="__codelineno-3-5" name="__codelineno-3-5" href="#__codelineno-3-5"></a><span class="nt">&lt;/repository&gt;</span>
&lt;/repository&gt; </span></code></pre></div>
</code></pre>
</details> </details>
@ -1555,54 +1551,54 @@
<p>As <code>tgbotapi_version</code> variable in next snippets will be used variable with next last published version:</p> <p>As <code>tgbotapi_version</code> variable in next snippets will be used variable with next last published version:</p>
<p><a href="https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi"><img alt="Maven Central" src="https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi/badge.svg" /></a></p> <p><a href="https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi"><img alt="Maven Central" src="https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi/badge.svg" /></a></p>
<h6 id="buildgradle_1">build.gradle<a class="headerlink" href="#buildgradle_1" title="Permanent link">&para;</a></h6> <h6 id="buildgradle_1">build.gradle<a class="headerlink" href="#buildgradle_1" title="Permanent link">&para;</a></h6>
<pre><code class="language-groovy">implementation &quot;dev.inmo:tgbotapi:$tgbotapi_version&quot; <div class="language-groovy highlight"><pre><span></span><code><span id="__span-4-1"><a id="__codelineno-4-1" name="__codelineno-4-1" href="#__codelineno-4-1"></a><span class="n">implementation</span><span class="w"> </span><span class="s2">&quot;dev.inmo:tgbotapi:$tgbotapi_version&quot;</span>
</code></pre> </span></code></pre></div>
<h6 id="pomxml_1">pom.xml<a class="headerlink" href="#pomxml_1" title="Permanent link">&para;</a></h6> <h6 id="pomxml_1">pom.xml<a class="headerlink" href="#pomxml_1" title="Permanent link">&para;</a></h6>
<pre><code class="language-xml">&lt;dependency&gt; <div class="language-xml highlight"><pre><span></span><code><span id="__span-5-1"><a id="__codelineno-5-1" name="__codelineno-5-1" href="#__codelineno-5-1"></a><span class="nt">&lt;dependency&gt;</span>
&lt;groupId&gt;dev.inmo&lt;/groupId&gt; </span><span id="__span-5-2"><a id="__codelineno-5-2" name="__codelineno-5-2" href="#__codelineno-5-2"></a><span class="w"> </span><span class="nt">&lt;groupId&gt;</span>dev.inmo<span class="nt">&lt;/groupId&gt;</span>
&lt;artifactId&gt;tgbotapi&lt;/artifactId&gt; </span><span id="__span-5-3"><a id="__codelineno-5-3" name="__codelineno-5-3" href="#__codelineno-5-3"></a><span class="w"> </span><span class="nt">&lt;artifactId&gt;</span>tgbotapi<span class="nt">&lt;/artifactId&gt;</span>
&lt;version&gt;${tgbotapi_version}&lt;/version&gt; </span><span id="__span-5-4"><a id="__codelineno-5-4" name="__codelineno-5-4" href="#__codelineno-5-4"></a><span class="w"> </span><span class="nt">&lt;version&gt;</span>${tgbotapi_version}<span class="nt">&lt;/version&gt;</span>
&lt;/dependency&gt; </span><span id="__span-5-5"><a id="__codelineno-5-5" name="__codelineno-5-5" href="#__codelineno-5-5"></a><span class="nt">&lt;/dependency&gt;</span>
</code></pre> </span></code></pre></div>
<h3 id="telegrambotapi-core">TelegramBotAPI Core<a class="headerlink" href="#telegrambotapi-core" title="Permanent link">&para;</a></h3> <h3 id="telegrambotapi-core">TelegramBotAPI Core<a class="headerlink" href="#telegrambotapi-core" title="Permanent link">&para;</a></h3>
<p>As <code>tgbotapi_version</code> variable in next snippets will be used variable with next last published version:</p> <p>As <code>tgbotapi_version</code> variable in next snippets will be used variable with next last published version:</p>
<p><a href="https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi.core"><img alt="Maven Central" src="https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi.core/badge.svg" /></a></p> <p><a href="https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi.core"><img alt="Maven Central" src="https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi.core/badge.svg" /></a></p>
<h6 id="buildgradle_2">build.gradle<a class="headerlink" href="#buildgradle_2" title="Permanent link">&para;</a></h6> <h6 id="buildgradle_2">build.gradle<a class="headerlink" href="#buildgradle_2" title="Permanent link">&para;</a></h6>
<pre><code class="language-groovy">implementation &quot;dev.inmo:tgbotapi.core:$tgbotapi_version&quot; <div class="language-groovy highlight"><pre><span></span><code><span id="__span-6-1"><a id="__codelineno-6-1" name="__codelineno-6-1" href="#__codelineno-6-1"></a><span class="n">implementation</span><span class="w"> </span><span class="s2">&quot;dev.inmo:tgbotapi.core:$tgbotapi_version&quot;</span>
</code></pre> </span></code></pre></div>
<h6 id="pomxml_2">pom.xml<a class="headerlink" href="#pomxml_2" title="Permanent link">&para;</a></h6> <h6 id="pomxml_2">pom.xml<a class="headerlink" href="#pomxml_2" title="Permanent link">&para;</a></h6>
<pre><code class="language-xml">&lt;dependency&gt; <div class="language-xml highlight"><pre><span></span><code><span id="__span-7-1"><a id="__codelineno-7-1" name="__codelineno-7-1" href="#__codelineno-7-1"></a><span class="nt">&lt;dependency&gt;</span>
&lt;groupId&gt;dev.inmo&lt;/groupId&gt; </span><span id="__span-7-2"><a id="__codelineno-7-2" name="__codelineno-7-2" href="#__codelineno-7-2"></a><span class="w"> </span><span class="nt">&lt;groupId&gt;</span>dev.inmo<span class="nt">&lt;/groupId&gt;</span>
&lt;artifactId&gt;tgbotapi.core&lt;/artifactId&gt; </span><span id="__span-7-3"><a id="__codelineno-7-3" name="__codelineno-7-3" href="#__codelineno-7-3"></a><span class="w"> </span><span class="nt">&lt;artifactId&gt;</span>tgbotapi.core<span class="nt">&lt;/artifactId&gt;</span>
&lt;version&gt;${tgbotapi_version}&lt;/version&gt; </span><span id="__span-7-4"><a id="__codelineno-7-4" name="__codelineno-7-4" href="#__codelineno-7-4"></a><span class="w"> </span><span class="nt">&lt;version&gt;</span>${tgbotapi_version}<span class="nt">&lt;/version&gt;</span>
&lt;/dependency&gt; </span><span id="__span-7-5"><a id="__codelineno-7-5" name="__codelineno-7-5" href="#__codelineno-7-5"></a><span class="nt">&lt;/dependency&gt;</span>
</code></pre> </span></code></pre></div>
<h3 id="telegrambotapi-api-extensions">TelegramBotAPI API Extensions<a class="headerlink" href="#telegrambotapi-api-extensions" title="Permanent link">&para;</a></h3> <h3 id="telegrambotapi-api-extensions">TelegramBotAPI API Extensions<a class="headerlink" href="#telegrambotapi-api-extensions" title="Permanent link">&para;</a></h3>
<p>As <code>tgbotapi_version</code> variable in next snippets will be used variable with next last published version:</p> <p>As <code>tgbotapi_version</code> variable in next snippets will be used variable with next last published version:</p>
<p><a href="https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi.api"><img alt="Maven Central" src="https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi.api/badge.svg" /></a></p> <p><a href="https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi.api"><img alt="Maven Central" src="https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi.api/badge.svg" /></a></p>
<h6 id="buildgradle_3">build.gradle<a class="headerlink" href="#buildgradle_3" title="Permanent link">&para;</a></h6> <h6 id="buildgradle_3">build.gradle<a class="headerlink" href="#buildgradle_3" title="Permanent link">&para;</a></h6>
<pre><code class="language-groovy">implementation &quot;dev.inmo:tgbotapi.api:$tgbotapi_version&quot; <div class="language-groovy highlight"><pre><span></span><code><span id="__span-8-1"><a id="__codelineno-8-1" name="__codelineno-8-1" href="#__codelineno-8-1"></a><span class="n">implementation</span><span class="w"> </span><span class="s2">&quot;dev.inmo:tgbotapi.api:$tgbotapi_version&quot;</span>
</code></pre> </span></code></pre></div>
<h6 id="pomxml_3">pom.xml<a class="headerlink" href="#pomxml_3" title="Permanent link">&para;</a></h6> <h6 id="pomxml_3">pom.xml<a class="headerlink" href="#pomxml_3" title="Permanent link">&para;</a></h6>
<pre><code class="language-xml">&lt;dependency&gt; <div class="language-xml highlight"><pre><span></span><code><span id="__span-9-1"><a id="__codelineno-9-1" name="__codelineno-9-1" href="#__codelineno-9-1"></a><span class="nt">&lt;dependency&gt;</span>
&lt;groupId&gt;dev.inmo&lt;/groupId&gt; </span><span id="__span-9-2"><a id="__codelineno-9-2" name="__codelineno-9-2" href="#__codelineno-9-2"></a><span class="w"> </span><span class="nt">&lt;groupId&gt;</span>dev.inmo<span class="nt">&lt;/groupId&gt;</span>
&lt;artifactId&gt;tgbotapi.api&lt;/artifactId&gt; </span><span id="__span-9-3"><a id="__codelineno-9-3" name="__codelineno-9-3" href="#__codelineno-9-3"></a><span class="w"> </span><span class="nt">&lt;artifactId&gt;</span>tgbotapi.api<span class="nt">&lt;/artifactId&gt;</span>
&lt;version&gt;${tgbotapi_version}&lt;/version&gt; </span><span id="__span-9-4"><a id="__codelineno-9-4" name="__codelineno-9-4" href="#__codelineno-9-4"></a><span class="w"> </span><span class="nt">&lt;version&gt;</span>${tgbotapi_version}<span class="nt">&lt;/version&gt;</span>
&lt;/dependency&gt; </span><span id="__span-9-5"><a id="__codelineno-9-5" name="__codelineno-9-5" href="#__codelineno-9-5"></a><span class="nt">&lt;/dependency&gt;</span>
</code></pre> </span></code></pre></div>
<h3 id="telegrambotapi-utils-extensions">TelegramBotAPI Utils Extensions<a class="headerlink" href="#telegrambotapi-utils-extensions" title="Permanent link">&para;</a></h3> <h3 id="telegrambotapi-utils-extensions">TelegramBotAPI Utils Extensions<a class="headerlink" href="#telegrambotapi-utils-extensions" title="Permanent link">&para;</a></h3>
<p>As <code>tgbotapi_version</code> variable in next snippets will be used variable with next last published version:</p> <p>As <code>tgbotapi_version</code> variable in next snippets will be used variable with next last published version:</p>
<p><a href="https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi.utils"><img alt="Maven Central" src="https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi.utils/badge.svg" /></a></p> <p><a href="https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi.utils"><img alt="Maven Central" src="https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi.utils/badge.svg" /></a></p>
<h6 id="buildgradle_4">build.gradle<a class="headerlink" href="#buildgradle_4" title="Permanent link">&para;</a></h6> <h6 id="buildgradle_4">build.gradle<a class="headerlink" href="#buildgradle_4" title="Permanent link">&para;</a></h6>
<pre><code class="language-groovy">implementation &quot;dev.inmo:tgbotapi.utils:$tgbotapi_version&quot; <div class="language-groovy highlight"><pre><span></span><code><span id="__span-10-1"><a id="__codelineno-10-1" name="__codelineno-10-1" href="#__codelineno-10-1"></a><span class="n">implementation</span><span class="w"> </span><span class="s2">&quot;dev.inmo:tgbotapi.utils:$tgbotapi_version&quot;</span>
</code></pre> </span></code></pre></div>
<h6 id="pomxml_4">pom.xml<a class="headerlink" href="#pomxml_4" title="Permanent link">&para;</a></h6> <h6 id="pomxml_4">pom.xml<a class="headerlink" href="#pomxml_4" title="Permanent link">&para;</a></h6>
<pre><code class="language-xml">&lt;dependency&gt; <div class="language-xml highlight"><pre><span></span><code><span id="__span-11-1"><a id="__codelineno-11-1" name="__codelineno-11-1" href="#__codelineno-11-1"></a><span class="nt">&lt;dependency&gt;</span>
&lt;groupId&gt;dev.inmo&lt;/groupId&gt; </span><span id="__span-11-2"><a id="__codelineno-11-2" name="__codelineno-11-2" href="#__codelineno-11-2"></a><span class="w"> </span><span class="nt">&lt;groupId&gt;</span>dev.inmo<span class="nt">&lt;/groupId&gt;</span>
&lt;artifactId&gt;tgbotapi.utils&lt;/artifactId&gt; </span><span id="__span-11-3"><a id="__codelineno-11-3" name="__codelineno-11-3" href="#__codelineno-11-3"></a><span class="w"> </span><span class="nt">&lt;artifactId&gt;</span>tgbotapi.utils<span class="nt">&lt;/artifactId&gt;</span>
&lt;version&gt;${tgbotapi_version}&lt;/version&gt; </span><span id="__span-11-4"><a id="__codelineno-11-4" name="__codelineno-11-4" href="#__codelineno-11-4"></a><span class="w"> </span><span class="nt">&lt;version&gt;</span>${tgbotapi_version}<span class="nt">&lt;/version&gt;</span>
&lt;/dependency&gt; </span><span id="__span-11-5"><a id="__codelineno-11-5" name="__codelineno-11-5" href="#__codelineno-11-5"></a><span class="nt">&lt;/dependency&gt;</span>
</code></pre> </span></code></pre></div>
<h2 id="next-steps">Next steps<a class="headerlink" href="#next-steps" title="Permanent link">&para;</a></h2> <h2 id="next-steps">Next steps<a class="headerlink" href="#next-steps" title="Permanent link">&para;</a></h2>
<ul> <ul>
<li><a href="proxy-setup.html">Proxy setup</a></li> <li><a href="proxy-setup.html">Proxy setup</a></li>

View File

@ -1230,20 +1230,20 @@
<h1 id="proxy-setup">Proxy setup<a class="headerlink" href="#proxy-setup" title="Permanent link">&para;</a></h1> <h1 id="proxy-setup">Proxy setup<a class="headerlink" href="#proxy-setup" title="Permanent link">&para;</a></h1>
<p>In some locations Telegram Bots API urls will be unavailable. In this case all examples will just throw exception like:</p> <p>In some locations Telegram Bots API urls will be unavailable. In this case all examples will just throw exception like:</p>
<pre><code class="language-bash">Exception in thread &quot;main&quot; java.net.ConnectException: Connection refused <div class="language-bash highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a>Exception<span class="w"> </span><span class="k">in</span><span class="w"> </span>thread<span class="w"> </span><span class="s2">&quot;main&quot;</span><span class="w"> </span>java.net.ConnectException:<span class="w"> </span>Connection<span class="w"> </span>refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) </span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span>at<span class="w"> </span>sun.nio.ch.SocketChannelImpl.checkConnect<span class="o">(</span>Native<span class="w"> </span>Method<span class="o">)</span>
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) </span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="w"> </span>at<span class="w"> </span>sun.nio.ch.SocketChannelImpl.finishConnect<span class="o">(</span>SocketChannelImpl.java:717<span class="o">)</span>
at io.ktor.network.sockets.SocketImpl.connect$ktor_network(SocketImpl.kt:36) </span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="w"> </span>at<span class="w"> </span>io.ktor.network.sockets.SocketImpl.connect<span class="nv">$ktor_network</span><span class="o">(</span>SocketImpl.kt:36<span class="o">)</span>
at io.ktor.network.sockets.SocketImpl$connect$1.invokeSuspend(SocketImpl.kt) </span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="w"> </span>at<span class="w"> </span>io.ktor.network.sockets.SocketImpl<span class="nv">$connect$1</span>.invokeSuspend<span class="o">(</span>SocketImpl.kt<span class="o">)</span>
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) </span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a><span class="w"> </span>at<span class="w"> </span>kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith<span class="o">(</span>ContinuationImpl.kt:33<span class="o">)</span>
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56) </span><span id="__span-0-7"><a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="w"> </span>at<span class="w"> </span>kotlinx.coroutines.DispatchedTask.run<span class="o">(</span>DispatchedTask.kt:56<span class="o">)</span>
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) </span><span id="__span-0-8"><a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="w"> </span>at<span class="w"> </span>kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely<span class="o">(</span>CoroutineScheduler.kt:571<span class="o">)</span>
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738) </span><span id="__span-0-9"><a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a><span class="w"> </span>at<span class="w"> </span>kotlinx.coroutines.scheduling.CoroutineScheduler<span class="nv">$Worker</span>.executeTask<span class="o">(</span>CoroutineScheduler.kt:738<span class="o">)</span>
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678) </span><span id="__span-0-10"><a id="__codelineno-0-10" name="__codelineno-0-10" href="#__codelineno-0-10"></a><span class="w"> </span>at<span class="w"> </span>kotlinx.coroutines.scheduling.CoroutineScheduler<span class="nv">$Worker</span>.runWorker<span class="o">(</span>CoroutineScheduler.kt:678<span class="o">)</span>
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665) </span><span id="__span-0-11"><a id="__codelineno-0-11" name="__codelineno-0-11" href="#__codelineno-0-11"></a><span class="w"> </span>at<span class="w"> </span>kotlinx.coroutines.scheduling.CoroutineScheduler<span class="nv">$Worker</span>.run<span class="o">(</span>CoroutineScheduler.kt:665<span class="o">)</span>
</span><span id="__span-0-12"><a id="__codelineno-0-12" name="__codelineno-0-12" href="#__codelineno-0-12"></a>
Process finished with exit code 1 </span><span id="__span-0-13"><a id="__codelineno-0-13" name="__codelineno-0-13" href="#__codelineno-0-13"></a>Process<span class="w"> </span>finished<span class="w"> </span>with<span class="w"> </span><span class="nb">exit</span><span class="w"> </span>code<span class="w"> </span><span class="m">1</span>
</code></pre> </span></code></pre></div>
<p>There are several ways to solve this problem:</p> <p>There are several ways to solve this problem:</p>
<ul> <ul>
<li>Built-in proxy config (will require some socks or http proxy server)</li> <li>Built-in proxy config (will require some socks or http proxy server)</li>
@ -1253,20 +1253,20 @@ Process finished with exit code 1
<h3 id="using-ktor-client-built-in-proxy">Using Ktor Client built-in proxy<a class="headerlink" href="#using-ktor-client-built-in-proxy" title="Permanent link">&para;</a></h3> <h3 id="using-ktor-client-built-in-proxy">Using Ktor Client built-in proxy<a class="headerlink" href="#using-ktor-client-built-in-proxy" title="Permanent link">&para;</a></h3>
<p>First of all, you will need to use one more library:</p> <p>First of all, you will need to use one more library:</p>
<p><strong>build.gradle</strong>:</p> <p><strong>build.gradle</strong>:</p>
<pre><code class="language-groovy">implementation &quot;io.ktor:ktor-client-okhttp:2.0.1&quot; <div class="language-groovy highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="n">implementation</span><span class="w"> </span><span class="s2">&quot;io.ktor:ktor-client-okhttp:2.0.1&quot;</span>
</code></pre> </span></code></pre></div>
<div class="admonition note"> <div class="admonition note">
<p class="admonition-title">Dependency note</p> <p class="admonition-title">Dependency note</p>
<p> <p>
In the snippet above was used version <code>2.0.1</code> which is actual for <code>TelegramBotAPI</code> at the moment of filling this documentation (<code>May 22 2022</code>, <code>TelegramBotAPI</code> version <code>2.0.0</code>) and you can update version of this dependency in case if it is outdated.</p> In the snippet above was used version <code>2.0.1</code> which is actual for <code>TelegramBotAPI</code> at the moment of filling this documentation (<code>May 22 2022</code>, <code>TelegramBotAPI</code> version <code>2.0.0</code>) and you can update version of this dependency in case if it is outdated.</p>
</div> </div>
<p>For configuring proxy for your bot inside your program, you can use next snippet:</p> <p>For configuring proxy for your bot inside your program, you can use next snippet:</p>
<pre><code class="language-kotlin">val botToken = &quot;HERE MUST BE YOUR TOKEN&quot; <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">botToken</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;HERE MUST BE YOUR TOKEN&quot;</span>
val bot = telegramBot(botToken) { </span><span id="__span-2-2"><a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">telegramBot</span><span class="p">(</span><span class="n">botToken</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
ktorClientEngineFactory = OkHttp </span><span id="__span-2-3"><a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="w"> </span><span class="n">ktorClientEngineFactory</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">OkHttp</span>
proxy = ProxyBuilder.socks(&quot;127.0.0.1&quot;, 1080) </span><span id="__span-2-4"><a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="w"> </span><span class="n">proxy</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ProxyBuilder</span><span class="p">.</span><span class="na">socks</span><span class="p">(</span><span class="s">&quot;127.0.0.1&quot;</span><span class="p">,</span><span class="w"> </span><span class="m">1080</span><span class="p">)</span>
} </span><span id="__span-2-5"><a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<p>Explanation line by line:</p> <p>Explanation line by line:</p>
<ol> <ol>
<li><code>val botToken = "HERE MUST BE YOUR TOKEN"</code> - here we are just creating variable <code>botToken</code></li> <li><code>val botToken = "HERE MUST BE YOUR TOKEN"</code> - here we are just creating variable <code>botToken</code></li>

View File

@ -1260,18 +1260,18 @@
<p><a href="https://github.com/InsanusMokrassar/TelegramBotAPI/tree/master/tgbotapi.api">API extensions</a> is a module which you may include in your project in addition to <a href="https://github.com/InsanusMokrassar/TelegramBotAPI/tree/master/tgbotapi.core">core part</a>. In most cases this module will allow just use syntax like <code>bot.getUpdates()</code> instead of <code>bot.execute(GetUpdates())</code>, but there are several other things you will achieve with that syntax.</p> <p><a href="https://github.com/InsanusMokrassar/TelegramBotAPI/tree/master/tgbotapi.api">API extensions</a> is a module which you may include in your project in addition to <a href="https://github.com/InsanusMokrassar/TelegramBotAPI/tree/master/tgbotapi.core">core part</a>. In most cases this module will allow just use syntax like <code>bot.getUpdates()</code> instead of <code>bot.execute(GetUpdates())</code>, but there are several other things you will achieve with that syntax.</p>
<h2 id="bot-builder">Bot builder<a class="headerlink" href="#bot-builder" title="Permanent link">&para;</a></h2> <h2 id="bot-builder">Bot builder<a class="headerlink" href="#bot-builder" title="Permanent link">&para;</a></h2>
<p>This functionality allow you to build bot in more unified and comfortable way than standard creating with <code>telegramBot</code> function</p> <p>This functionality allow you to build bot in more unified and comfortable way than standard creating with <code>telegramBot</code> function</p>
<pre><code class="language-kotlin">buildBot( <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="n">buildBot</span><span class="p">(</span>
&quot;TOKEN&quot; </span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="s">&quot;TOKEN&quot;</span>
) { </span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="p">)</span><span class="w"> </span><span class="p">{</span>
proxy = ProxyBuilder.socks(host = &quot;127.0.0.1&quot;, port = 4001) // just an example, more info on https://ktor.io/docs/proxy.html </span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="w"> </span><span class="n">proxy</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ProxyBuilder</span><span class="p">.</span><span class="na">socks</span><span class="p">(</span><span class="n">host</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;127.0.0.1&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">port</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4001</span><span class="p">)</span><span class="w"> </span><span class="c1">// just an example, more info on https://ktor.io/docs/proxy.html</span>
ktorClientConfig = { </span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="w"> </span><span class="n">ktorClientConfig</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
// configuring of ktor client </span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a><span class="w"> </span><span class="c1">// configuring of ktor client</span>
} </span><span id="__span-0-7"><a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="w"> </span><span class="p">}</span>
ktorClientEngineFactory = { </span><span id="__span-0-8"><a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="w"> </span><span class="n">ktorClientEngineFactory</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
// configuring of ktor client engine </span><span id="__span-0-9"><a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a><span class="w"> </span><span class="c1">// configuring of ktor client engine </span>
} </span><span id="__span-0-10"><a id="__codelineno-0-10" name="__codelineno-0-10" href="#__codelineno-0-10"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-0-11"><a id="__codelineno-0-11" name="__codelineno-0-11" href="#__codelineno-0-11"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<h2 id="downloading-of-files">Downloading of files<a class="headerlink" href="#downloading-of-files" title="Permanent link">&para;</a></h2> <h2 id="downloading-of-files">Downloading of files<a class="headerlink" href="#downloading-of-files" title="Permanent link">&para;</a></h2>
<p>In standard library requests there are no way to download some file retrieved in updates or after requests. You may use syntax like <code>bot.downloadFile(file)</code> where <code>file</code> is <code>TelegramMediaFile</code> from telegram, <code>FileId</code> or even <code>PathedFile</code> from <a href="https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/get/GetFile.kt">GetFile</a> request (<a href="https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/files/DownloadFile.kt">sources</a>).</p> <p>In standard library requests there are no way to download some file retrieved in updates or after requests. You may use syntax like <code>bot.downloadFile(file)</code> where <code>file</code> is <code>TelegramMediaFile</code> from telegram, <code>FileId</code> or even <code>PathedFile</code> from <a href="https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/get/GetFile.kt">GetFile</a> request (<a href="https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/files/DownloadFile.kt">sources</a>).</p>
<h2 id="live-location">Live location<a class="headerlink" href="#live-location" title="Permanent link">&para;</a></h2> <h2 id="live-location">Live location<a class="headerlink" href="#live-location" title="Permanent link">&para;</a></h2>

View File

@ -1242,12 +1242,12 @@
<li><code>startChain</code> which will add new state for handling</li> <li><code>startChain</code> which will add new state for handling</li>
</ul> </ul>
<p>The most based way to create <code>StatesMachine</code> and register <code>StateHandler</code>s looks like in the next snippet:</p> <p>The most based way to create <code>StatesMachine</code> and register <code>StateHandler</code>s looks like in the next snippet:</p>
<pre><code class="language-kotlin">buildFSM&lt;TrafficLightState&gt; { <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="n">buildFSM</span><span class="o">&lt;</span><span class="n">TrafficLightState</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span>
strictlyOn&lt;SomeState&gt; { </span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="n">strictlyOn</span><span class="o">&lt;</span><span class="n">SomeState</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span>
// state handling </span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="w"> </span><span class="c1">// state handling</span>
} </span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="w"> </span><span class="p">}</span>
}.start(CoroutineScope(...)).join() </span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="p">}.</span><span class="na">start</span><span class="p">(</span><span class="n">CoroutineScope</span><span class="p">(...)).</span><span class="na">join</span><span class="p">()</span>
</code></pre> </span></code></pre></div>
<div class="admonition note"> <div class="admonition note">
<p class="admonition-title">Full example</p> <p class="admonition-title">Full example</p>
<p> <p>
@ -1273,11 +1273,11 @@ You may find full example of FSM usage in <a href="https://github.com/InsanusMok
</li> </li>
</ul> </ul>
<p>All of them will take as an callback some object with type <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.behaviour_builder/index.html#-1892390839%2FClasslikes%2F-1982836883">CustomBehaviourContextReceiver</a> and will looks like in the next snippet:</p> <p>All of them will take as an callback some object with type <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.behaviour_builder/index.html#-1892390839%2FClasslikes%2F-1982836883">CustomBehaviourContextReceiver</a> and will looks like in the next snippet:</p>
<pre><code class="language-kotlin">telegramBotWithBehaviourAndFSMAndStartLongPolling&lt;YourStateType&gt;(&quot;BOT_TOKEN&quot;) { <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="n">telegramBotWithBehaviourAndFSMAndStartLongPolling</span><span class="o">&lt;</span><span class="n">YourStateType</span><span class="o">&gt;</span><span class="p">(</span><span class="s">&quot;BOT_TOKEN&quot;</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
// here you may use any operations from BehaviourBuilder </span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a><span class="w"> </span><span class="c1">// here you may use any operations from BehaviourBuilder</span>
// here you may use any operations from BehaviourContextWithFSMBuilder like strictlyOn and others </span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="w"> </span><span class="c1">// here you may use any operations from BehaviourContextWithFSMBuilder like strictlyOn and others</span>
} </span><span id="__span-1-4"><a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<h2 id="examples">Examples<a class="headerlink" href="#examples" title="Permanent link">&para;</a></h2> <h2 id="examples">Examples<a class="headerlink" href="#examples" title="Permanent link">&para;</a></h2>
<ul> <ul>
<li><a href="https://github.com/InsanusMokrassar/TelegramBotAPI-examples/blob/master/FSMBot/src/main/kotlin/SimpleFSMBot.kt">TelegramBotAPI-examples/FSMBot</a></li> <li><a href="https://github.com/InsanusMokrassar/TelegramBotAPI-examples/blob/master/FSMBot/src/main/kotlin/SimpleFSMBot.kt">TelegramBotAPI-examples/FSMBot</a></li>

View File

@ -1253,50 +1253,50 @@
</ul> </ul>
<h2 id="initialization">Initialization<a class="headerlink" href="#initialization" title="Permanent link">&para;</a></h2> <h2 id="initialization">Initialization<a class="headerlink" href="#initialization" title="Permanent link">&para;</a></h2>
<p>As was said above, there is <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.behaviour_builder/build-behaviour.html">buildBehaviour</a> function which allow you set up your bot logic. Let&rsquo;s see an example:</p> <p>As was said above, there is <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.behaviour_builder/build-behaviour.html">buildBehaviour</a> function which allow you set up your bot logic. Let&rsquo;s see an example:</p>
<pre><code class="language-kotlin">val bot = telegramBot(&quot;TOKEN&quot;) <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">telegramBot</span><span class="p">(</span><span class="s">&quot;TOKEN&quot;</span><span class="p">)</span>
</span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a>
bot.buildBehaviour { </span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="n">bot</span><span class="p">.</span><span class="na">buildBehaviour</span><span class="w"> </span><span class="p">{</span>
onCommand(&quot;start&quot;) { // creating of trigger </span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="w"> </span><span class="n">onCommand</span><span class="p">(</span><span class="s">&quot;start&quot;</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// creating of trigger</span>
val message = it </span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">it</span>
val content = message.content </span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">content</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">message</span><span class="p">.</span><span class="na">content</span>
</span><span id="__span-0-7"><a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a>
reply(message, &quot;Ok, send me one photo&quot;) // send text message with replying on incoming message </span><span id="__span-0-8"><a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="w"> </span><span class="n">reply</span><span class="p">(</span><span class="n">message</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Ok, send me one photo&quot;</span><span class="p">)</span><span class="w"> </span><span class="c1">// send text message with replying on incoming message</span>
</span><span id="__span-0-9"><a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a>
val photoContent = waitPhoto().first() // waitPhoto will return List, so, get first element </span><span id="__span-0-10"><a id="__codelineno-0-10" name="__codelineno-0-10" href="#__codelineno-0-10"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">photoContent</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">waitPhoto</span><span class="p">().</span><span class="na">first</span><span class="p">()</span><span class="w"> </span><span class="c1">// waitPhoto will return List, so, get first element</span>
</span><span id="__span-0-11"><a id="__codelineno-0-11" name="__codelineno-0-11" href="#__codelineno-0-11"></a>
val photo = downloadFile(photoContent) // ByteArray of photo </span><span id="__span-0-12"><a id="__codelineno-0-12" name="__codelineno-0-12" href="#__codelineno-0-12"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">photo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">downloadFile</span><span class="p">(</span><span class="n">photoContent</span><span class="p">)</span><span class="w"> </span><span class="c1">// ByteArray of photo</span>
</span><span id="__span-0-13"><a id="__codelineno-0-13" name="__codelineno-0-13" href="#__codelineno-0-13"></a>
// some logic with saving of photos </span><span id="__span-0-14"><a id="__codelineno-0-14" name="__codelineno-0-14" href="#__codelineno-0-14"></a><span class="w"> </span><span class="c1">// some logic with saving of photos</span>
} </span><span id="__span-0-15"><a id="__codelineno-0-15" name="__codelineno-0-15" href="#__codelineno-0-15"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-0-16"><a id="__codelineno-0-16" name="__codelineno-0-16" href="#__codelineno-0-16"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<h2 id="filters">Filters<a class="headerlink" href="#filters" title="Permanent link">&para;</a></h2> <h2 id="filters">Filters<a class="headerlink" href="#filters" title="Permanent link">&para;</a></h2>
<p>In most cases there are opportunity to filter some of messages before starting of main logic. Let&rsquo;s look at this using the example above:</p> <p>In most cases there are opportunity to filter some of messages before starting of main logic. Let&rsquo;s look at this using the example above:</p>
<pre><code class="language-kotlin">val bot = telegramBot(&quot;TOKEN&quot;) <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">telegramBot</span><span class="p">(</span><span class="s">&quot;TOKEN&quot;</span><span class="p">)</span>
</span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a>
bot.buildBehaviour { </span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="n">bot</span><span class="p">.</span><span class="na">buildBehaviour</span><span class="w"> </span><span class="p">{</span>
onCommand( </span><span id="__span-1-4"><a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="w"> </span><span class="n">onCommand</span><span class="p">(</span>
&quot;start&quot;, </span><span id="__span-1-5"><a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="w"> </span><span class="s">&quot;start&quot;</span><span class="p">,</span>
initialFilter = { </span><span id="__span-1-6"><a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a><span class="w"> </span><span class="n">initialFilter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
it.content.textSources.size == 1 // make sure that user has sent /start without any additions </span><span id="__span-1-7"><a id="__codelineno-1-7" name="__codelineno-1-7" href="#__codelineno-1-7"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">content</span><span class="p">.</span><span class="na">textSources</span><span class="p">.</span><span class="na">size</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="c1">// make sure that user has sent /start without any additions</span>
} </span><span id="__span-1-8"><a id="__codelineno-1-8" name="__codelineno-1-8" href="#__codelineno-1-8"></a><span class="w"> </span><span class="p">}</span>
) { </span><span id="__span-1-9"><a id="__codelineno-1-9" name="__codelineno-1-9" href="#__codelineno-1-9"></a><span class="w"> </span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
// ... </span><span id="__span-1-10"><a id="__codelineno-1-10" name="__codelineno-1-10" href="#__codelineno-1-10"></a><span class="w"> </span><span class="c1">// ...</span>
} </span><span id="__span-1-11"><a id="__codelineno-1-11" name="__codelineno-1-11" href="#__codelineno-1-11"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-1-12"><a id="__codelineno-1-12" name="__codelineno-1-12" href="#__codelineno-1-12"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<p>OR</p> <p>OR</p>
<pre><code class="language-kotlin">val bot = telegramBot(&quot;TOKEN&quot;) <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">telegramBot</span><span class="p">(</span><span class="s">&quot;TOKEN&quot;</span><span class="p">)</span>
</span><span id="__span-2-2"><a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a>
bot.buildBehaviour { </span><span id="__span-2-3"><a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="n">bot</span><span class="p">.</span><span class="na">buildBehaviour</span><span class="w"> </span><span class="p">{</span>
onCommand( </span><span id="__span-2-4"><a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="w"> </span><span class="n">onCommand</span><span class="p">(</span>
&quot;start&quot;, </span><span id="__span-2-5"><a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a><span class="w"> </span><span class="s">&quot;start&quot;</span><span class="p">,</span>
requireOnlyCommandInMessage = true // it is default, but you can overwrite it with `requireOnlyCommandInMessage = false` </span><span id="__span-2-6"><a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a><span class="w"> </span><span class="n">requireOnlyCommandInMessage</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="c1">// it is default, but you can overwrite it with `requireOnlyCommandInMessage = false`</span>
) { </span><span id="__span-2-7"><a id="__codelineno-2-7" name="__codelineno-2-7" href="#__codelineno-2-7"></a><span class="w"> </span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
// ... </span><span id="__span-2-8"><a id="__codelineno-2-8" name="__codelineno-2-8" href="#__codelineno-2-8"></a><span class="w"> </span><span class="c1">// ...</span>
} </span><span id="__span-2-9"><a id="__codelineno-2-9" name="__codelineno-2-9" href="#__codelineno-2-9"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-2-10"><a id="__codelineno-2-10" name="__codelineno-2-10" href="#__codelineno-2-10"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>

View File

@ -1292,73 +1292,71 @@
</ul> </ul>
<h3 id="catching-with-result">Catching with result<a class="headerlink" href="#catching-with-result" title="Permanent link">&para;</a></h3> <h3 id="catching-with-result">Catching with result<a class="headerlink" href="#catching-with-result" title="Permanent link">&para;</a></h3>
<p>If you prefer to receive <code>Result</code> objects instead of some weird callbacks, you may use the next syntax:</p> <p>If you prefer to receive <code>Result</code> objects instead of some weird callbacks, you may use the next syntax:</p>
<pre><code class="language-kotlin">safelyWithResult { <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="n">safelyWithResult</span><span class="w"> </span><span class="p">{</span>
// do something </span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="c1">// do something</span>
}.onSuccess { // will be called if everything is right </span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="p">}.</span><span class="na">onSuccess</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// will be called if everything is right</span>
// handle success </span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="w"> </span><span class="c1">// handle success</span>
}.onFailure { // will be called if something went wrong </span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="p">}.</span><span class="na">onFailure</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// will be called if something went wrong</span>
// handle error </span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a><span class="w"> </span><span class="c1">// handle error</span>
it.printStackTrace() </span><span id="__span-0-7"><a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">printStackTrace</span><span class="p">()</span>
}.getOrThrow() // will return value or throw exception </span><span id="__span-0-8"><a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="p">}.</span><span class="na">getOrThrow</span><span class="p">()</span><span class="w"> </span><span class="c1">// will return value or throw exception</span>
</code></pre> </span></code></pre></div>
<h3 id="catching-with-callback">Catching with callback<a class="headerlink" href="#catching-with-callback" title="Permanent link">&para;</a></h3> <h3 id="catching-with-callback">Catching with callback<a class="headerlink" href="#catching-with-callback" title="Permanent link">&para;</a></h3>
<p>Also there is more simple (in some cases) way to handle exceptions with callbacks:</p> <p>Also there is more simple (in some cases) way to handle exceptions with callbacks:</p>
<pre><code class="language-kotlin">safely( <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="n">safely</span><span class="p">(</span>
{ </span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a><span class="w"> </span><span class="p">{</span>
// handle error </span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="w"> </span><span class="c1">// handle error</span>
it.printStackTrace() </span><span id="__span-1-4"><a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">printStackTrace</span><span class="p">()</span>
null // return value </span><span id="__span-1-5"><a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="w"> </span><span class="kc">null</span><span class="w"> </span><span class="c1">// return value</span>
} </span><span id="__span-1-6"><a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a><span class="w"> </span><span class="p">}</span>
) { </span><span id="__span-1-7"><a id="__codelineno-1-7" name="__codelineno-1-7" href="#__codelineno-1-7"></a><span class="p">)</span><span class="w"> </span><span class="p">{</span>
// do something </span><span id="__span-1-8"><a id="__codelineno-1-8" name="__codelineno-1-8" href="#__codelineno-1-8"></a><span class="w"> </span><span class="c1">// do something</span>
} </span><span id="__span-1-9"><a id="__codelineno-1-9" name="__codelineno-1-9" href="#__codelineno-1-9"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<h3 id="bonus-different-types-of-handling">Bonus: different types of handling<a class="headerlink" href="#bonus-different-types-of-handling" title="Permanent link">&para;</a></h3> <h3 id="bonus-different-types-of-handling">Bonus: different types of handling<a class="headerlink" href="#bonus-different-types-of-handling" title="Permanent link">&para;</a></h3>
<p>There are two types of handling:</p> <p>There are two types of handling:</p>
<ul> <ul>
<li>Just safely - when you are using something to obviously retrieve value or throw exception. When handling callback has been skipped, it will throw exception by default. For example:</li> <li>Just safely - when you are using something to obviously retrieve value or throw exception. When handling callback has been skipped, it will throw exception by default. For example:
<div class="language-kotlin highlight"><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="n">safely</span><span class="p">(</span>
</span><span id="__span-2-2"><a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a><span class="w"> </span><span class="p">{</span>
</span><span id="__span-2-3"><a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">printStackTrace</span><span class="p">()</span>
</span><span id="__span-2-4"><a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="w"> </span><span class="s">&quot;error&quot;</span>
</span><span id="__span-2-5"><a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a><span class="w"> </span><span class="p">}</span>
</span><span id="__span-2-6"><a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a><span class="p">)</span><span class="w"> </span><span class="p">{</span>
</span><span id="__span-2-7"><a id="__codelineno-2-7" name="__codelineno-2-7" href="#__codelineno-2-7"></a><span class="w"> </span><span class="n">error</span><span class="p">(</span><span class="s">&quot;Hi :)&quot;</span><span class="p">)</span><span class="w"> </span><span class="c1">// emulate exception throwing</span>
</span><span id="__span-2-8"><a id="__codelineno-2-8" name="__codelineno-2-8" href="#__codelineno-2-8"></a><span class="w"> </span><span class="s">&quot;ok&quot;</span>
</span><span id="__span-2-9"><a id="__codelineno-2-9" name="__codelineno-2-9" href="#__codelineno-2-9"></a><span class="p">}</span><span class="w"> </span><span class="c1">// result will be with type String</span>
</span></code></pre></div></li>
<li>Safely without exceptions - almost the same as <code>safely</code>, but this type by default allow to return nullable value (when exception was thrown) instead of just throwing (as with <code>safely</code>):
<div class="language-kotlin highlight"><pre><span></span><code><span id="__span-3-1"><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a><span class="n">safelyWithouExceptions</span><span class="w"> </span><span class="p">{</span>
</span><span id="__span-3-2"><a id="__codelineno-3-2" name="__codelineno-3-2" href="#__codelineno-3-2"></a><span class="w"> </span><span class="c1">// do something</span>
</span><span id="__span-3-3"><a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a><span class="p">}</span><span class="w"> </span><span class="c1">// will returns nullable result type</span>
</span></code></pre></div></li>
</ul> </ul>
<pre><code class="language-kotlin">safely(
{
it.printStackTrace()
&quot;error&quot;
}
) {
error(&quot;Hi :)&quot;) // emulate exception throwing
&quot;ok&quot;
} // result will be with type String
</code></pre>
<ul>
<li>Safely without exceptions - almost the same as <code>safely</code>, but this type by default allow to return nullable value (when exception was thrown) instead of just throwing (as with <code>safely</code>):</li>
</ul>
<pre><code class="language-kotlin">safelyWithouExceptions {
// do something
} // will returns nullable result type
</code></pre>
<h2 id="global-exceptions-handling">Global exceptions handling<a class="headerlink" href="#global-exceptions-handling" title="Permanent link">&para;</a></h2> <h2 id="global-exceptions-handling">Global exceptions handling<a class="headerlink" href="#global-exceptions-handling" title="Permanent link">&para;</a></h2>
<p>The most simple way to configure exceptions handling is to change <code>CoroutineContext</code> when you are creating your <code>CoroutineScope</code> for bot processing:</p> <p>The most simple way to configure exceptions handling is to change <code>CoroutineContext</code> when you are creating your <code>CoroutineScope</code> for bot processing:</p>
<pre><code class="language-kotlin">val bot = telegramBot(&quot;TOKEN&quot;) <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-4-1"><a id="__codelineno-4-1" name="__codelineno-4-1" href="#__codelineno-4-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">telegramBot</span><span class="p">(</span><span class="s">&quot;TOKEN&quot;</span><span class="p">)</span>
</span><span id="__span-4-2"><a id="__codelineno-4-2" name="__codelineno-4-2" href="#__codelineno-4-2"></a>
bot.buildBehaviour ( </span><span id="__span-4-3"><a id="__codelineno-4-3" name="__codelineno-4-3" href="#__codelineno-4-3"></a><span class="n">bot</span><span class="p">.</span><span class="na">buildBehaviour</span><span class="w"> </span><span class="p">(</span>
scope = scope, </span><span id="__span-4-4"><a id="__codelineno-4-4" name="__codelineno-4-4" href="#__codelineno-4-4"></a><span class="w"> </span><span class="n">scope</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">scope</span><span class="p">,</span>
defaultExceptionsHandler = { </span><span id="__span-4-5"><a id="__codelineno-4-5" name="__codelineno-4-5" href="#__codelineno-4-5"></a><span class="w"> </span><span class="n">defaultExceptionsHandler</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
it.printStackTrace() </span><span id="__span-4-6"><a id="__codelineno-4-6" name="__codelineno-4-6" href="#__codelineno-4-6"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">printStackTrace</span><span class="p">()</span>
} </span><span id="__span-4-7"><a id="__codelineno-4-7" name="__codelineno-4-7" href="#__codelineno-4-7"></a><span class="w"> </span><span class="p">}</span>
) { </span><span id="__span-4-8"><a id="__codelineno-4-8" name="__codelineno-4-8" href="#__codelineno-4-8"></a><span class="p">)</span><span class="w"> </span><span class="p">{</span>
// ... </span><span id="__span-4-9"><a id="__codelineno-4-9" name="__codelineno-4-9" href="#__codelineno-4-9"></a><span class="w"> </span><span class="c1">// ...</span>
} </span><span id="__span-4-10"><a id="__codelineno-4-10" name="__codelineno-4-10" href="#__codelineno-4-10"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<p>OR</p> <p>OR</p>
<pre><code class="language-kotlin">val bot = telegramBotWithBehaviour ( <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-5-1"><a id="__codelineno-5-1" name="__codelineno-5-1" href="#__codelineno-5-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">telegramBotWithBehaviour</span><span class="w"> </span><span class="p">(</span>
&quot;TOKEN&quot;, </span><span id="__span-5-2"><a id="__codelineno-5-2" name="__codelineno-5-2" href="#__codelineno-5-2"></a><span class="w"> </span><span class="s">&quot;TOKEN&quot;</span><span class="p">,</span>
scope = scope, </span><span id="__span-5-3"><a id="__codelineno-5-3" name="__codelineno-5-3" href="#__codelineno-5-3"></a><span class="w"> </span><span class="n">scope</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">scope</span><span class="p">,</span>
defaultExceptionsHandler = { </span><span id="__span-5-4"><a id="__codelineno-5-4" name="__codelineno-5-4" href="#__codelineno-5-4"></a><span class="w"> </span><span class="n">defaultExceptionsHandler</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
it.printStackTrace() </span><span id="__span-5-5"><a id="__codelineno-5-5" name="__codelineno-5-5" href="#__codelineno-5-5"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">printStackTrace</span><span class="p">()</span>
} </span><span id="__span-5-6"><a id="__codelineno-5-6" name="__codelineno-5-6" href="#__codelineno-5-6"></a><span class="w"> </span><span class="p">}</span>
) { </span><span id="__span-5-7"><a id="__codelineno-5-7" name="__codelineno-5-7" href="#__codelineno-5-7"></a><span class="p">)</span><span class="w"> </span><span class="p">{</span>
// ... </span><span id="__span-5-8"><a id="__codelineno-5-8" name="__codelineno-5-8" href="#__codelineno-5-8"></a><span class="w"> </span><span class="c1">// ...</span>
} </span><span id="__span-5-9"><a id="__codelineno-5-9" name="__codelineno-5-9" href="#__codelineno-5-9"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<p>Here we have used <code>ContextSafelyExceptionHandler</code> class. It will pass default handling of exceptions and will call the block in most cases when something inside of your bot logic has thrown exception.</p> <p>Here we have used <code>ContextSafelyExceptionHandler</code> class. It will pass default handling of exceptions and will call the block in most cases when something inside of your bot logic has thrown exception.</p>

View File

@ -1183,10 +1183,10 @@
</ul> </ul>
<h3 id="where-to-get-file-id-or-url">Where to get File id or url?<a class="headerlink" href="#where-to-get-file-id-or-url" title="Permanent link">&para;</a></h3> <h3 id="where-to-get-file-id-or-url">Where to get File id or url?<a class="headerlink" href="#where-to-get-file-id-or-url" title="Permanent link">&para;</a></h3>
<p>The most simple way to send some file is to get file id and send it. You may get file id from any message with media. For example, if you have received some <a href="https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/Message.kt#L12">Message</a>, you may use <a href="https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt#L1094">asCommonMessage</a> conversation to be able to get its <code>content</code> and then convert it to some content with media. Full code here:</p> <p>The most simple way to send some file is to get file id and send it. You may get file id from any message with media. For example, if you have received some <a href="https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/Message.kt#L12">Message</a>, you may use <a href="https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt#L1094">asCommonMessage</a> conversation to be able to get its <code>content</code> and then convert it to some content with media. Full code here:</p>
<pre><code class="language-kotlin">val message: Message; <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">message</span><span class="p">:</span><span class="w"> </span><span class="n">Message</span><span class="p">;</span>
</span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a>
val fileId = message.asCommonMessage() ?.withContent&lt;MediaContent&gt;() ?.content ?.media ?.fileId; </span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="kd">val</span><span class="w"> </span><span class="nv">fileId</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">message</span><span class="p">.</span><span class="na">asCommonMessage</span><span class="p">()</span><span class="w"> </span><span class="o">?.</span><span class="na">withContent</span><span class="o">&lt;</span><span class="n">MediaContent</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="o">?.</span><span class="na">content</span><span class="w"> </span><span class="o">?.</span><span class="na">media</span><span class="w"> </span><span class="o">?.</span><span class="na">fileId</span><span class="p">;</span>
</code></pre> </span></code></pre></div>
<p><strong>WAT? O.o</strong></p> <p><strong>WAT? O.o</strong></p>
<p>In the code above we get some message, safely converted it to <code>CommonMessage</code> with <code>asCommonMessage</code>, then safely took its content via <code>withContent&lt;MediaContent&gt;() ?.content</code> and then just get its media file id.</p> <p>In the code above we get some message, safely converted it to <code>CommonMessage</code> with <code>asCommonMessage</code>, then safely took its content via <code>withContent&lt;MediaContent&gt;() ?.content</code> and then just get its media file id.</p>
<h2 id="download-files">Download files<a class="headerlink" href="#download-files" title="Permanent link">&para;</a></h2> <h2 id="download-files">Download files<a class="headerlink" href="#download-files" title="Permanent link">&para;</a></h2>
@ -1198,61 +1198,61 @@ val fileId = message.asCommonMessage() ?.withContent&lt;MediaContent&gt;() ?.con
</ul> </ul>
<h3 id="downloading-with-api-extensions">Downloading with <code>API</code> extensions<a class="headerlink" href="#downloading-with-api-extensions" title="Permanent link">&para;</a></h3> <h3 id="downloading-with-api-extensions">Downloading with <code>API</code> extensions<a class="headerlink" href="#downloading-with-api-extensions" title="Permanent link">&para;</a></h3>
<h4 id="files-jvmandroid">Files (JVM/Android)<a class="headerlink" href="#files-jvmandroid" title="Permanent link">&para;</a></h4> <h4 id="files-jvmandroid">Files (JVM/Android)<a class="headerlink" href="#files-jvmandroid" title="Permanent link">&para;</a></h4>
<pre><code class="language-kotlin">val bot: TelegramBot; <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="p">:</span><span class="w"> </span><span class="n">TelegramBot</span><span class="p">;</span>
val fileId: FileId; </span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a><span class="kd">val</span><span class="w"> </span><span class="nv">fileId</span><span class="p">:</span><span class="w"> </span><span class="n">FileId</span><span class="p">;</span>
val outputFile: File; </span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="kd">val</span><span class="w"> </span><span class="nv">outputFile</span><span class="p">:</span><span class="w"> </span><span class="n">File</span><span class="p">;</span>
</span><span id="__span-1-4"><a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a>
bot.downloadFile(fileId, outputFile) </span><span id="__span-1-5"><a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="n">bot</span><span class="p">.</span><span class="na">downloadFile</span><span class="p">(</span><span class="n">fileId</span><span class="p">,</span><span class="w"> </span><span class="n">outputFile</span><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>See <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.api.files/download-file.html">downloadFile</a> extension docs in the <strong>JVM tab</strong> to get more available options</p> <p>See <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.api.files/download-file.html">downloadFile</a> extension docs in the <strong>JVM tab</strong> to get more available options</p>
<p>There is also way with saving of data into temporal file. That will allow you to do with data whatever you want without high requirements to memory or network connection:</p> <p>There is also way with saving of data into temporal file. That will allow you to do with data whatever you want without high requirements to memory or network connection:</p>
<pre><code class="language-kotlin">val bot: TelegramBot; <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="p">:</span><span class="w"> </span><span class="n">TelegramBot</span><span class="p">;</span>
val fileId: FileId; </span><span id="__span-2-2"><a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a><span class="kd">val</span><span class="w"> </span><span class="nv">fileId</span><span class="p">:</span><span class="w"> </span><span class="n">FileId</span><span class="p">;</span>
</span><span id="__span-2-3"><a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a>
val tempFile: File = bot.downloadFileToTemp(fileId) </span><span id="__span-2-4"><a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="kd">val</span><span class="w"> </span><span class="nv">tempFile</span><span class="p">:</span><span class="w"> </span><span class="n">File</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bot</span><span class="p">.</span><span class="na">downloadFileToTemp</span><span class="p">(</span><span class="n">fileId</span><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>See <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.api.files/download-file-to-temp.html">downloadFileToTemp</a> extension docs to get more available options</p> <p>See <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.api.files/download-file-to-temp.html">downloadFileToTemp</a> extension docs to get more available options</p>
<h4 id="byte-read-channel">Byte read channel<a class="headerlink" href="#byte-read-channel" title="Permanent link">&para;</a></h4> <h4 id="byte-read-channel">Byte read channel<a class="headerlink" href="#byte-read-channel" title="Permanent link">&para;</a></h4>
<pre><code class="language-kotlin">val bot: TelegramBot; <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-3-1"><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="p">:</span><span class="w"> </span><span class="n">TelegramBot</span><span class="p">;</span>
val fileId: FileId; </span><span id="__span-3-2"><a id="__codelineno-3-2" name="__codelineno-3-2" href="#__codelineno-3-2"></a><span class="kd">val</span><span class="w"> </span><span class="nv">fileId</span><span class="p">:</span><span class="w"> </span><span class="n">FileId</span><span class="p">;</span>
</span><span id="__span-3-3"><a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a>
val bytes: ByteReadChannelAllocator = bot.downloadFileStream(fileId) </span><span id="__span-3-4"><a id="__codelineno-3-4" name="__codelineno-3-4" href="#__codelineno-3-4"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bytes</span><span class="p">:</span><span class="w"> </span><span class="n">ByteReadChannelAllocator</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bot</span><span class="p">.</span><span class="na">downloadFileStream</span><span class="p">(</span><span class="n">fileId</span><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>See <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.api.files/download-file-stream.html">downloadFileStream</a> extension docs to get more available options</p> <p>See <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.api.files/download-file-stream.html">downloadFileStream</a> extension docs to get more available options</p>
<h4 id="byte-read-channel-allocator">Byte read channel allocator<a class="headerlink" href="#byte-read-channel-allocator" title="Permanent link">&para;</a></h4> <h4 id="byte-read-channel-allocator">Byte read channel allocator<a class="headerlink" href="#byte-read-channel-allocator" title="Permanent link">&para;</a></h4>
<pre><code class="language-kotlin">val bot: TelegramBot; <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-4-1"><a id="__codelineno-4-1" name="__codelineno-4-1" href="#__codelineno-4-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="p">:</span><span class="w"> </span><span class="n">TelegramBot</span><span class="p">;</span>
val fileId: FileId; </span><span id="__span-4-2"><a id="__codelineno-4-2" name="__codelineno-4-2" href="#__codelineno-4-2"></a><span class="kd">val</span><span class="w"> </span><span class="nv">fileId</span><span class="p">:</span><span class="w"> </span><span class="n">FileId</span><span class="p">;</span>
</span><span id="__span-4-3"><a id="__codelineno-4-3" name="__codelineno-4-3" href="#__codelineno-4-3"></a>
val bytes: ByteReadChannelAllocator = bot.downloadFileStreamAllocator(fileId) </span><span id="__span-4-4"><a id="__codelineno-4-4" name="__codelineno-4-4" href="#__codelineno-4-4"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bytes</span><span class="p">:</span><span class="w"> </span><span class="n">ByteReadChannelAllocator</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bot</span><span class="p">.</span><span class="na">downloadFileStreamAllocator</span><span class="p">(</span><span class="n">fileId</span><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>See <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.api.files/download-file-stream-allocator.html">downloadFileStreamAllocator</a> extension docs to get more available options</p> <p>See <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.api.files/download-file-stream-allocator.html">downloadFileStreamAllocator</a> extension docs to get more available options</p>
<h4 id="byte-arrays">Byte arrays<a class="headerlink" href="#byte-arrays" title="Permanent link">&para;</a></h4> <h4 id="byte-arrays">Byte arrays<a class="headerlink" href="#byte-arrays" title="Permanent link">&para;</a></h4>
<pre><code class="language-kotlin">val bot: TelegramBot; <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-5-1"><a id="__codelineno-5-1" name="__codelineno-5-1" href="#__codelineno-5-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="p">:</span><span class="w"> </span><span class="n">TelegramBot</span><span class="p">;</span>
val fileId: FileId; </span><span id="__span-5-2"><a id="__codelineno-5-2" name="__codelineno-5-2" href="#__codelineno-5-2"></a><span class="kd">val</span><span class="w"> </span><span class="nv">fileId</span><span class="p">:</span><span class="w"> </span><span class="n">FileId</span><span class="p">;</span>
</span><span id="__span-5-3"><a id="__codelineno-5-3" name="__codelineno-5-3" href="#__codelineno-5-3"></a>
val bytes: ByteArray = bot.downloadFile(fileId) </span><span id="__span-5-4"><a id="__codelineno-5-4" name="__codelineno-5-4" href="#__codelineno-5-4"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bytes</span><span class="p">:</span><span class="w"> </span><span class="n">ByteArray</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bot</span><span class="p">.</span><span class="na">downloadFile</span><span class="p">(</span><span class="n">fileId</span><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>See <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.api.files/download-file.html">downloadFile</a> extension docs to get more available options</p> <p>See <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.api.files/download-file.html">downloadFile</a> extension docs to get more available options</p>
<h3 id="low-level-or-how-does-it-work">Low level or <code>how does it work?</code><a class="headerlink" href="#low-level-or-how-does-it-work" title="Permanent link">&para;</a></h3> <h3 id="low-level-or-how-does-it-work">Low level or <code>how does it work?</code><a class="headerlink" href="#low-level-or-how-does-it-work" title="Permanent link">&para;</a></h3>
<p>You may download file with streams or with downloading into the memory first. On low level you should do several things. They are presented in next snippet:</p> <p>You may download file with streams or with downloading into the memory first. On low level you should do several things. They are presented in next snippet:</p>
<pre><code class="language-kotlin">val bot: TelegramBot; <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-6-1"><a id="__codelineno-6-1" name="__codelineno-6-1" href="#__codelineno-6-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="p">:</span><span class="w"> </span><span class="n">TelegramBot</span><span class="p">;</span>
val fileId: FileId; </span><span id="__span-6-2"><a id="__codelineno-6-2" name="__codelineno-6-2" href="#__codelineno-6-2"></a><span class="kd">val</span><span class="w"> </span><span class="nv">fileId</span><span class="p">:</span><span class="w"> </span><span class="n">FileId</span><span class="p">;</span>
</span><span id="__span-6-3"><a id="__codelineno-6-3" name="__codelineno-6-3" href="#__codelineno-6-3"></a>
val pathedFile: PathedFile = bot.execute(GetFile(fileId)) </span><span id="__span-6-4"><a id="__codelineno-6-4" name="__codelineno-6-4" href="#__codelineno-6-4"></a><span class="kd">val</span><span class="w"> </span><span class="nv">pathedFile</span><span class="p">:</span><span class="w"> </span><span class="n">PathedFile</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bot</span><span class="p">.</span><span class="na">execute</span><span class="p">(</span><span class="n">GetFile</span><span class="p">(</span><span class="n">fileId</span><span class="p">))</span>
</span><span id="__span-6-5"><a id="__codelineno-6-5" name="__codelineno-6-5" href="#__codelineno-6-5"></a>
val downloadedBytes: ByteArray = bot.execute(DownloadFile(pathedFile.filePath)) </span><span id="__span-6-6"><a id="__codelineno-6-6" name="__codelineno-6-6" href="#__codelineno-6-6"></a><span class="kd">val</span><span class="w"> </span><span class="nv">downloadedBytes</span><span class="p">:</span><span class="w"> </span><span class="n">ByteArray</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bot</span><span class="p">.</span><span class="na">execute</span><span class="p">(</span><span class="n">DownloadFile</span><span class="p">(</span><span class="n">pathedFile</span><span class="p">.</span><span class="na">filePath</span><span class="p">))</span>
</code></pre> </span></code></pre></div>
<p>In the snippet above we are getting file <code>PathedFile</code> by its <code>FileId</code> and use it to download file bytes into memory using <code>DownloadFile</code> request.</p> <p>In the snippet above we are getting file <code>PathedFile</code> by its <code>FileId</code> and use it to download file bytes into memory using <code>DownloadFile</code> request.</p>
<p>You may use almost the same way but with byte read channel allocator:</p> <p>You may use almost the same way but with byte read channel allocator:</p>
<pre><code class="language-kotlin">val bot: TelegramBot; <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-7-1"><a id="__codelineno-7-1" name="__codelineno-7-1" href="#__codelineno-7-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="p">:</span><span class="w"> </span><span class="n">TelegramBot</span><span class="p">;</span>
val fileId: FileId; </span><span id="__span-7-2"><a id="__codelineno-7-2" name="__codelineno-7-2" href="#__codelineno-7-2"></a><span class="kd">val</span><span class="w"> </span><span class="nv">fileId</span><span class="p">:</span><span class="w"> </span><span class="n">FileId</span><span class="p">;</span>
</span><span id="__span-7-3"><a id="__codelineno-7-3" name="__codelineno-7-3" href="#__codelineno-7-3"></a>
val pathedFile: PathedFile = bot.execute(GetFile(fileId)) </span><span id="__span-7-4"><a id="__codelineno-7-4" name="__codelineno-7-4" href="#__codelineno-7-4"></a><span class="kd">val</span><span class="w"> </span><span class="nv">pathedFile</span><span class="p">:</span><span class="w"> </span><span class="n">PathedFile</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bot</span><span class="p">.</span><span class="na">execute</span><span class="p">(</span><span class="n">GetFile</span><span class="p">(</span><span class="n">fileId</span><span class="p">))</span>
</span><span id="__span-7-5"><a id="__codelineno-7-5" name="__codelineno-7-5" href="#__codelineno-7-5"></a>
val channelAllocator: ByteReadChannelAllocator = bot.execute(DownloadFileStream(pathedFile.filePath)) </span><span id="__span-7-6"><a id="__codelineno-7-6" name="__codelineno-7-6" href="#__codelineno-7-6"></a><span class="kd">val</span><span class="w"> </span><span class="nv">channelAllocator</span><span class="p">:</span><span class="w"> </span><span class="n">ByteReadChannelAllocator</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bot</span><span class="p">.</span><span class="na">execute</span><span class="p">(</span><span class="n">DownloadFileStream</span><span class="p">(</span><span class="n">pathedFile</span><span class="p">.</span><span class="na">filePath</span><span class="p">))</span>
</span><span id="__span-7-7"><a id="__codelineno-7-7" name="__codelineno-7-7" href="#__codelineno-7-7"></a>
val byteReadChannel: ByteReadChannel = channelAllocator() </span><span id="__span-7-8"><a id="__codelineno-7-8" name="__codelineno-7-8" href="#__codelineno-7-8"></a><span class="kd">val</span><span class="w"> </span><span class="nv">byteReadChannel</span><span class="p">:</span><span class="w"> </span><span class="n">ByteReadChannel</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">channelAllocator</span><span class="p">()</span>
</code></pre> </span></code></pre></div>
<p>And then you may look into <a href="https://api.ktor.io/ktor-io/io.ktor.utils.io/-byte-read-channel/index.html">ByteReadChannel</a> docs to get more info about what you can do with that.</p> <p>And then you may look into <a href="https://api.ktor.io/ktor-io/io.ktor.utils.io/-byte-read-channel/index.html">ByteReadChannel</a> docs to get more info about what you can do with that.</p>
<div class="admonition note"> <div class="admonition note">
<p class="admonition-title">Several useful links</p> <p class="admonition-title">Several useful links</p>
@ -1283,10 +1283,10 @@ Sending via file is accessible from all supported platforms, but there is small
<li>Via <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.requests.abstracts/as-multipart-file.html">asMultiparFile</a> extension applicable to any <code>ByteArray</code>, <code>ByteReadChannel</code>, <code>ByteReadChannelAllocator</code> or <code>File</code> (on any platform)</li> <li>Via <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.requests.abstracts/as-multipart-file.html">asMultiparFile</a> extension applicable to any <code>ByteArray</code>, <code>ByteReadChannel</code>, <code>ByteReadChannelAllocator</code> or <code>File</code> (on any platform)</li>
</ul> </ul>
<p>In most cases, sending via files looks like in the next snippet:</p> <p>In most cases, sending via files looks like in the next snippet:</p>
<pre><code class="language-kotlin">val file: File; <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-8-1"><a id="__codelineno-8-1" name="__codelineno-8-1" href="#__codelineno-8-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">file</span><span class="p">:</span><span class="w"> </span><span class="n">File</span><span class="p">;</span>
</span><span id="__span-8-2"><a id="__codelineno-8-2" name="__codelineno-8-2" href="#__codelineno-8-2"></a>
bot.sendDocument(chatId, file.asMultipartFile()) </span><span id="__span-8-3"><a id="__codelineno-8-3" name="__codelineno-8-3" href="#__codelineno-8-3"></a><span class="n">bot</span><span class="p">.</span><span class="na">sendDocument</span><span class="p">(</span><span class="n">chatId</span><span class="p">,</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="na">asMultipartFile</span><span class="p">())</span>
</code></pre> </span></code></pre></div>

View File

@ -1180,8 +1180,8 @@
<p>So, in most cases all your request calls with simplified api of this library (like <code>bot.getMe()</code>) will looks like <code>bot.execute(GetMe)</code>. Result of these calls is defined in type of any request (for example, for <a href="https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/GetMe.kt">GetMe</a> request the result type is <a href="https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt#L101">ExtendedBot</a>). As a result, you can avoid any extension api (like <a href="api-extensions.html">special API extensions</a>) and use low level request with full controlling of the whole logic flow.</p> <p>So, in most cases all your request calls with simplified api of this library (like <code>bot.getMe()</code>) will looks like <code>bot.execute(GetMe)</code>. Result of these calls is defined in type of any request (for example, for <a href="https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/GetMe.kt">GetMe</a> request the result type is <a href="https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt#L101">ExtendedBot</a>). As a result, you can avoid any extension api (like <a href="api-extensions.html">special API extensions</a>) and use low level request with full controlling of the whole logic flow.</p>
<h2 id="how-to-handle-updates">How to handle updates<a class="headerlink" href="#how-to-handle-updates" title="Permanent link">&para;</a></h2> <h2 id="how-to-handle-updates">How to handle updates<a class="headerlink" href="#how-to-handle-updates" title="Permanent link">&para;</a></h2>
<p>As was written above, it will require some request:</p> <p>As was written above, it will require some request:</p>
<pre><code class="language-kotlin">val updates = bot.execute(GetUpdates()) <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">updates</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bot</span><span class="p">.</span><span class="na">execute</span><span class="p">(</span><span class="n">GetUpdates</span><span class="p">())</span>
</code></pre> </span></code></pre></div>
<p>Result type of <a href="https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/GetUpdates.kt#L24">GetUpdates</a> request is <a href="https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/abstracts/Update.kt#L13">Update</a>. You may find <em>inheritors</em> of this interface in <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.types.update.abstracts/-update/index.html">Update kdocs</a>.</p> <p>Result type of <a href="https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/GetUpdates.kt#L24">GetUpdates</a> request is <a href="https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/abstracts/Update.kt#L13">Update</a>. You may find <em>inheritors</em> of this interface in <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.types.update.abstracts/-update/index.html">Update kdocs</a>.</p>
<h2 id="what-is-next">What is next?<a class="headerlink" href="#what-is-next" title="Permanent link">&para;</a></h2> <h2 id="what-is-next">What is next?<a class="headerlink" href="#what-is-next" title="Permanent link">&para;</a></h2>
<p>As was said above, you may look into our <a href="api-extensions.html">API extensions</a> in case you wish to use more high-level functions instead of <code>bot.execute(SomeRequest())</code>. Besides, it will be very useful to know more about <a href="../updates">updates retrieving</a>.</p> <p>As was said above, you may look into our <a href="api-extensions.html">API extensions</a> in case you wish to use more high-level functions instead of <code>bot.execute(SomeRequest())</code>. Besides, it will be very useful to know more about <a href="../updates">updates retrieving</a>.</p>

View File

@ -1234,21 +1234,21 @@
In tgbotapi there is no any additional handling of media groups by default and in case you will use simple <a href="https://insanusmokrassar.github.io/TelegramBotAPI/docs/dev.inmo.tgbotapi.extensions.api/get-updates.html">bot.getUpdates</a>, you will get the list of row updates and media groups will be included in this list as separated messages with <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.types.message.content/-media-group-part-content/index.html">MediaGroupPartContent</a>. In that case you may use <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.utils.updates/convert-with-media-group-updates.html">convertWithMediaGroupUpdates</a> to be able to work with media groups as will be described below</p> In tgbotapi there is no any additional handling of media groups by default and in case you will use simple <a href="https://insanusmokrassar.github.io/TelegramBotAPI/docs/dev.inmo.tgbotapi.extensions.api/get-updates.html">bot.getUpdates</a>, you will get the list of row updates and media groups will be included in this list as separated messages with <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.types.message.content/-media-group-part-content/index.html">MediaGroupPartContent</a>. In that case you may use <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.utils.updates/convert-with-media-group-updates.html">convertWithMediaGroupUpdates</a> to be able to work with media groups as will be described below</p>
</div> </div>
<p>In case you are using standard <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.utils.updates.retrieving/long-polling.html">long polling</a> (one of alternatives is <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.behaviour_builder/telegram-bot-with-behaviour-and-long-polling.html">telegramBotWithBehaviourAndLongPolling</a>) or <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.utils.updates.retrieving/set-webhook-info-and-start-listen-webhooks.html">webhooks</a> updates will be converted uner the hood and as a result, you will take media groups as a content in one message:</p> <p>In case you are using standard <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.utils.updates.retrieving/long-polling.html">long polling</a> (one of alternatives is <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.behaviour_builder/telegram-bot-with-behaviour-and-long-polling.html">telegramBotWithBehaviourAndLongPolling</a>) or <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.utils.updates.retrieving/set-webhook-info-and-start-listen-webhooks.html">webhooks</a> updates will be converted uner the hood and as a result, you will take media groups as a content in one message:</p>
<pre><code class="language-kotlin">telegramBotWithBehaviourAndLongPolling( <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="n">telegramBotWithBehaviourAndLongPolling</span><span class="p">(</span>
&quot;token&quot; </span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="s">&quot;token&quot;</span>
) { </span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="p">)</span><span class="w"> </span><span class="p">{</span>
onVisualGallery { // it: CommonMessage&lt;MediaGroupContent&lt;VisualMediaGroupPartContent&gt;&gt; </span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="w"> </span><span class="n">onVisualGallery</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// it: CommonMessage&lt;MediaGroupContent&lt;VisualMediaGroupPartContent&gt;&gt;</span>
it.content // MediaGroupContent&lt;VisualMediaGroupPartContent&gt; </span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">content</span><span class="w"> </span><span class="c1">// MediaGroupContent&lt;VisualMediaGroupPartContent&gt;</span>
it.content.group // List&lt;MediaGroupCollectionContent.PartWrapper&lt;VisualMediaGroupPartContent&gt;&gt; </span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">content</span><span class="p">.</span><span class="na">group</span><span class="w"> </span><span class="c1">// List&lt;MediaGroupCollectionContent.PartWrapper&lt;VisualMediaGroupPartContent&gt;&gt;</span>
it.content.group.forEach { // it: MediaGroupCollectionContent.PartWrapper&lt;VisualMediaGroupPartContent&gt; </span><span id="__span-0-7"><a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">content</span><span class="p">.</span><span class="na">group</span><span class="p">.</span><span class="na">forEach</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// it: MediaGroupCollectionContent.PartWrapper&lt;VisualMediaGroupPartContent&gt;</span>
it.messageId // source message id for current media group part </span><span id="__span-0-8"><a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">messageId</span><span class="w"> </span><span class="c1">// source message id for current media group part</span>
it.sourceMessage // source message for current media group part </span><span id="__span-0-9"><a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">sourceMessage</span><span class="w"> </span><span class="c1">// source message for current media group part</span>
it.content // VisualMediaGroupPartContent </span><span id="__span-0-10"><a id="__codelineno-0-10" name="__codelineno-0-10" href="#__codelineno-0-10"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">content</span><span class="w"> </span><span class="c1">// VisualMediaGroupPartContent</span>
println(it.content) // will print current content part info </span><span id="__span-0-11"><a id="__codelineno-0-11" name="__codelineno-0-11" href="#__codelineno-0-11"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">.</span><span class="na">content</span><span class="p">)</span><span class="w"> </span><span class="c1">// will print current content part info</span>
} </span><span id="__span-0-12"><a id="__codelineno-0-12" name="__codelineno-0-12" href="#__codelineno-0-12"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-0-13"><a id="__codelineno-0-13" name="__codelineno-0-13" href="#__codelineno-0-13"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-0-14"><a id="__codelineno-0-14" name="__codelineno-0-14" href="#__codelineno-0-14"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<p><strong>KDocs:</strong></p> <p><strong>KDocs:</strong></p>
<ul> <ul>
<li><a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling/on-visual-gallery-messages.html">onVisualGallery</a></li> <li><a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling/on-visual-gallery-messages.html">onVisualGallery</a></li>

View File

@ -1247,23 +1247,23 @@
<p>As you may see, it is a little bit complex and require several tools for types conversation.</p> <p>As you may see, it is a little bit complex and require several tools for types conversation.</p>
<h2 id="as">As<a class="headerlink" href="#as" title="Permanent link">&para;</a></h2> <h2 id="as">As<a class="headerlink" href="#as" title="Permanent link">&para;</a></h2>
<p><code>as</code> conversations will return new type in case if it is possible. For example, when you got <code>Message</code>, you may use <code>asContentMessage</code> conversation to get message with <code>content</code>:</p> <p><code>as</code> conversations will return new type in case if it is possible. For example, when you got <code>Message</code>, you may use <code>asContentMessage</code> conversation to get message with <code>content</code>:</p>
<pre><code class="language-kotlin">val message: Message; <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">message</span><span class="p">:</span><span class="w"> </span><span class="n">Message</span><span class="p">;</span>
println(message.asContentMessage() ?.content) </span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="n">println</span><span class="p">(</span><span class="n">message</span><span class="p">.</span><span class="na">asContentMessage</span><span class="p">()</span><span class="w"> </span><span class="o">?.</span><span class="na">content</span><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>This code will print <code>null</code> in case when <code>message</code> is not <code>ContentMessage</code>, and <code>content</code> when is.</p> <p>This code will print <code>null</code> in case when <code>message</code> is not <code>ContentMessage</code>, and <code>content</code> when is.</p>
<h2 id="require">Require<a class="headerlink" href="#require" title="Permanent link">&para;</a></h2> <h2 id="require">Require<a class="headerlink" href="#require" title="Permanent link">&para;</a></h2>
<p><code>require</code> works like <code>as</code>, but instead of returning nullable type, it will always return object with required type OR throw <code>ClassCastException</code>:</p> <p><code>require</code> works like <code>as</code>, but instead of returning nullable type, it will always return object with required type OR throw <code>ClassCastException</code>:</p>
<pre><code class="language-kotlin">val message: Message; <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">message</span><span class="p">:</span><span class="w"> </span><span class="n">Message</span><span class="p">;</span>
println(message.requireContentMessage().content) </span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a><span class="n">println</span><span class="p">(</span><span class="n">message</span><span class="p">.</span><span class="na">requireContentMessage</span><span class="p">().</span><span class="na">content</span><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>This code will throw exception when message is not <code>ContentMessage</code> and print <code>content</code> when is.</p> <p>This code will throw exception when message is not <code>ContentMessage</code> and print <code>content</code> when is.</p>
<h2 id="when">When<a class="headerlink" href="#when" title="Permanent link">&para;</a></h2> <h2 id="when">When<a class="headerlink" href="#when" title="Permanent link">&para;</a></h2>
<p><code>when</code> extensions will call passed <code>block</code> when type is correct. For example:</p> <p><code>when</code> extensions will call passed <code>block</code> when type is correct. For example:</p>
<pre><code class="language-kotlin">val message: Message; <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">message</span><span class="p">:</span><span class="w"> </span><span class="n">Message</span><span class="p">;</span>
message.whenContentMessage { </span><span id="__span-2-2"><a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a><span class="n">message</span><span class="p">.</span><span class="na">whenContentMessage</span><span class="w"> </span><span class="p">{</span>
println(it.content) </span><span id="__span-2-3"><a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">.</span><span class="na">content</span><span class="p">)</span>
} </span><span id="__span-2-4"><a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<p>Code placed above will print <code>content</code> when <code>message</code> is <code>ContentMessage</code> and do nothing when not</p> <p>Code placed above will print <code>content</code> when <code>message</code> is <code>ContentMessage</code> and do nothing when not</p>

View File

@ -1252,29 +1252,29 @@
<li>Provide updates to this filter with <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.updateshandlers/-updates-filter/as-update-receiver.html">filter#asUpdateReceiver</a> object</li> <li>Provide updates to this filter with <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.updateshandlers/-updates-filter/as-update-receiver.html">filter#asUpdateReceiver</a> object</li>
</ul> </ul>
<p>Let&rsquo;s look how it works with the factory above:</p> <p>Let&rsquo;s look how it works with the factory above:</p>
<pre><code class="language-kotlin">// Step 1 - create filter <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="c1">// Step 1 - create filter</span>
val filter = flowsUpdatesFilter { </span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="kd">val</span><span class="w"> </span><span class="nv">filter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">flowsUpdatesFilter</span><span class="w"> </span><span class="p">{</span>
// Step 2 - set up handling. In this case we will print any message from group or user in console </span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="w"> </span><span class="c1">// Step 2 - set up handling. In this case we will print any message from group or user in console</span>
messageFlow.onEach { </span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="w"> </span><span class="n">messageFlow</span><span class="p">.</span><span class="na">onEach</span><span class="w"> </span><span class="p">{</span>
println(it) </span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span>
}.launchIn(someCoroutineScope) </span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a><span class="w"> </span><span class="p">}.</span><span class="na">launchIn</span><span class="p">(</span><span class="n">someCoroutineScope</span><span class="p">)</span>
} </span><span id="__span-0-7"><a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="p">}</span>
</span><span id="__span-0-8"><a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a>
// Step 3 - passing updates to filter </span><span id="__span-0-9"><a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a><span class="c1">// Step 3 - passing updates to filter</span>
bot.getUpdates().forEach { </span><span id="__span-0-10"><a id="__codelineno-0-10" name="__codelineno-0-10" href="#__codelineno-0-10"></a><span class="n">bot</span><span class="p">.</span><span class="na">getUpdates</span><span class="p">().</span><span class="na">forEach</span><span class="w"> </span><span class="p">{</span>
filter.asUpdatesReceiver(it) </span><span id="__span-0-11"><a id="__codelineno-0-11" name="__codelineno-0-11" href="#__codelineno-0-11"></a><span class="w"> </span><span class="n">filter</span><span class="p">.</span><span class="na">asUpdatesReceiver</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span>
} </span><span id="__span-0-12"><a id="__codelineno-0-12" name="__codelineno-0-12" href="#__codelineno-0-12"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<h2 id="long-polling">Long polling<a class="headerlink" href="#long-polling" title="Permanent link">&para;</a></h2> <h2 id="long-polling">Long polling<a class="headerlink" href="#long-polling" title="Permanent link">&para;</a></h2>
<p>Some example with long polling has been described above. But it is more useful to use some factories for it. In this page we will look for simple variant with <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.utils.updates.retrieving/long-polling.html">TelegramBot#longPolling</a>. So, with this function, your handling of updates will looks like:</p> <p>Some example with long polling has been described above. But it is more useful to use some factories for it. In this page we will look for simple variant with <a href="https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.extensions.utils.updates.retrieving/long-polling.html">TelegramBot#longPolling</a>. So, with this function, your handling of updates will looks like:</p>
<pre><code class="language-kotlin">val bot = telegramBot(&quot;TOKEN&quot;) <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">telegramBot</span><span class="p">(</span><span class="s">&quot;TOKEN&quot;</span><span class="p">)</span>
</span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a>
bot.longPolling { </span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="n">bot</span><span class="p">.</span><span class="na">longPolling</span><span class="w"> </span><span class="p">{</span>
messageFlow.onEach { </span><span id="__span-1-4"><a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="w"> </span><span class="n">messageFlow</span><span class="p">.</span><span class="na">onEach</span><span class="w"> </span><span class="p">{</span>
println(it) </span><span id="__span-1-5"><a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span>
}.launchIn(someCoroutineScope) </span><span id="__span-1-6"><a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a><span class="w"> </span><span class="p">}.</span><span class="na">launchIn</span><span class="p">(</span><span class="n">someCoroutineScope</span><span class="p">)</span>
}.join() </span><span id="__span-1-7"><a id="__codelineno-1-7" name="__codelineno-1-7" href="#__codelineno-1-7"></a><span class="p">}.</span><span class="na">join</span><span class="p">()</span>
</code></pre> </span></code></pre></div>
<p>This example looks like the example above with three steps, but there are several important things here:</p> <p>This example looks like the example above with three steps, but there are several important things here:</p>
<ul> <ul>
<li>You <strong>do not</strong> manage retrieving of updates by hands</li> <li>You <strong>do not</strong> manage retrieving of updates by hands</li>

View File

@ -1245,85 +1245,85 @@
Here will be presented variants of configuration of webhooks and starting server. You always able to set webhook manualy, create your own ktor server and include webhooks handling in it or create and start server with only webhooks handling. More info you can get on page <a href="webhooks.html">Webhooks</a></p> Here will be presented variants of configuration of webhooks and starting server. You always able to set webhook manualy, create your own ktor server and include webhooks handling in it or create and start server with only webhooks handling. More info you can get on page <a href="webhooks.html">Webhooks</a></p>
</div> </div>
<h3 id="short-example-with-behaviour-builder">Short example with Behaviour Builder<a class="headerlink" href="#short-example-with-behaviour-builder" title="Permanent link">&para;</a></h3> <h3 id="short-example-with-behaviour-builder">Short example with Behaviour Builder<a class="headerlink" href="#short-example-with-behaviour-builder" title="Permanent link">&para;</a></h3>
<pre><code class="language-kotlin">suspend fun main { <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="kd">suspend</span><span class="w"> </span><span class="kd">fun</span><span class="w"> </span><span class="nf">main</span><span class="w"> </span><span class="p">{</span>
// This subroute will be used as random webhook subroute to improve security according to the recommendations of Telegram </span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="c1">// This subroute will be used as random webhook subroute to improve security according to the recommendations of Telegram</span>
val subroute = uuid4().toString() </span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">subroute</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">uuid4</span><span class="p">().</span><span class="na">toString</span><span class="p">()</span>
// Input/Output coroutines scope more info here: https://kotlinlang.org/docs/coroutines-guide.html </span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="w"> </span><span class="c1">// Input/Output coroutines scope more info here: https://kotlinlang.org/docs/coroutines-guide.html</span>
val scope = CoroutineScope(Dispatchers.IO) </span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="nv">scope</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">CoroutineScope</span><span class="p">(</span><span class="n">Dispatchers</span><span class="p">.</span><span class="na">IO</span><span class="p">)</span>
// Here will be automatically created bot and available inside of lambda where you will setup your bot behaviour </span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a><span class="w"> </span><span class="c1">// Here will be automatically created bot and available inside of lambda where you will setup your bot behaviour</span>
telegramBotWithBehaviour( </span><span id="__span-0-7"><a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="w"> </span><span class="n">telegramBotWithBehaviour</span><span class="p">(</span>
// Pass TOKEN inside of your application environment variables </span><span id="__span-0-8"><a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="w"> </span><span class="c1">// Pass TOKEN inside of your application environment variables</span>
System.getenv(&quot;TOKEN&quot;), </span><span id="__span-0-9"><a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">getenv</span><span class="p">(</span><span class="s">&quot;TOKEN&quot;</span><span class="p">),</span>
scope = scope </span><span id="__span-0-10"><a id="__codelineno-0-10" name="__codelineno-0-10" href="#__codelineno-0-10"></a><span class="w"> </span><span class="n">scope</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">scope</span>
) { </span><span id="__span-0-11"><a id="__codelineno-0-11" name="__codelineno-0-11" href="#__codelineno-0-11"></a><span class="w"> </span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
// Set up webhooks and start to listen them </span><span id="__span-0-12"><a id="__codelineno-0-12" name="__codelineno-0-12" href="#__codelineno-0-12"></a><span class="w"> </span><span class="c1">// Set up webhooks and start to listen them</span>
setWebhookInfoAndStartListenWebhooks( </span><span id="__span-0-13"><a id="__codelineno-0-13" name="__codelineno-0-13" href="#__codelineno-0-13"></a><span class="w"> </span><span class="n">setWebhookInfoAndStartListenWebhooks</span><span class="p">(</span>
// Automatic env which will be passed by heroku to the app </span><span id="__span-0-14"><a id="__codelineno-0-14" name="__codelineno-0-14" href="#__codelineno-0-14"></a><span class="w"> </span><span class="c1">// Automatic env which will be passed by heroku to the app</span>
System.getenv(&quot;PORT&quot;).toInt(), </span><span id="__span-0-15"><a id="__codelineno-0-15" name="__codelineno-0-15" href="#__codelineno-0-15"></a><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">getenv</span><span class="p">(</span><span class="s">&quot;PORT&quot;</span><span class="p">).</span><span class="na">toInt</span><span class="p">(),</span>
// Server engine. More info here: https://ktor.io/docs/engines.html </span><span id="__span-0-16"><a id="__codelineno-0-16" name="__codelineno-0-16" href="#__codelineno-0-16"></a><span class="w"> </span><span class="c1">// Server engine. More info here: https://ktor.io/docs/engines.html</span>
Tomcat, </span><span id="__span-0-17"><a id="__codelineno-0-17" name="__codelineno-0-17" href="#__codelineno-0-17"></a><span class="w"> </span><span class="n">Tomcat</span><span class="p">,</span>
// Pass URL environment variable via settings of application. It must looks like https://&lt;app name&gt;.herokuapp.com </span><span id="__span-0-18"><a id="__codelineno-0-18" name="__codelineno-0-18" href="#__codelineno-0-18"></a><span class="w"> </span><span class="c1">// Pass URL environment variable via settings of application. It must looks like https://&lt;app name&gt;.herokuapp.com</span>
SetWebhook(&quot;${System.getenv(&quot;URL&quot;).removeSuffix(&quot;/&quot;)}/$subroute&quot;), </span><span id="__span-0-19"><a id="__codelineno-0-19" name="__codelineno-0-19" href="#__codelineno-0-19"></a><span class="w"> </span><span class="n">SetWebhook</span><span class="p">(</span><span class="s">&quot;</span><span class="si">${</span><span class="n">System</span><span class="p">.</span><span class="na">getenv</span><span class="p">(</span><span class="s">&quot;</span><span class="n">URL</span><span class="s">&quot;</span><span class="p">).</span><span class="na">removeSuffix</span><span class="p">(</span><span class="s">&quot;</span><span class="o">/</span><span class="s">&quot;</span><span class="p">)</span><span class="si">}</span><span class="s">/</span><span class="si">$</span><span class="n">subroute</span><span class="s">&quot;</span><span class="p">),</span>
// Just callback which will be called when exceptions will happen inside of webhooks </span><span id="__span-0-20"><a id="__codelineno-0-20" name="__codelineno-0-20" href="#__codelineno-0-20"></a><span class="w"> </span><span class="c1">// Just callback which will be called when exceptions will happen inside of webhooks</span>
{ </span><span id="__span-0-21"><a id="__codelineno-0-21" name="__codelineno-0-21" href="#__codelineno-0-21"></a><span class="w"> </span><span class="p">{</span>
it.printStackTrace() </span><span id="__span-0-22"><a id="__codelineno-0-22" name="__codelineno-0-22" href="#__codelineno-0-22"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">printStackTrace</span><span class="p">()</span>
}, </span><span id="__span-0-23"><a id="__codelineno-0-23" name="__codelineno-0-23" href="#__codelineno-0-23"></a><span class="w"> </span><span class="p">},</span>
// Set up listen requests from outside </span><span id="__span-0-24"><a id="__codelineno-0-24" name="__codelineno-0-24" href="#__codelineno-0-24"></a><span class="w"> </span><span class="c1">// Set up listen requests from outside</span>
&quot;0.0.0.0&quot;, </span><span id="__span-0-25"><a id="__codelineno-0-25" name="__codelineno-0-25" href="#__codelineno-0-25"></a><span class="w"> </span><span class="s">&quot;0.0.0.0&quot;</span><span class="p">,</span>
// Set up subroute to listen webhooks to </span><span id="__span-0-26"><a id="__codelineno-0-26" name="__codelineno-0-26" href="#__codelineno-0-26"></a><span class="w"> </span><span class="c1">// Set up subroute to listen webhooks to</span>
subroute, </span><span id="__span-0-27"><a id="__codelineno-0-27" name="__codelineno-0-27" href="#__codelineno-0-27"></a><span class="w"> </span><span class="n">subroute</span><span class="p">,</span>
// BehaviourContext is the CoroutineScope and it is recommended to pass it inside of webhooks server </span><span id="__span-0-28"><a id="__codelineno-0-28" name="__codelineno-0-28" href="#__codelineno-0-28"></a><span class="w"> </span><span class="c1">// BehaviourContext is the CoroutineScope and it is recommended to pass it inside of webhooks server</span>
scope = this, </span><span id="__span-0-29"><a id="__codelineno-0-29" name="__codelineno-0-29" href="#__codelineno-0-29"></a><span class="w"> </span><span class="n">scope</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">this</span><span class="p">,</span>
// BehaviourContext is the FlowsUpdatesFilter and it is recommended to pass its asUpdateReceiver as a block to retrieve all the updates </span><span id="__span-0-30"><a id="__codelineno-0-30" name="__codelineno-0-30" href="#__codelineno-0-30"></a><span class="w"> </span><span class="c1">// BehaviourContext is the FlowsUpdatesFilter and it is recommended to pass its asUpdateReceiver as a block to retrieve all the updates</span>
block = asUpdateReceiver </span><span id="__span-0-31"><a id="__codelineno-0-31" name="__codelineno-0-31" href="#__codelineno-0-31"></a><span class="w"> </span><span class="n">block</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">asUpdateReceiver</span>
) </span><span id="__span-0-32"><a id="__codelineno-0-32" name="__codelineno-0-32" href="#__codelineno-0-32"></a><span class="w"> </span><span class="p">)</span>
// Test reaction on each command with reply and text `Got it` </span><span id="__span-0-33"><a id="__codelineno-0-33" name="__codelineno-0-33" href="#__codelineno-0-33"></a><span class="w"> </span><span class="c1">// Test reaction on each command with reply and text `Got it`</span>
onUnhandledCommand { </span><span id="__span-0-34"><a id="__codelineno-0-34" name="__codelineno-0-34" href="#__codelineno-0-34"></a><span class="w"> </span><span class="n">onUnhandledCommand</span><span class="w"> </span><span class="p">{</span>
reply(it, &quot;Got it&quot;) </span><span id="__span-0-35"><a id="__codelineno-0-35" name="__codelineno-0-35" href="#__codelineno-0-35"></a><span class="w"> </span><span class="n">reply</span><span class="p">(</span><span class="nb">it</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Got it&quot;</span><span class="p">)</span>
} </span><span id="__span-0-36"><a id="__codelineno-0-36" name="__codelineno-0-36" href="#__codelineno-0-36"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-0-37"><a id="__codelineno-0-37" name="__codelineno-0-37" href="#__codelineno-0-37"></a><span class="w"> </span><span class="p">}</span>
// Just potentially infinite await of bot completion </span><span id="__span-0-38"><a id="__codelineno-0-38" name="__codelineno-0-38" href="#__codelineno-0-38"></a><span class="w"> </span><span class="c1">// Just potentially infinite await of bot completion</span>
scope.coroutineContext.job.join() </span><span id="__span-0-39"><a id="__codelineno-0-39" name="__codelineno-0-39" href="#__codelineno-0-39"></a><span class="w"> </span><span class="n">scope</span><span class="p">.</span><span class="na">coroutineContext</span><span class="p">.</span><span class="na">job</span><span class="p">.</span><span class="na">join</span><span class="p">()</span>
} </span><span id="__span-0-40"><a id="__codelineno-0-40" name="__codelineno-0-40" href="#__codelineno-0-40"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<h3 id="configuration-example-without-behaviour-builder">Configuration example without Behaviour Builder<a class="headerlink" href="#configuration-example-without-behaviour-builder" title="Permanent link">&para;</a></h3> <h3 id="configuration-example-without-behaviour-builder">Configuration example without Behaviour Builder<a class="headerlink" href="#configuration-example-without-behaviour-builder" title="Permanent link">&para;</a></h3>
<pre><code class="language-kotlin">// This subroute will be used as random webhook subroute to improve security according to the recommendations of Telegram <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="c1">// This subroute will be used as random webhook subroute to improve security according to the recommendations of Telegram</span>
val subroute = uuid4().toString() </span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a><span class="kd">val</span><span class="w"> </span><span class="nv">subroute</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">uuid4</span><span class="p">().</span><span class="na">toString</span><span class="p">()</span>
val bot = telegramBot(TOKEN) </span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">telegramBot</span><span class="p">(</span><span class="n">TOKEN</span><span class="p">)</span>
val scope = CoroutineScope(Dispatchers.Default) </span><span id="__span-1-4"><a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="kd">val</span><span class="w"> </span><span class="nv">scope</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">CoroutineScope</span><span class="p">(</span><span class="n">Dispatchers</span><span class="p">.</span><span class="na">Default</span><span class="p">)</span>
</span><span id="__span-1-5"><a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a>
val filter = flowsUpdatesFilter { </span><span id="__span-1-6"><a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a><span class="kd">val</span><span class="w"> </span><span class="nv">filter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">flowsUpdatesFilter</span><span class="w"> </span><span class="p">{</span>
messageFlow.onEach { </span><span id="__span-1-7"><a id="__codelineno-1-7" name="__codelineno-1-7" href="#__codelineno-1-7"></a><span class="w"> </span><span class="n">messageFlow</span><span class="p">.</span><span class="na">onEach</span><span class="w"> </span><span class="p">{</span>
println(it) // will be printed </span><span id="__span-1-8"><a id="__codelineno-1-8" name="__codelineno-1-8" href="#__codelineno-1-8"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span><span class="w"> </span><span class="c1">// will be printed </span>
}.launchIn(scope) </span><span id="__span-1-9"><a id="__codelineno-1-9" name="__codelineno-1-9" href="#__codelineno-1-9"></a><span class="w"> </span><span class="p">}.</span><span class="na">launchIn</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span>
} </span><span id="__span-1-10"><a id="__codelineno-1-10" name="__codelineno-1-10" href="#__codelineno-1-10"></a><span class="p">}</span>
</span><span id="__span-1-11"><a id="__codelineno-1-11" name="__codelineno-1-11" href="#__codelineno-1-11"></a>
val subroute = UUID.randomUUID().toString() // It will be used as subpath for security target as recommended by https://core.telegram.org/bots/api#setwebhook </span><span id="__span-1-12"><a id="__codelineno-1-12" name="__codelineno-1-12" href="#__codelineno-1-12"></a><span class="kd">val</span><span class="w"> </span><span class="nv">subroute</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">UUID</span><span class="p">.</span><span class="na">randomUUID</span><span class="p">().</span><span class="na">toString</span><span class="p">()</span><span class="w"> </span><span class="c1">// It will be used as subpath for security target as recommended by https://core.telegram.org/bots/api#setwebhook</span>
</span><span id="__span-1-13"><a id="__codelineno-1-13" name="__codelineno-1-13" href="#__codelineno-1-13"></a>
val server = bot.setWebhookInfoAndStartListenWebhooks( </span><span id="__span-1-14"><a id="__codelineno-1-14" name="__codelineno-1-14" href="#__codelineno-1-14"></a><span class="kd">val</span><span class="w"> </span><span class="nv">server</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bot</span><span class="p">.</span><span class="na">setWebhookInfoAndStartListenWebhooks</span><span class="p">(</span>
// Automatic env which will be passed by heroku to the app </span><span id="__span-1-15"><a id="__codelineno-1-15" name="__codelineno-1-15" href="#__codelineno-1-15"></a><span class="w"> </span><span class="c1">// Automatic env which will be passed by heroku to the app</span>
System.getenv(&quot;PORT&quot;).toInt(), </span><span id="__span-1-16"><a id="__codelineno-1-16" name="__codelineno-1-16" href="#__codelineno-1-16"></a><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">getenv</span><span class="p">(</span><span class="s">&quot;PORT&quot;</span><span class="p">).</span><span class="na">toInt</span><span class="p">(),</span>
// Server engine. More info here: https://ktor.io/docs/engines.html </span><span id="__span-1-17"><a id="__codelineno-1-17" name="__codelineno-1-17" href="#__codelineno-1-17"></a><span class="w"> </span><span class="c1">// Server engine. More info here: https://ktor.io/docs/engines.html</span>
Tomcat, </span><span id="__span-1-18"><a id="__codelineno-1-18" name="__codelineno-1-18" href="#__codelineno-1-18"></a><span class="w"> </span><span class="n">Tomcat</span><span class="p">,</span>
// Pass URL environment variable via settings of application. It must looks like https://&lt;app name&gt;.herokuapp.com </span><span id="__span-1-19"><a id="__codelineno-1-19" name="__codelineno-1-19" href="#__codelineno-1-19"></a><span class="w"> </span><span class="c1">// Pass URL environment variable via settings of application. It must looks like https://&lt;app name&gt;.herokuapp.com</span>
SetWebhook(&quot;${System.getenv(&quot;URL&quot;).removeSuffix(&quot;/&quot;)}/$subroute&quot;), </span><span id="__span-1-20"><a id="__codelineno-1-20" name="__codelineno-1-20" href="#__codelineno-1-20"></a><span class="w"> </span><span class="n">SetWebhook</span><span class="p">(</span><span class="s">&quot;</span><span class="si">${</span><span class="n">System</span><span class="p">.</span><span class="na">getenv</span><span class="p">(</span><span class="s">&quot;</span><span class="n">URL</span><span class="s">&quot;</span><span class="p">).</span><span class="na">removeSuffix</span><span class="p">(</span><span class="s">&quot;</span><span class="o">/</span><span class="s">&quot;</span><span class="p">)</span><span class="si">}</span><span class="s">/</span><span class="si">$</span><span class="n">subroute</span><span class="s">&quot;</span><span class="p">),</span>
// Just callback which will be called when exceptions will happen inside of webhooks </span><span id="__span-1-21"><a id="__codelineno-1-21" name="__codelineno-1-21" href="#__codelineno-1-21"></a><span class="w"> </span><span class="c1">// Just callback which will be called when exceptions will happen inside of webhooks</span>
{ </span><span id="__span-1-22"><a id="__codelineno-1-22" name="__codelineno-1-22" href="#__codelineno-1-22"></a><span class="w"> </span><span class="p">{</span>
it.printStackTrace() </span><span id="__span-1-23"><a id="__codelineno-1-23" name="__codelineno-1-23" href="#__codelineno-1-23"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">printStackTrace</span><span class="p">()</span>
}, </span><span id="__span-1-24"><a id="__codelineno-1-24" name="__codelineno-1-24" href="#__codelineno-1-24"></a><span class="w"> </span><span class="p">},</span>
// Set up listen requests from outside </span><span id="__span-1-25"><a id="__codelineno-1-25" name="__codelineno-1-25" href="#__codelineno-1-25"></a><span class="w"> </span><span class="c1">// Set up listen requests from outside</span>
&quot;0.0.0.0&quot;, </span><span id="__span-1-26"><a id="__codelineno-1-26" name="__codelineno-1-26" href="#__codelineno-1-26"></a><span class="w"> </span><span class="s">&quot;0.0.0.0&quot;</span><span class="p">,</span>
// Set up subroute to listen webhooks to </span><span id="__span-1-27"><a id="__codelineno-1-27" name="__codelineno-1-27" href="#__codelineno-1-27"></a><span class="w"> </span><span class="c1">// Set up subroute to listen webhooks to</span>
subroute, </span><span id="__span-1-28"><a id="__codelineno-1-28" name="__codelineno-1-28" href="#__codelineno-1-28"></a><span class="w"> </span><span class="n">subroute</span><span class="p">,</span>
scope = scope, </span><span id="__span-1-29"><a id="__codelineno-1-29" name="__codelineno-1-29" href="#__codelineno-1-29"></a><span class="w"> </span><span class="n">scope</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">scope</span><span class="p">,</span>
block = filter.asUpdateReceiver </span><span id="__span-1-30"><a id="__codelineno-1-30" name="__codelineno-1-30" href="#__codelineno-1-30"></a><span class="w"> </span><span class="n">block</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">filter</span><span class="p">.</span><span class="na">asUpdateReceiver</span>
) </span><span id="__span-1-31"><a id="__codelineno-1-31" name="__codelineno-1-31" href="#__codelineno-1-31"></a><span class="p">)</span>
</span><span id="__span-1-32"><a id="__codelineno-1-32" name="__codelineno-1-32" href="#__codelineno-1-32"></a>
server.environment.connectors.forEach { </span><span id="__span-1-33"><a id="__codelineno-1-33" name="__codelineno-1-33" href="#__codelineno-1-33"></a><span class="n">server</span><span class="p">.</span><span class="na">environment</span><span class="p">.</span><span class="na">connectors</span><span class="p">.</span><span class="na">forEach</span><span class="w"> </span><span class="p">{</span>
println(it) </span><span id="__span-1-34"><a id="__codelineno-1-34" name="__codelineno-1-34" href="#__codelineno-1-34"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span>
} </span><span id="__span-1-35"><a id="__codelineno-1-35" name="__codelineno-1-35" href="#__codelineno-1-35"></a><span class="p">}</span>
server.start(false) </span><span id="__span-1-36"><a id="__codelineno-1-36" name="__codelineno-1-36" href="#__codelineno-1-36"></a><span class="n">server</span><span class="p">.</span><span class="na">start</span><span class="p">(</span><span class="kc">false</span><span class="p">)</span>
</code></pre> </span></code></pre></div>

View File

@ -1305,28 +1305,28 @@
</ul> </ul>
<h3 id="longpolling">longPolling<a class="headerlink" href="#longpolling" title="Permanent link">&para;</a></h3> <h3 id="longpolling">longPolling<a class="headerlink" href="#longpolling" title="Permanent link">&para;</a></h3>
<p><code>longPolling</code> is a simple way to start getting updates and work with bot:</p> <p><code>longPolling</code> is a simple way to start getting updates and work with bot:</p>
<pre><code class="language-kotlin">val bot = telegramBot(token) <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">telegramBot</span><span class="p">(</span><span class="n">token</span><span class="p">)</span>
bot.longPolling( </span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="n">bot</span><span class="p">.</span><span class="na">longPolling</span><span class="p">(</span>
textMessages().subscribe(scope) { // here &quot;scope&quot; is a CoroutineScope </span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="w"> </span><span class="n">textMessages</span><span class="p">().</span><span class="na">subscribe</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// here &quot;scope&quot; is a CoroutineScope</span>
println(it) // will be printed each update from chats with messages </span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span><span class="w"> </span><span class="c1">// will be printed each update from chats with messages</span>
} </span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="w"> </span><span class="p">}</span>
) </span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a><span class="p">)</span>
</code></pre> </span></code></pre></div>
<h3 id="startgettingofupdatesbylongpolling">startGettingOfUpdatesByLongPolling<a class="headerlink" href="#startgettingofupdatesbylongpolling" title="Permanent link">&para;</a></h3> <h3 id="startgettingofupdatesbylongpolling">startGettingOfUpdatesByLongPolling<a class="headerlink" href="#startgettingofupdatesbylongpolling" title="Permanent link">&para;</a></h3>
<p>The main aim of <code>startGettingOfUpdatesByLongPolling</code> extension was to provide more simple way to get updates in automatic mode:</p> <p>The main aim of <code>startGettingOfUpdatesByLongPolling</code> extension was to provide more simple way to get updates in automatic mode:</p>
<pre><code class="language-kotlin">val bot = telegramBot(token) <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">telegramBot</span><span class="p">(</span><span class="n">token</span><span class="p">)</span>
bot.startGettingOfUpdatesByLongPolling( </span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a><span class="n">bot</span><span class="p">.</span><span class="na">startGettingOfUpdatesByLongPolling</span><span class="p">(</span>
{ </span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="w"> </span><span class="p">{</span>
println(it) // will be printed each update from chats with messages </span><span id="__span-1-4"><a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span><span class="w"> </span><span class="c1">// will be printed each update from chats with messages</span>
} </span><span id="__span-1-5"><a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="w"> </span><span class="p">}</span>
) </span><span id="__span-1-6"><a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>The other way is to use the most basic <code>startGettingOfUpdatesByLongPolling</code> extension:</p> <p>The other way is to use the most basic <code>startGettingOfUpdatesByLongPolling</code> extension:</p>
<pre><code class="language-kotlin">val bot = telegramBot(token) <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">telegramBot</span><span class="p">(</span><span class="n">token</span><span class="p">)</span>
bot.startGettingOfUpdatesByLongPolling { </span><span id="__span-2-2"><a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a><span class="n">bot</span><span class="p">.</span><span class="na">startGettingOfUpdatesByLongPolling</span><span class="w"> </span><span class="p">{</span>
println(it) // will be printed each update </span><span id="__span-2-3"><a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span><span class="w"> </span><span class="c1">// will be printed each update</span>
} </span><span id="__span-2-4"><a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<h2 id="see-also">See also<a class="headerlink" href="#see-also" title="Permanent link">&para;</a></h2> <h2 id="see-also">See also<a class="headerlink" href="#see-also" title="Permanent link">&para;</a></h2>
<ul> <ul>
<li><a href="webhooks.html">Webhooks</a></li> <li><a href="webhooks.html">Webhooks</a></li>

View File

@ -1319,90 +1319,86 @@
<p>Anyway, this filter can&rsquo;t work with updates by itself. For retrieving updates you should pass this filter to some of getting updates functions (<a href="long-polling">long polling</a> or <a href="webhooks.html">webhooks</a>).</p> <p>Anyway, this filter can&rsquo;t work with updates by itself. For retrieving updates you should pass this filter to some of getting updates functions (<a href="long-polling">long polling</a> or <a href="webhooks.html">webhooks</a>).</p>
<h3 id="simpleupdatesfilter">SimpleUpdatesFilter<a class="headerlink" href="#simpleupdatesfilter" title="Permanent link">&para;</a></h3> <h3 id="simpleupdatesfilter">SimpleUpdatesFilter<a class="headerlink" href="#simpleupdatesfilter" title="Permanent link">&para;</a></h3>
<p><code>SimpleUpdatesFilter</code> is a simple variant of filters. It have a lot of <code>UpdateReceiver</code> properties which can be set up on creating of this object. For example, if you wish to get messages from chats (but not from channels), you can use next snippet:</p> <p><code>SimpleUpdatesFilter</code> is a simple variant of filters. It have a lot of <code>UpdateReceiver</code> properties which can be set up on creating of this object. For example, if you wish to get messages from chats (but not from channels), you can use next snippet:</p>
<pre><code class="language-kotlin">SimpleUpdatesFilter { <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="n">SimpleUpdatesFilter</span><span class="w"> </span><span class="p">{</span>
println(it) </span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span>
} </span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<h3 id="flowsupdatesfilter">FlowsUpdatesFilter<a class="headerlink" href="#flowsupdatesfilter" title="Permanent link">&para;</a></h3> <h3 id="flowsupdatesfilter">FlowsUpdatesFilter<a class="headerlink" href="#flowsupdatesfilter" title="Permanent link">&para;</a></h3>
<p>A little bit more modern way is to use <code>FlowsUpdatesFilter</code>. It is very powerfull API of Kotlin Coroutines Flows, built-in support of additional extensions for <code>FlowsUpdatesFilter</code> and <code>Flow&lt;...&gt;</code> receivers and opportunity to split one filter for as much receivers as you want. Filter creating example:</p> <p>A little bit more modern way is to use <code>FlowsUpdatesFilter</code>. It is very powerfull API of Kotlin Coroutines Flows, built-in support of additional extensions for <code>FlowsUpdatesFilter</code> and <code>Flow&lt;...&gt;</code> receivers and opportunity to split one filter for as much receivers as you want. Filter creating example:</p>
<pre><code class="language-kotlin">val scope = CoroutineScope(Dispatchers.Default) <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">scope</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">CoroutineScope</span><span class="p">(</span><span class="n">Dispatchers</span><span class="p">.</span><span class="na">Default</span><span class="p">)</span>
flowsUpdatesFilter { </span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a><span class="n">flowsUpdatesFilter</span><span class="w"> </span><span class="p">{</span>
messageFlow.onEach { </span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="w"> </span><span class="n">messageFlow</span><span class="p">.</span><span class="na">onEach</span><span class="w"> </span><span class="p">{</span>
println(it) </span><span id="__span-1-4"><a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span>
}.launchIn(scope) </span><span id="__span-1-5"><a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="w"> </span><span class="p">}.</span><span class="na">launchIn</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span>
} </span><span id="__span-1-6"><a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<h4 id="combining-of-flows">Combining of flows<a class="headerlink" href="#combining-of-flows" title="Permanent link">&para;</a></h4> <h4 id="combining-of-flows">Combining of flows<a class="headerlink" href="#combining-of-flows" title="Permanent link">&para;</a></h4>
<p>In cases you need not separate logic for handling of messages from channels and chats there are three ways to combine different flows into one:</p> <p>In cases you need not separate logic for handling of messages from channels and chats there are three ways to combine different flows into one:</p>
<ul> <ul>
<li>Standard <code>plus</code> operation and handling of different flows:</li> <li>Standard <code>plus</code> operation and handling of different flows:
<div class="language-kotlin highlight"><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="n">flowsUpdatesFilter</span><span class="w"> </span><span class="p">{</span>
</span><span id="__span-2-2"><a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a><span class="w"> </span><span class="p">(</span><span class="n">messageFlow</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">channelPostFlow</span><span class="p">).</span><span class="na">onEach</span><span class="w"> </span><span class="p">{</span>
</span><span id="__span-2-3"><a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span><span class="w"> </span><span class="c1">// will be printed each message update from channels and chats both</span>
</span><span id="__span-2-4"><a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a><span class="w"> </span><span class="p">}.</span><span class="na">launchIn</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span>
</span><span id="__span-2-5"><a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a><span class="p">}</span>
</span></code></pre></div></li>
<li>TelegramBotAPI library support function <code>aggregateFlows</code>:
<div class="language-kotlin highlight"><pre><span></span><code><span id="__span-3-1"><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a><span class="n">flowsUpdatesFilter</span><span class="w"> </span><span class="p">{</span>
</span><span id="__span-3-2"><a id="__codelineno-3-2" name="__codelineno-3-2" href="#__codelineno-3-2"></a><span class="w"> </span><span class="n">aggregateFlows</span><span class="p">(</span>
</span><span id="__span-3-3"><a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a><span class="w"> </span><span class="n">scope</span><span class="p">,</span>
</span><span id="__span-3-4"><a id="__codelineno-3-4" name="__codelineno-3-4" href="#__codelineno-3-4"></a><span class="w"> </span><span class="n">messageFlow</span><span class="p">,</span>
</span><span id="__span-3-5"><a id="__codelineno-3-5" name="__codelineno-3-5" href="#__codelineno-3-5"></a><span class="w"> </span><span class="n">channelPostFlow</span>
</span><span id="__span-3-6"><a id="__codelineno-3-6" name="__codelineno-3-6" href="#__codelineno-3-6"></a><span class="w"> </span><span class="p">).</span><span class="na">onEach</span><span class="w"> </span><span class="p">{</span>
</span><span id="__span-3-7"><a id="__codelineno-3-7" name="__codelineno-3-7" href="#__codelineno-3-7"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span><span class="w"> </span><span class="c1">// will be printed each message update from channels and chats both</span>
</span><span id="__span-3-8"><a id="__codelineno-3-8" name="__codelineno-3-8" href="#__codelineno-3-8"></a><span class="w"> </span><span class="p">}.</span><span class="na">launchIn</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span>
</span><span id="__span-3-9"><a id="__codelineno-3-9" name="__codelineno-3-9" href="#__codelineno-3-9"></a><span class="p">}</span>
</span></code></pre></div></li>
<li><code>FlowsUpdatesFilter</code> extensions:
<div class="language-kotlin highlight"><pre><span></span><code><span id="__span-4-1"><a id="__codelineno-4-1" name="__codelineno-4-1" href="#__codelineno-4-1"></a><span class="n">flowsUpdatesFilter</span><span class="w"> </span><span class="p">{</span>
</span><span id="__span-4-2"><a id="__codelineno-4-2" name="__codelineno-4-2" href="#__codelineno-4-2"></a><span class="w"> </span><span class="n">allSentMessagesFlow</span><span class="p">.</span><span class="na">onEach</span><span class="w"> </span><span class="p">{</span>
</span><span id="__span-4-3"><a id="__codelineno-4-3" name="__codelineno-4-3" href="#__codelineno-4-3"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span><span class="w"> </span><span class="c1">// will be printed each message update from channels and chats both</span>
</span><span id="__span-4-4"><a id="__codelineno-4-4" name="__codelineno-4-4" href="#__codelineno-4-4"></a><span class="w"> </span><span class="p">}.</span><span class="na">launchIn</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span>
</span><span id="__span-4-5"><a id="__codelineno-4-5" name="__codelineno-4-5" href="#__codelineno-4-5"></a><span class="p">}</span>
</span></code></pre></div></li>
</ul> </ul>
<pre><code class="language-kotlin">flowsUpdatesFilter {
(messageFlow + channelPostFlow).onEach {
println(it) // will be printed each message update from channels and chats both
}.launchIn(scope)
}
</code></pre>
<ul>
<li>TelegramBotAPI library support function <code>aggregateFlows</code>:</li>
</ul>
<pre><code class="language-kotlin">flowsUpdatesFilter {
aggregateFlows(
scope,
messageFlow,
channelPostFlow
).onEach {
println(it) // will be printed each message update from channels and chats both
}.launchIn(scope)
}
</code></pre>
<ul>
<li><code>FlowsUpdatesFilter</code> extensions:</li>
</ul>
<pre><code class="language-kotlin">flowsUpdatesFilter {
allSentMessagesFlow.onEach {
println(it) // will be printed each message update from channels and chats both
}.launchIn(scope)
}
</code></pre>
<h4 id="types-filtering">Types filtering<a class="headerlink" href="#types-filtering" title="Permanent link">&para;</a></h4> <h4 id="types-filtering">Types filtering<a class="headerlink" href="#types-filtering" title="Permanent link">&para;</a></h4>
<p><code>FlowsUpdatesFilter</code> have a lot of extensions for messages types filtering:</p> <p><code>FlowsUpdatesFilter</code> have a lot of extensions for messages types filtering:</p>
<pre><code class="language-kotlin">flowsUpdatesFilter { <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-5-1"><a id="__codelineno-5-1" name="__codelineno-5-1" href="#__codelineno-5-1"></a><span class="n">flowsUpdatesFilter</span><span class="w"> </span><span class="p">{</span>
textMessages(scope).onEach { </span><span id="__span-5-2"><a id="__codelineno-5-2" name="__codelineno-5-2" href="#__codelineno-5-2"></a><span class="w"> </span><span class="n">textMessages</span><span class="p">(</span><span class="n">scope</span><span class="p">).</span><span class="na">onEach</span><span class="w"> </span><span class="p">{</span>
println(it) // will be printed each message from channels and chats both with content only `TextContent` </span><span id="__span-5-3"><a id="__codelineno-5-3" name="__codelineno-5-3" href="#__codelineno-5-3"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span><span class="w"> </span><span class="c1">// will be printed each message from channels and chats both with content only `TextContent`</span>
}.launchIn(scope) </span><span id="__span-5-4"><a id="__codelineno-5-4" name="__codelineno-5-4" href="#__codelineno-5-4"></a><span class="w"> </span><span class="p">}.</span><span class="na">launchIn</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span>
} </span><span id="__span-5-5"><a id="__codelineno-5-5" name="__codelineno-5-5" href="#__codelineno-5-5"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<p>The same things were created for media groups:</p> <p>The same things were created for media groups:</p>
<pre><code class="language-kotlin">flowsUpdatesFilter { <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-6-1"><a id="__codelineno-6-1" name="__codelineno-6-1" href="#__codelineno-6-1"></a><span class="n">flowsUpdatesFilter</span><span class="w"> </span><span class="p">{</span>
mediaGroupMessages(scope).onEach { </span><span id="__span-6-2"><a id="__codelineno-6-2" name="__codelineno-6-2" href="#__codelineno-6-2"></a><span class="w"> </span><span class="n">mediaGroupMessages</span><span class="p">(</span><span class="n">scope</span><span class="p">).</span><span class="na">onEach</span><span class="w"> </span><span class="p">{</span>
println(it) // will be printed each media group messages list from both channels and chats without filtering of content </span><span id="__span-6-3"><a id="__codelineno-6-3" name="__codelineno-6-3" href="#__codelineno-6-3"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span><span class="w"> </span><span class="c1">// will be printed each media group messages list from both channels and chats without filtering of content</span>
}.launchIn(scope) </span><span id="__span-6-4"><a id="__codelineno-6-4" name="__codelineno-6-4" href="#__codelineno-6-4"></a><span class="w"> </span><span class="p">}.</span><span class="na">launchIn</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span>
</span><span id="__span-6-5"><a id="__codelineno-6-5" name="__codelineno-6-5" href="#__codelineno-6-5"></a>
mediaGroupPhotosMessages(scope).onEach { </span><span id="__span-6-6"><a id="__codelineno-6-6" name="__codelineno-6-6" href="#__codelineno-6-6"></a><span class="w"> </span><span class="n">mediaGroupPhotosMessages</span><span class="p">(</span><span class="n">scope</span><span class="p">).</span><span class="na">onEach</span><span class="w"> </span><span class="p">{</span>
println(it) // will be printed each media group messages list from both channels and chats with PhotoContent only </span><span id="__span-6-7"><a id="__codelineno-6-7" name="__codelineno-6-7" href="#__codelineno-6-7"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span><span class="w"> </span><span class="c1">// will be printed each media group messages list from both channels and chats with PhotoContent only</span>
}.launchIn(scope) </span><span id="__span-6-8"><a id="__codelineno-6-8" name="__codelineno-6-8" href="#__codelineno-6-8"></a><span class="w"> </span><span class="p">}.</span><span class="na">launchIn</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span>
</span><span id="__span-6-9"><a id="__codelineno-6-9" name="__codelineno-6-9" href="#__codelineno-6-9"></a>
mediaGroupVideosMessages(scope).onEach { </span><span id="__span-6-10"><a id="__codelineno-6-10" name="__codelineno-6-10" href="#__codelineno-6-10"></a><span class="w"> </span><span class="n">mediaGroupVideosMessages</span><span class="p">(</span><span class="n">scope</span><span class="p">).</span><span class="na">onEach</span><span class="w"> </span><span class="p">{</span>
println(it) // will be printed each media group messages list from both channels and chats with VideoContent only </span><span id="__span-6-11"><a id="__codelineno-6-11" name="__codelineno-6-11" href="#__codelineno-6-11"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span><span class="w"> </span><span class="c1">// will be printed each media group messages list from both channels and chats with VideoContent only</span>
}.launchIn(scope) </span><span id="__span-6-12"><a id="__codelineno-6-12" name="__codelineno-6-12" href="#__codelineno-6-12"></a><span class="w"> </span><span class="p">}.</span><span class="na">launchIn</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span>
} </span><span id="__span-6-13"><a id="__codelineno-6-13" name="__codelineno-6-13" href="#__codelineno-6-13"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<p>Besides, there is an opportunity to avoid separation on media groups and common messages and receive photos and videos content in one flow:</p> <p>Besides, there is an opportunity to avoid separation on media groups and common messages and receive photos and videos content in one flow:</p>
<pre><code class="language-kotlin">flowsUpdatesFilter { <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-7-1"><a id="__codelineno-7-1" name="__codelineno-7-1" href="#__codelineno-7-1"></a><span class="n">flowsUpdatesFilter</span><span class="w"> </span><span class="p">{</span>
sentMessagesWithMediaGroups(scope).onEach { </span><span id="__span-7-2"><a id="__codelineno-7-2" name="__codelineno-7-2" href="#__codelineno-7-2"></a><span class="w"> </span><span class="n">sentMessagesWithMediaGroups</span><span class="p">(</span><span class="n">scope</span><span class="p">).</span><span class="na">onEach</span><span class="w"> </span><span class="p">{</span>
println(it) // will be printed each message including each separated media group message from both channels and chats without filtering of content </span><span id="__span-7-3"><a id="__codelineno-7-3" name="__codelineno-7-3" href="#__codelineno-7-3"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span><span class="w"> </span><span class="c1">// will be printed each message including each separated media group message from both channels and chats without filtering of content</span>
}.launchIn(scope) </span><span id="__span-7-4"><a id="__codelineno-7-4" name="__codelineno-7-4" href="#__codelineno-7-4"></a><span class="w"> </span><span class="p">}.</span><span class="na">launchIn</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span>
</span><span id="__span-7-5"><a id="__codelineno-7-5" name="__codelineno-7-5" href="#__codelineno-7-5"></a>
photoMessagesWithMediaGroups(scope).onEach { </span><span id="__span-7-6"><a id="__codelineno-7-6" name="__codelineno-7-6" href="#__codelineno-7-6"></a><span class="w"> </span><span class="n">photoMessagesWithMediaGroups</span><span class="p">(</span><span class="n">scope</span><span class="p">).</span><span class="na">onEach</span><span class="w"> </span><span class="p">{</span>
println(it) // will be printed each message including each separated media group message from both channels and chats with PhotoContent only </span><span id="__span-7-7"><a id="__codelineno-7-7" name="__codelineno-7-7" href="#__codelineno-7-7"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span><span class="w"> </span><span class="c1">// will be printed each message including each separated media group message from both channels and chats with PhotoContent only</span>
}.launchIn(scope) </span><span id="__span-7-8"><a id="__codelineno-7-8" name="__codelineno-7-8" href="#__codelineno-7-8"></a><span class="w"> </span><span class="p">}.</span><span class="na">launchIn</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span>
</span><span id="__span-7-9"><a id="__codelineno-7-9" name="__codelineno-7-9" href="#__codelineno-7-9"></a>
videoMessagesWithMediaGroups(scope).onEach { </span><span id="__span-7-10"><a id="__codelineno-7-10" name="__codelineno-7-10" href="#__codelineno-7-10"></a><span class="w"> </span><span class="n">videoMessagesWithMediaGroups</span><span class="p">(</span><span class="n">scope</span><span class="p">).</span><span class="na">onEach</span><span class="w"> </span><span class="p">{</span>
println(it) // will be printed each message including each separated media group message from both channels and chats with VideoContent only </span><span id="__span-7-11"><a id="__codelineno-7-11" name="__codelineno-7-11" href="#__codelineno-7-11"></a><span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="nb">it</span><span class="p">)</span><span class="w"> </span><span class="c1">// will be printed each message including each separated media group message from both channels and chats with VideoContent only</span>
}.launchIn(scope) </span><span id="__span-7-12"><a id="__codelineno-7-12" name="__codelineno-7-12" href="#__codelineno-7-12"></a><span class="w"> </span><span class="p">}.</span><span class="na">launchIn</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span>
} </span><span id="__span-7-13"><a id="__codelineno-7-13" name="__codelineno-7-13" href="#__codelineno-7-13"></a><span class="p">}</span>
</code></pre> </span></code></pre></div>
<h2 id="see-also">See also<a class="headerlink" href="#see-also" title="Permanent link">&para;</a></h2> <h2 id="see-also">See also<a class="headerlink" href="#see-also" title="Permanent link">&para;</a></h2>
<ul> <ul>
<li><a href="long-polling.html">Long polling</a></li> <li><a href="long-polling.html">Long polling</a></li>

View File

@ -1266,98 +1266,98 @@
</ul> </ul>
<h3 id="setwebhookinfoandstartlistenwebhooks"><code>setWebhookInfoAndStartListenWebhooks</code><a class="headerlink" href="#setwebhookinfoandstartlistenwebhooks" title="Permanent link">&para;</a></h3> <h3 id="setwebhookinfoandstartlistenwebhooks"><code>setWebhookInfoAndStartListenWebhooks</code><a class="headerlink" href="#setwebhookinfoandstartlistenwebhooks" title="Permanent link">&para;</a></h3>
<p>It is the most common way to set updates webhooks and start listening of them. Example:</p> <p>It is the most common way to set updates webhooks and start listening of them. Example:</p>
<pre><code class="language-kotlin">val bot = telegramBot(TOKEN) <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">bot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">telegramBot</span><span class="p">(</span><span class="n">TOKEN</span><span class="p">)</span>
</span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a>
val filter = flowsUpdatesFilter { </span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="kd">val</span><span class="w"> </span><span class="nv">filter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">flowsUpdatesFilter</span><span class="w"> </span><span class="p">{</span>
// ... </span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="w"> </span><span class="c1">// ...</span>
} </span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="p">}</span>
</span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a>
bot.setWebhookInfoAndStartListenWebhooks( </span><span id="__span-0-7"><a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="n">bot</span><span class="p">.</span><span class="na">setWebhookInfoAndStartListenWebhooks</span><span class="p">(</span>
8080, // listening port. It is required for cases when your server hidden by some proxy or other system like Heroku </span><span id="__span-0-8"><a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="w"> </span><span class="m">8080</span><span class="p">,</span><span class="w"> </span><span class="c1">// listening port. It is required for cases when your server hidden by some proxy or other system like Heroku</span>
CIO, // default ktor server engine. It is recommended to replace it with something like `Netty`. More info about engines here: https://ktor.io/servers/configuration.html </span><span id="__span-0-9"><a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a><span class="w"> </span><span class="n">CIO</span><span class="p">,</span><span class="w"> </span><span class="c1">// default ktor server engine. It is recommended to replace it with something like `Netty`. More info about engines here: https://ktor.io/servers/configuration.html</span>
SetWebhook( </span><span id="__span-0-10"><a id="__codelineno-0-10" name="__codelineno-0-10" href="#__codelineno-0-10"></a><span class="w"> </span><span class="n">SetWebhook</span><span class="p">(</span>
&quot;address.com/webhook_route&quot;, </span><span id="__span-0-11"><a id="__codelineno-0-11" name="__codelineno-0-11" href="#__codelineno-0-11"></a><span class="w"> </span><span class="s">&quot;address.com/webhook_route&quot;</span><span class="p">,</span>
File(&quot;/path/to/certificate&quot;).toInputFile(), // certificate file. More info here: https://core.telegram.org/bots/webhooks#a-certificate-where-do-i-get-one-and-how </span><span id="__span-0-12"><a id="__codelineno-0-12" name="__codelineno-0-12" href="#__codelineno-0-12"></a><span class="w"> </span><span class="n">File</span><span class="p">(</span><span class="s">&quot;/path/to/certificate&quot;</span><span class="p">).</span><span class="na">toInputFile</span><span class="p">(),</span><span class="w"> </span><span class="c1">// certificate file. More info here: https://core.telegram.org/bots/webhooks#a-certificate-where-do-i-get-one-and-how</span>
40, // max allowed updates, by default is null </span><span id="__span-0-13"><a id="__codelineno-0-13" name="__codelineno-0-13" href="#__codelineno-0-13"></a><span class="w"> </span><span class="m">40</span><span class="p">,</span><span class="w"> </span><span class="c1">// max allowed updates, by default is null</span>
filter.allowedUpdates </span><span id="__span-0-14"><a id="__codelineno-0-14" name="__codelineno-0-14" href="#__codelineno-0-14"></a><span class="w"> </span><span class="n">filter</span><span class="p">.</span><span class="na">allowedUpdates</span>
), </span><span id="__span-0-15"><a id="__codelineno-0-15" name="__codelineno-0-15" href="#__codelineno-0-15"></a><span class="w"> </span><span class="p">),</span>
{ </span><span id="__span-0-16"><a id="__codelineno-0-16" name="__codelineno-0-16" href="#__codelineno-0-16"></a><span class="w"> </span><span class="p">{</span>
it.printStackTrace() // optional handling of exceptions </span><span id="__span-0-17"><a id="__codelineno-0-17" name="__codelineno-0-17" href="#__codelineno-0-17"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">printStackTrace</span><span class="p">()</span><span class="w"> </span><span class="c1">// optional handling of exceptions</span>
}, </span><span id="__span-0-18"><a id="__codelineno-0-18" name="__codelineno-0-18" href="#__codelineno-0-18"></a><span class="w"> </span><span class="p">},</span>
&quot;0.0.0.0&quot;, // listening host which will be used to bind by server </span><span id="__span-0-19"><a id="__codelineno-0-19" name="__codelineno-0-19" href="#__codelineno-0-19"></a><span class="w"> </span><span class="s">&quot;0.0.0.0&quot;</span><span class="p">,</span><span class="w"> </span><span class="c1">// listening host which will be used to bind by server</span>
&quot;subroute&quot;, // Optional subroute, if null - will listen root of address </span><span id="__span-0-20"><a id="__codelineno-0-20" name="__codelineno-0-20" href="#__codelineno-0-20"></a><span class="w"> </span><span class="s">&quot;subroute&quot;</span><span class="p">,</span><span class="w"> </span><span class="c1">// Optional subroute, if null - will listen root of address</span>
WebhookPrivateKeyConfig( // optional config of private key. It will be installed in server to use TLS with custom certificate. More info here: https://core.telegram.org/bots/webhooks#a-certificate-where-do-i-get-one-and-how </span><span id="__span-0-21"><a id="__codelineno-0-21" name="__codelineno-0-21" href="#__codelineno-0-21"></a><span class="w"> </span><span class="n">WebhookPrivateKeyConfig</span><span class="p">(</span><span class="w"> </span><span class="c1">// optional config of private key. It will be installed in server to use TLS with custom certificate. More info here: https://core.telegram.org/bots/webhooks#a-certificate-where-do-i-get-one-and-how</span>
&quot;/path/to/keystore.jks&quot;, </span><span id="__span-0-22"><a id="__codelineno-0-22" name="__codelineno-0-22" href="#__codelineno-0-22"></a><span class="w"> </span><span class="s">&quot;/path/to/keystore.jks&quot;</span><span class="p">,</span>
&quot;KeystorePassword&quot;, </span><span id="__span-0-23"><a id="__codelineno-0-23" name="__codelineno-0-23" href="#__codelineno-0-23"></a><span class="w"> </span><span class="s">&quot;KeystorePassword&quot;</span><span class="p">,</span>
&quot;Keystore key alias name&quot;, </span><span id="__span-0-24"><a id="__codelineno-0-24" name="__codelineno-0-24" href="#__codelineno-0-24"></a><span class="w"> </span><span class="s">&quot;Keystore key alias name&quot;</span><span class="p">,</span>
&quot;KeystoreAliasPassword&quot; </span><span id="__span-0-25"><a id="__codelineno-0-25" name="__codelineno-0-25" href="#__codelineno-0-25"></a><span class="w"> </span><span class="s">&quot;KeystoreAliasPassword&quot;</span>
), </span><span id="__span-0-26"><a id="__codelineno-0-26" name="__codelineno-0-26" href="#__codelineno-0-26"></a><span class="w"> </span><span class="p">),</span>
scope, // Kotlin coroutine scope for internal transforming of media groups </span><span id="__span-0-27"><a id="__codelineno-0-27" name="__codelineno-0-27" href="#__codelineno-0-27"></a><span class="w"> </span><span class="n">scope</span><span class="p">,</span><span class="w"> </span><span class="c1">// Kotlin coroutine scope for internal transforming of media groups</span>
filter.asUpdateReceiver </span><span id="__span-0-28"><a id="__codelineno-0-28" name="__codelineno-0-28" href="#__codelineno-0-28"></a><span class="w"> </span><span class="n">filter</span><span class="p">.</span><span class="na">asUpdateReceiver</span>
) </span><span id="__span-0-29"><a id="__codelineno-0-29" name="__codelineno-0-29" href="#__codelineno-0-29"></a><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>If you will use previous example, ktor server will bind and listen url <code>0.0.0.0:8080/subroute</code> and telegram will send requests to address <code>address.com/webhook_route</code> with custom certificate. Alternative variant will use the other <code>SetWebhook</code> request variant:</p> <p>If you will use previous example, ktor server will bind and listen url <code>0.0.0.0:8080/subroute</code> and telegram will send requests to address <code>address.com/webhook_route</code> with custom certificate. Alternative variant will use the other <code>SetWebhook</code> request variant:</p>
<pre><code class="language-kotlin">SetWebhook( <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="n">SetWebhook</span><span class="p">(</span>
&quot;address.com/webhook_route&quot;, </span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a><span class="w"> </span><span class="s">&quot;address.com/webhook_route&quot;</span><span class="p">,</span>
&quot;some_file_bot_id&quot;.toInputFile(), </span><span id="__span-1-3"><a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="w"> </span><span class="s">&quot;some_file_bot_id&quot;</span><span class="p">.</span><span class="na">toInputFile</span><span class="p">(),</span>
40, // max allowed updates, by default is null </span><span id="__span-1-4"><a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="w"> </span><span class="m">40</span><span class="p">,</span><span class="w"> </span><span class="c1">// max allowed updates, by default is null</span>
filter.allowedUpdates </span><span id="__span-1-5"><a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="w"> </span><span class="n">filter</span><span class="p">.</span><span class="na">allowedUpdates</span>
) </span><span id="__span-1-6"><a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>As a result, request <code>SetWebhook</code> will be executed and after this server will start its working and handling of updates.</p> <p>As a result, request <code>SetWebhook</code> will be executed and after this server will start its working and handling of updates.</p>
<h3 id="startlistenwebhooks"><code>startListenWebhooks</code><a class="headerlink" href="#startlistenwebhooks" title="Permanent link">&para;</a></h3> <h3 id="startlistenwebhooks"><code>startListenWebhooks</code><a class="headerlink" href="#startlistenwebhooks" title="Permanent link">&para;</a></h3>
<p>This function is working almost exactly like previous example, but this one will not set up webhook info in telegram:</p> <p>This function is working almost exactly like previous example, but this one will not set up webhook info in telegram:</p>
<pre><code class="language-kotlin">val filter = flowsUpdatesFilter { <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">filter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">flowsUpdatesFilter</span><span class="w"> </span><span class="p">{</span>
// ... </span><span id="__span-2-2"><a id="__codelineno-2-2" name="__codelineno-2-2" href="#__codelineno-2-2"></a><span class="w"> </span><span class="c1">// ...</span>
} </span><span id="__span-2-3"><a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a><span class="p">}</span>
</span><span id="__span-2-4"><a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a>
startListenWebhooks( </span><span id="__span-2-5"><a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a><span class="n">startListenWebhooks</span><span class="p">(</span>
8080, // listening port. It is required for cases when your server hidden by some proxy or other system like Heroku </span><span id="__span-2-6"><a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a><span class="w"> </span><span class="m">8080</span><span class="p">,</span><span class="w"> </span><span class="c1">// listening port. It is required for cases when your server hidden by some proxy or other system like Heroku</span>
CIO, // default ktor server engine. It is recommended to replace it with something like `Netty`. More info about engines here: https://ktor.io/servers/configuration.html </span><span id="__span-2-7"><a id="__codelineno-2-7" name="__codelineno-2-7" href="#__codelineno-2-7"></a><span class="w"> </span><span class="n">CIO</span><span class="p">,</span><span class="w"> </span><span class="c1">// default ktor server engine. It is recommended to replace it with something like `Netty`. More info about engines here: https://ktor.io/servers/configuration.html</span>
{ </span><span id="__span-2-8"><a id="__codelineno-2-8" name="__codelineno-2-8" href="#__codelineno-2-8"></a><span class="w"> </span><span class="p">{</span>
it.printStackTrace() // optional handling of exceptions </span><span id="__span-2-9"><a id="__codelineno-2-9" name="__codelineno-2-9" href="#__codelineno-2-9"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">printStackTrace</span><span class="p">()</span><span class="w"> </span><span class="c1">// optional handling of exceptions</span>
}, </span><span id="__span-2-10"><a id="__codelineno-2-10" name="__codelineno-2-10" href="#__codelineno-2-10"></a><span class="w"> </span><span class="p">},</span>
&quot;0.0.0.0&quot;, // listening host which will be used to bind by server </span><span id="__span-2-11"><a id="__codelineno-2-11" name="__codelineno-2-11" href="#__codelineno-2-11"></a><span class="w"> </span><span class="s">&quot;0.0.0.0&quot;</span><span class="p">,</span><span class="w"> </span><span class="c1">// listening host which will be used to bind by server</span>
&quot;subroute&quot;, // Optional subroute, if null - will listen root of address </span><span id="__span-2-12"><a id="__codelineno-2-12" name="__codelineno-2-12" href="#__codelineno-2-12"></a><span class="w"> </span><span class="s">&quot;subroute&quot;</span><span class="p">,</span><span class="w"> </span><span class="c1">// Optional subroute, if null - will listen root of address</span>
WebhookPrivateKeyConfig( // optional config of private key. It will be installed in server to use TLS with custom certificate. More info here: https://core.telegram.org/bots/webhooks#a-certificate-where-do-i-get-one-and-how </span><span id="__span-2-13"><a id="__codelineno-2-13" name="__codelineno-2-13" href="#__codelineno-2-13"></a><span class="w"> </span><span class="n">WebhookPrivateKeyConfig</span><span class="p">(</span><span class="w"> </span><span class="c1">// optional config of private key. It will be installed in server to use TLS with custom certificate. More info here: https://core.telegram.org/bots/webhooks#a-certificate-where-do-i-get-one-and-how</span>
&quot;/path/to/keystore.jks&quot;, </span><span id="__span-2-14"><a id="__codelineno-2-14" name="__codelineno-2-14" href="#__codelineno-2-14"></a><span class="w"> </span><span class="s">&quot;/path/to/keystore.jks&quot;</span><span class="p">,</span>
&quot;KeystorePassword&quot;, </span><span id="__span-2-15"><a id="__codelineno-2-15" name="__codelineno-2-15" href="#__codelineno-2-15"></a><span class="w"> </span><span class="s">&quot;KeystorePassword&quot;</span><span class="p">,</span>
&quot;Keystore key alias name&quot;, </span><span id="__span-2-16"><a id="__codelineno-2-16" name="__codelineno-2-16" href="#__codelineno-2-16"></a><span class="w"> </span><span class="s">&quot;Keystore key alias name&quot;</span><span class="p">,</span>
&quot;KeystoreAliasPassword&quot; </span><span id="__span-2-17"><a id="__codelineno-2-17" name="__codelineno-2-17" href="#__codelineno-2-17"></a><span class="w"> </span><span class="s">&quot;KeystoreAliasPassword&quot;</span>
), </span><span id="__span-2-18"><a id="__codelineno-2-18" name="__codelineno-2-18" href="#__codelineno-2-18"></a><span class="w"> </span><span class="p">),</span>
scope, // Kotlin coroutine scope for internal transforming of media groups </span><span id="__span-2-19"><a id="__codelineno-2-19" name="__codelineno-2-19" href="#__codelineno-2-19"></a><span class="w"> </span><span class="n">scope</span><span class="p">,</span><span class="w"> </span><span class="c1">// Kotlin coroutine scope for internal transforming of media groups</span>
filter.asUpdateReceiver </span><span id="__span-2-20"><a id="__codelineno-2-20" name="__codelineno-2-20" href="#__codelineno-2-20"></a><span class="w"> </span><span class="n">filter</span><span class="p">.</span><span class="na">asUpdateReceiver</span>
) </span><span id="__span-2-21"><a id="__codelineno-2-21" name="__codelineno-2-21" href="#__codelineno-2-21"></a><span class="p">)</span>
</code></pre> </span></code></pre></div>
<p>The result will be the same as in previous example: server will start its working and handling of updates on <code>0.0.0.0:8080/subroute</code>. The difference here is that in case if this bot must not answer or send some requiests - it will not be necessary to create bot for receiving of updates.</p> <p>The result will be the same as in previous example: server will start its working and handling of updates on <code>0.0.0.0:8080/subroute</code>. The difference here is that in case if this bot must not answer or send some requiests - it will not be necessary to create bot for receiving of updates.</p>
<h3 id="extensions-includewebhookhandlinginroute-and-includewebhookhandlinginroutewithflows">Extensions <code>includeWebhookHandlingInRoute</code> and <code>includeWebhookHandlingInRouteWithFlows</code><a class="headerlink" href="#extensions-includewebhookhandlinginroute-and-includewebhookhandlinginroutewithflows" title="Permanent link">&para;</a></h3> <h3 id="extensions-includewebhookhandlinginroute-and-includewebhookhandlinginroutewithflows">Extensions <code>includeWebhookHandlingInRoute</code> and <code>includeWebhookHandlingInRouteWithFlows</code><a class="headerlink" href="#extensions-includewebhookhandlinginroute-and-includewebhookhandlinginroutewithflows" title="Permanent link">&para;</a></h3>
<p>For these extensions you will need to start your server manualy. In common case it will look like:</p> <p>For these extensions you will need to start your server manualy. In common case it will look like:</p>
<pre><code class="language-kotlin">val scope = CoroutineScope(Dispatchers.Default) <div class="language-kotlin highlight"><pre><span></span><code><span id="__span-3-1"><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a><span class="kd">val</span><span class="w"> </span><span class="nv">scope</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">CoroutineScope</span><span class="p">(</span><span class="n">Dispatchers</span><span class="p">.</span><span class="na">Default</span><span class="p">)</span>
</span><span id="__span-3-2"><a id="__codelineno-3-2" name="__codelineno-3-2" href="#__codelineno-3-2"></a>
val filter = flowsUpdatesFilter { </span><span id="__span-3-3"><a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a><span class="kd">val</span><span class="w"> </span><span class="nv">filter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">flowsUpdatesFilter</span><span class="w"> </span><span class="p">{</span>
// ... </span><span id="__span-3-4"><a id="__codelineno-3-4" name="__codelineno-3-4" href="#__codelineno-3-4"></a><span class="w"> </span><span class="c1">// ...</span>
} </span><span id="__span-3-5"><a id="__codelineno-3-5" name="__codelineno-3-5" href="#__codelineno-3-5"></a><span class="p">}</span>
</span><span id="__span-3-6"><a id="__codelineno-3-6" name="__codelineno-3-6" href="#__codelineno-3-6"></a>
val environment = applicationEngineEnvironment { </span><span id="__span-3-7"><a id="__codelineno-3-7" name="__codelineno-3-7" href="#__codelineno-3-7"></a><span class="kd">val</span><span class="w"> </span><span class="nv">environment</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">applicationEngineEnvironment</span><span class="w"> </span><span class="p">{</span>
module { </span><span id="__span-3-8"><a id="__codelineno-3-8" name="__codelineno-3-8" href="#__codelineno-3-8"></a><span class="w"> </span><span class="n">module</span><span class="w"> </span><span class="p">{</span>
routing { </span><span id="__span-3-9"><a id="__codelineno-3-9" name="__codelineno-3-9" href="#__codelineno-3-9"></a><span class="w"> </span><span class="n">routing</span><span class="w"> </span><span class="p">{</span>
includeWebhookHandlingInRoute( </span><span id="__span-3-10"><a id="__codelineno-3-10" name="__codelineno-3-10" href="#__codelineno-3-10"></a><span class="w"> </span><span class="n">includeWebhookHandlingInRoute</span><span class="p">(</span>
scope, </span><span id="__span-3-11"><a id="__codelineno-3-11" name="__codelineno-3-11" href="#__codelineno-3-11"></a><span class="w"> </span><span class="n">scope</span><span class="p">,</span>
{ </span><span id="__span-3-12"><a id="__codelineno-3-12" name="__codelineno-3-12" href="#__codelineno-3-12"></a><span class="w"> </span><span class="p">{</span>
it.printStackTrace() </span><span id="__span-3-13"><a id="__codelineno-3-13" name="__codelineno-3-13" href="#__codelineno-3-13"></a><span class="w"> </span><span class="nb">it</span><span class="p">.</span><span class="na">printStackTrace</span><span class="p">()</span>
}, </span><span id="__span-3-14"><a id="__codelineno-3-14" name="__codelineno-3-14" href="#__codelineno-3-14"></a><span class="w"> </span><span class="p">},</span>
filter.asUpdateReceiver </span><span id="__span-3-15"><a id="__codelineno-3-15" name="__codelineno-3-15" href="#__codelineno-3-15"></a><span class="w"> </span><span class="n">filter</span><span class="p">.</span><span class="na">asUpdateReceiver</span>
) </span><span id="__span-3-16"><a id="__codelineno-3-16" name="__codelineno-3-16" href="#__codelineno-3-16"></a><span class="w"> </span><span class="p">)</span>
} </span><span id="__span-3-17"><a id="__codelineno-3-17" name="__codelineno-3-17" href="#__codelineno-3-17"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-3-18"><a id="__codelineno-3-18" name="__codelineno-3-18" href="#__codelineno-3-18"></a><span class="w"> </span><span class="p">}</span>
connector { </span><span id="__span-3-19"><a id="__codelineno-3-19" name="__codelineno-3-19" href="#__codelineno-3-19"></a><span class="w"> </span><span class="n">connector</span><span class="w"> </span><span class="p">{</span>
host = &quot;0.0.0.0&quot; </span><span id="__span-3-20"><a id="__codelineno-3-20" name="__codelineno-3-20" href="#__codelineno-3-20"></a><span class="w"> </span><span class="n">host</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;0.0.0.0&quot;</span>
port = 8080 </span><span id="__span-3-21"><a id="__codelineno-3-21" name="__codelineno-3-21" href="#__codelineno-3-21"></a><span class="w"> </span><span class="n">port</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">8080</span>
} </span><span id="__span-3-22"><a id="__codelineno-3-22" name="__codelineno-3-22" href="#__codelineno-3-22"></a><span class="w"> </span><span class="p">}</span>
} </span><span id="__span-3-23"><a id="__codelineno-3-23" name="__codelineno-3-23" href="#__codelineno-3-23"></a><span class="p">}</span>
</span><span id="__span-3-24"><a id="__codelineno-3-24" name="__codelineno-3-24" href="#__codelineno-3-24"></a>
embeddedServer(CIO, environment).start(true) // will start server and wait its stoping </span><span id="__span-3-25"><a id="__codelineno-3-25" name="__codelineno-3-25" href="#__codelineno-3-25"></a><span class="n">embeddedServer</span><span class="p">(</span><span class="n">CIO</span><span class="p">,</span><span class="w"> </span><span class="n">environment</span><span class="p">).</span><span class="na">start</span><span class="p">(</span><span class="kc">true</span><span class="p">)</span><span class="w"> </span><span class="c1">// will start server and wait its stoping</span>
</code></pre> </span></code></pre></div>
<p>In the example above server will started and binded for listening on <code>0.0.0.0:8080</code>.</p> <p>In the example above server will started and binded for listening on <code>0.0.0.0:8080</code>.</p>
<h2 id="see-also">See also<a class="headerlink" href="#see-also" title="Permanent link">&para;</a></h2> <h2 id="see-also">See also<a class="headerlink" href="#see-also" title="Permanent link">&para;</a></h2>
<ul> <ul>