mirror of
https://github.com/InsanusMokrassar/docs.git
synced 2024-12-29 12:00:17 +00:00
deploy: f7a2359066
This commit is contained in:
parent
7457aea297
commit
d67f0e483a
@ -1284,33 +1284,33 @@
|
||||
See <a href="string-format.html">String format</a> for more info about the crontab-line syntax</p>
|
||||
</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>
|
||||
<pre><code class="language-kotlin">val schedule = "5 * * * *"
|
||||
val scheduler = buildSchedule(schedule)
|
||||
|
||||
scheduler.asFlow().onEach {
|
||||
// this block will be called every minute at 5 seconds
|
||||
}.launchIn(someCoroutineScope)
|
||||
</code></pre>
|
||||
<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">"5 * * * *"</span>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
<h4 id="lambda-way">Lambda way<a class="headerlink" href="#lambda-way" title="Permanent link">¶</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>
|
||||
<pre><code class="language-kotlin">val scheduler = buildSchedule {
|
||||
seconds {
|
||||
at(5)
|
||||
}
|
||||
}
|
||||
|
||||
scheduler.asFlow().onEach {
|
||||
// this block will be called every minute at 5 seconds
|
||||
}.launchIn(someCoroutineScope)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
<h3 id="custom-scheduler">Custom scheduler<a class="headerlink" href="#custom-scheduler" title="Permanent link">¶</a></h3>
|
||||
<p>You are always able to use your own realisation of scheduler. For example:</p>
|
||||
<pre><code class="language-kotlin">class RandomScheduler : KronScheduler {
|
||||
override suspend fun next(relatively: DateTime): DateTime {
|
||||
return relatively + DateTimeSpan(seconds = Random.nextInt() % 60)
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
|
||||
|
||||
|
@ -1403,22 +1403,22 @@
|
||||
</tbody>
|
||||
</table>
|
||||
<p>Example with almost same description:</p>
|
||||
<pre><code>/-------------------- (0-59) ············ Seconds
|
||||
| /------------------ (0-59) ············ Minutes
|
||||
| | /---------------- (0-23) ············ Hours
|
||||
| | | /-------------- (0-30) ············ Days of months
|
||||
| | | | /------------ (0-11) ············ Months
|
||||
| | | | | /---------- (optional, any int) Year
|
||||
| | | | | | /-------- (optional) ········ Timezone offset
|
||||
| | | | | | | /----- (optional, 0-6) ··· Week days
|
||||
| | | | | | | | /-- (optional, 0-999) · Milliseconds (0 by default)
|
||||
* * * * * * 0o *w 0ms
|
||||
</code></pre>
|
||||
<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
|
||||
</span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a>| /------------------ (0-59) ············ Minutes
|
||||
</span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a>| | /---------------- (0-23) ············ Hours
|
||||
</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
|
||||
</span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a>| | | | /------------ (0-11) ············ Months
|
||||
</span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a>| | | | | /---------- (optional, any int) Year
|
||||
</span><span id="__span-0-7"><a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a>| | | | | | /-------- (optional) ········ Timezone offset
|
||||
</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
|
||||
</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)
|
||||
</span><span id="__span-0-10"><a id="__codelineno-0-10" name="__codelineno-0-10" href="#__codelineno-0-10"></a>* * * * * * 0o *w 0ms
|
||||
</span></code></pre></div>
|
||||
<p>Years, timezone, week days and milliseconds are optional settings. Next snippets are equal:</p>
|
||||
<pre><code>*/15 * * * *
|
||||
*/15 * * * * * // with year
|
||||
*/15 * * * * * 0ms // with year and milliseconds
|
||||
</code></pre>
|
||||
<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 * * * *
|
||||
</span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a>*/15 * * * * * // with year
|
||||
</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
|
||||
</span></code></pre></div>
|
||||
<h3 id="supported-syntax">Supported syntax<a class="headerlink" href="#supported-syntax" title="Permanent link">¶</a></h3>
|
||||
<p>Currently the library support next syntax for date/time elements:</p>
|
||||
<ul>
|
||||
@ -1432,31 +1432,31 @@
|
||||
</ul>
|
||||
<h4 id="ranges">Ranges<a class="headerlink" href="#ranges" title="Permanent link">¶</a></h4>
|
||||
<p>Ranges are working like common <code>rangeTo</code> (or <code>..</code>) in kotlin:</p>
|
||||
<pre><code>0-5 * * * *
|
||||
</code></pre>
|
||||
<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 * * * *
|
||||
</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>
|
||||
<h4 id="startstep">Start/Step<a class="headerlink" href="#startstep" title="Permanent link">¶</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>
|
||||
<pre><code>5/15 * * * *
|
||||
</code></pre>
|
||||
<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 * * * *
|
||||
</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>
|
||||
<h4 id="every">Every<a class="headerlink" href="#every" title="Permanent link">¶</a></h4>
|
||||
<p>Every is more simple syntax and could be explained as a shortcut for <code>0/{int}</code>. Example:</p>
|
||||
<pre><code>*/15 * * * *
|
||||
</code></pre>
|
||||
<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 * * * *
|
||||
</span></code></pre></div>
|
||||
<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">¶</a></h4>
|
||||
<p>The most simple syntax. It means, that scheduler will call triggering every time when element was reached:</p>
|
||||
<pre><code>15 * * * *
|
||||
</code></pre>
|
||||
<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 * * * *
|
||||
</span></code></pre></div>
|
||||
<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">¶</a></h4>
|
||||
<p>All the previous elements can be combined with listing. Lets just see several examples:</p>
|
||||
<pre><code>0,10 * * * *
|
||||
</code></pre>
|
||||
<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 * * * *
|
||||
</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>
|
||||
<pre><code>0-5,10 * * * *
|
||||
</code></pre>
|
||||
<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 * * * *
|
||||
</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>
|
||||
<h3 id="examples">Examples<a class="headerlink" href="#examples" title="Permanent link">¶</a></h3>
|
||||
<ul>
|
||||
|
@ -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>
|
||||
<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">¶</a></h4>
|
||||
<p>In two words, you should call <code>buildSchedule</code> or <code>createSimpleScheduler</code>:</p>
|
||||
<pre><code class="language-kotlin">buildSchedule("5 * * * *").asFlow().collect { /* do something */ }
|
||||
</code></pre>
|
||||
<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">"5 * * * *"</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>
|
||||
</span></code></pre></div>
|
||||
<p>You can read more about syntax in <a href="../describing/string-format.html">String format</a> section.</p>
|
||||
|
||||
|
||||
|
@ -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>
|
||||
</div>
|
||||
<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("5 * * * *")
|
||||
</code></pre>
|
||||
<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">"5 * * * *"</span><span class="p">)</span>
|
||||
</span></code></pre></div>
|
||||
<p>Or:</p>
|
||||
<pre><code class="language-kotlin">val scheduler = buildSchedule {
|
||||
seconds {
|
||||
at(5)
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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></code></pre></div>
|
||||
<p>On the top of any <code>KronScheduler</code> currently there are several groups of extensions:</p>
|
||||
<ul>
|
||||
<li>Executes</li>
|
||||
|
@ -1300,20 +1300,20 @@
|
||||
<h3 id="notice-about-repository">Notice about repository<a class="headerlink" href="#notice-about-repository" title="Permanent link">¶</a></h3>
|
||||
<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">¶</a></h6>
|
||||
<pre><code class="language-groovy">mavenCentral()
|
||||
</code></pre>
|
||||
<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>
|
||||
</span></code></pre></div>
|
||||
<h3 id="dependencies">Dependencies<a class="headerlink" href="#dependencies" title="Permanent link">¶</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>
|
||||
<h4 id="gradle">Gradle<a class="headerlink" href="#gradle" title="Permanent link">¶</a></h4>
|
||||
<pre><code class="language-groovy">implementation "dev.inmo:krontab:$krontab_version"
|
||||
</code></pre>
|
||||
<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">"dev.inmo:krontab:$krontab_version"</span>
|
||||
</span></code></pre></div>
|
||||
<h4 id="maven">Maven<a class="headerlink" href="#maven" title="Permanent link">¶</a></h4>
|
||||
<pre><code class="language-xml"><dependency>
|
||||
<groupId>dev.inmo</groupId>
|
||||
<artifactId>krontab</artifactId>
|
||||
<version>${krontab_version}</version>
|
||||
</dependency>
|
||||
</code></pre>
|
||||
<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"><dependency></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="nt"><groupId></span>dev.inmo<span class="nt"></groupId></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="nt"><artifactId></span>krontab<span class="nt"></artifactId></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="nt"><version></span>${krontab_version}<span class="nt"></version></span>
|
||||
</span><span id="__span-2-5"><a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a><span class="nt"></dependency></span>
|
||||
</span></code></pre></div>
|
||||
|
||||
|
||||
|
||||
|
@ -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>
|
||||
</ul>
|
||||
<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
|
||||
|
||||
// with callback
|
||||
logger.info(tag, throwable) {
|
||||
"Some your message for this event"
|
||||
}
|
||||
|
||||
// with suspendable callback
|
||||
logger.infoS(tag, throwable) {
|
||||
withContext(Dispatchers.Default) {
|
||||
"Some your message for this event"
|
||||
}
|
||||
}
|
||||
|
||||
// Just with message
|
||||
logger.info("Some your message for this event", throwable)
|
||||
|
||||
// With message and tag as strings
|
||||
logger.info(tag, "Some your message for this event", throwable)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"Some your message for this event"</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"Some your message for this event"</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>
|
||||
</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>
|
||||
</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">"Some your message for this event"</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>
|
||||
</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>
|
||||
</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">"Some your message for this event"</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span>
|
||||
</span></code></pre></div>
|
||||
<p>Of course, any of this calls can be shortenned:</p>
|
||||
<pre><code class="language-kotlin">val logger = KSLog.default
|
||||
|
||||
// with callback
|
||||
logger.i(tag, throwable) {
|
||||
"Some your message for this event"
|
||||
}
|
||||
|
||||
// with suspendable callback
|
||||
logger.iS(tag, throwable) {
|
||||
withContext(Dispatchers.Default) {
|
||||
"Some your message for this event"
|
||||
}
|
||||
}
|
||||
|
||||
// Just with message
|
||||
logger.i("Some your message for this event", throwable)
|
||||
|
||||
// With message and tag as strings
|
||||
logger.i(tag, "Some your message for this event", throwable)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"Some your message for this event"</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"Some your message for this event"</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>
|
||||
</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>
|
||||
</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">"Some your message for this event"</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>
|
||||
</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>
|
||||
</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">"Some your message for this event"</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span>
|
||||
</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>
|
||||
<pre><code class="language-kotlin">val logger = KSLog.default
|
||||
|
||||
// with callback
|
||||
logger.log(LogLevel.INFO, tag, throwable) {
|
||||
"Some your message for this event"
|
||||
}
|
||||
|
||||
// with suspendable callback
|
||||
logger.logS(LogLevel.INFO, tag, throwable) {
|
||||
withContext(Dispatchers.Default) {
|
||||
"Some your message for this event"
|
||||
}
|
||||
}
|
||||
|
||||
// Just with message
|
||||
logger.log(LogLevel.INFO, "Some your message for this event", throwable)
|
||||
|
||||
// With message and tag as strings
|
||||
logger.log(LogLevel.INFO, tag, "Some your message for this event", throwable)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"Some your message for this event"</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"Some your message for this event"</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>
|
||||
</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>
|
||||
</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">"Some your message for this event"</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>
|
||||
</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>
|
||||
</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">"Some your message for this event"</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span>
|
||||
</span></code></pre></div>
|
||||
<p>OR</p>
|
||||
<pre><code class="language-kotlin">val logger = KSLog.default
|
||||
|
||||
// with callback
|
||||
logger.l(LogLevel.INFO, tag, throwable) {
|
||||
"Some your message for this event"
|
||||
}
|
||||
|
||||
// with suspendable callback
|
||||
logger.lS(LogLevel.INFO, tag, throwable) {
|
||||
withContext(Dispatchers.Default) {
|
||||
"Some your message for this event"
|
||||
}
|
||||
}
|
||||
|
||||
// Just with message
|
||||
logger.l(LogLevel.INFO, "Some your message for this event", throwable)
|
||||
|
||||
// With message and tag as strings
|
||||
logger.l(LogLevel.INFO, tag, "Some your message for this event", throwable)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"Some your message for this event"</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"Some your message for this event"</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>
|
||||
</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>
|
||||
</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">"Some your message for this event"</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>
|
||||
</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>
|
||||
</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">"Some your message for this event"</span><span class="p">,</span><span class="w"> </span><span class="n">throwable</span><span class="p">)</span>
|
||||
</span></code></pre></div>
|
||||
|
||||
|
||||
|
||||
|
114
kslog/setup.html
114
kslog/setup.html
@ -1394,30 +1394,30 @@
|
||||
<h2 id="dependency-installation">Dependency installation<a class="headerlink" href="#dependency-installation" title="Permanent link">¶</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>
|
||||
<h3 id="gradle-groovy">Gradle (Groovy)<a class="headerlink" href="#gradle-groovy" title="Permanent link">¶</a></h3>
|
||||
<pre><code class="language-groovy">implementation "dev.inmo:kslog:$kslog_version"
|
||||
</code></pre>
|
||||
<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">"dev.inmo:kslog:$kslog_version"</span>
|
||||
</span></code></pre></div>
|
||||
<h3 id="gradle-kotlin-script">Gradle (Kotlin Script)<a class="headerlink" href="#gradle-kotlin-script" title="Permanent link">¶</a></h3>
|
||||
<pre><code class="language-kotlin">implementation("dev.inmo:kslog:$kslog_version")
|
||||
</code></pre>
|
||||
<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">"dev.inmo:kslog:</span><span class="si">$</span><span class="n">kslog_version</span><span class="s">"</span><span class="p">)</span>
|
||||
</span></code></pre></div>
|
||||
<h3 id="maven-pom">Maven (pom)<a class="headerlink" href="#maven-pom" title="Permanent link">¶</a></h3>
|
||||
<pre><code class="language-xml"><dependency>
|
||||
<groupId>dev.inmo</groupId>
|
||||
<artifactId>kslog</artifactId>
|
||||
<version>${kslog_version}</version>
|
||||
</dependency>
|
||||
</code></pre>
|
||||
<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"><dependency></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="nt"><groupId></span>dev.inmo<span class="nt"></groupId></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="nt"><artifactId></span>kslog<span class="nt"></artifactId></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="nt"><version></span>${kslog_version}<span class="nt"></version></span>
|
||||
</span><span id="__span-2-5"><a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a><span class="nt"></dependency></span>
|
||||
</span></code></pre></div>
|
||||
<h2 id="setup-in-code">Setup in code<a class="headerlink" href="#setup-in-code" title="Permanent link">¶</a></h2>
|
||||
<p>The main point in setup in your code is to setup default logger:</p>
|
||||
<pre><code class="language-kotlin">KSLog.default = KSLog("defaultTag")
|
||||
</code></pre>
|
||||
<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">"defaultTag"</span><span class="p">)</span>
|
||||
</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>
|
||||
<pre><code class="language-kotlin">KSLog(
|
||||
"loggingWithCustomFormat",
|
||||
messageFormatter = { level, tag, message, throwable ->
|
||||
println("[$level] $tag - $message: $throwable")
|
||||
}
|
||||
)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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">"loggingWithCustomFormat"</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">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">-></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="n">println</span><span class="p">(</span><span class="s">"[</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">"</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>
|
||||
</span></code></pre></div>
|
||||
<p>Additionally you may use one of several different settings:</p>
|
||||
<ul>
|
||||
<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>
|
||||
</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>
|
||||
<pre><code class="language-kotlin">val logger = KSLog(
|
||||
"exampleTag",
|
||||
minLoggingLevel = LogLevel.INFO
|
||||
)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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">"exampleTag"</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><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>
|
||||
</span></code></pre></div>
|
||||
<p>The next levels will be logged with <code>logger</code>:</p>
|
||||
<ul>
|
||||
<li><code>INFO</code></li>
|
||||
@ -1446,55 +1446,55 @@
|
||||
<h2 id="special-loggers">Special loggers<a class="headerlink" href="#special-loggers" title="Permanent link">¶</a></h2>
|
||||
<h3 id="callbackkslog">CallbackKSLog<a class="headerlink" href="#callbackkslog" title="Permanent link">¶</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>
|
||||
<pre><code class="language-kotlin">KSLog { level, tag, message, throwable ->
|
||||
println("[$level] $tag - $message: $throwable")
|
||||
}
|
||||
</code></pre>
|
||||
<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">-></span>
|
||||
</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">"[</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">"</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>
|
||||
</span></code></pre></div>
|
||||
<h3 id="taglogger">TagLogger<a class="headerlink" href="#taglogger" title="Permanent link">¶</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>
|
||||
<pre><code class="language-kotlin">val logger = TagLogger("tagLoggerTag")
|
||||
</code></pre>
|
||||
<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">"tagLoggerTag"</span><span class="p">)</span>
|
||||
</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>
|
||||
<h3 id="filterkslog">FilterKSLog<a class="headerlink" href="#filterkslog" title="Permanent link">¶</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>
|
||||
<pre><code class="language-kotlin">val baseLogger = KSLog("base") // log everything with the tag `base` if not set other
|
||||
val filtered = baseLogger.filtered { _, t, _ ->
|
||||
t == "base"
|
||||
}
|
||||
</code></pre>
|
||||
<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">"base"</span><span class="p">)</span><span class="w"> </span><span class="c1">// log everything with the tag `base` if not set other</span>
|
||||
</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">-></span>
|
||||
</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">"base"</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>
|
||||
</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>
|
||||
<h3 id="typedkslog">TypedKSLog<a class="headerlink" href="#typedkslog" title="Permanent link">¶</a></h3>
|
||||
<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("base") // log everything with the tag `base` if not set other
|
||||
val typed = buildTypedLogger {
|
||||
on<Int>(baseLogger) // log all ints to the baseLogger
|
||||
on<Float> { _, _, message, _ ->// log all floats to the passed logger
|
||||
println(message.toString()) // just print all floats
|
||||
}
|
||||
default { level, tag, message, throwable ->
|
||||
KSLog.performLog(level, tag, message, throwable)
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<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">"base"</span><span class="p">)</span><span class="w"> </span><span class="c1">// log everything with the tag `base` if not set other</span>
|
||||
</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>
|
||||
</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"><</span><span class="kt">Int</span><span class="o">></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>
|
||||
</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"><</span><span class="kt">Float</span><span class="o">></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">-></span><span class="c1">// log all floats to the passed logger</span>
|
||||
</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>
|
||||
</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">-></span>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
<h3 id="automatical-loggers">Automatical loggers<a class="headerlink" href="#automatical-loggers" title="Permanent link">¶</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>
|
||||
<pre><code class="language-kotlin">class SomeClass {
|
||||
init {
|
||||
logger.i("inited")
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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">"inited"</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>
|
||||
</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>
|
||||
<ul>
|
||||
<li>Extension <code>logger</code></li>
|
||||
</ul>
|
||||
<h2 id="jvm-specific-setup">JVM specific setup<a class="headerlink" href="#jvm-specific-setup" title="Permanent link">¶</a></h2>
|
||||
<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(
|
||||
"yourTag"
|
||||
Logger.getLogger("YourJavaLoggerName")
|
||||
)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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">"yourTag"</span>
|
||||
</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">"YourJavaLoggerName"</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>
|
||||
</span></code></pre></div>
|
||||
|
||||
|
||||
|
||||
|
File diff suppressed because one or more lines are too long
BIN
sitemap.xml.gz
BIN
sitemap.xml.gz
Binary file not shown.
@ -1189,55 +1189,55 @@
|
||||
</tbody>
|
||||
</table>
|
||||
<p>Low-level way to create keyboard looks like in the next snippet:</p>
|
||||
<pre><code class="language-kotlin">ReplyKeyboardMarkup(
|
||||
matrix {
|
||||
row {
|
||||
add(SimpleKeyboardButton("Simple text"))
|
||||
// ...
|
||||
}
|
||||
// ...
|
||||
}
|
||||
)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"Simple text"</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>
|
||||
</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>
|
||||
<pre><code class="language-kotlin">// reply keyboard
|
||||
replyKeyboard {
|
||||
row {
|
||||
simpleButton("7")
|
||||
simpleButton("8")
|
||||
simpleButton("9")
|
||||
simpleButton("*")
|
||||
}
|
||||
row {
|
||||
simpleButton("4")
|
||||
simpleButton("5")
|
||||
simpleButton("6")
|
||||
simpleButton("/")
|
||||
}
|
||||
row {
|
||||
simpleButton("1")
|
||||
simpleButton("2")
|
||||
simpleButton("3")
|
||||
simpleButton("-")
|
||||
}
|
||||
row {
|
||||
simpleButton("0")
|
||||
simpleButton(".")
|
||||
simpleButton("=")
|
||||
simpleButton("+")
|
||||
}
|
||||
}
|
||||
|
||||
// inline keyboard
|
||||
inlineKeyboard {
|
||||
row {
|
||||
dataButton("Get random music", "random")
|
||||
}
|
||||
row {
|
||||
urlButton("Send music to friends", "https://some.link")
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"7"</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="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">"8"</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="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">"9"</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="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">"*"</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>
|
||||
</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>
|
||||
</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">"4"</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="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">"5"</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="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">"6"</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="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">"/"</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>
|
||||
</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>
|
||||
</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">"1"</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><span class="w"> </span><span class="n">simpleButton</span><span class="p">(</span><span class="s">"2"</span><span class="p">)</span>
|
||||
</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">"3"</span><span class="p">)</span>
|
||||
</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">"-"</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>
|
||||
</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>
|
||||
</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">"0"</span><span class="p">)</span>
|
||||
</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">"."</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="n">simpleButton</span><span class="p">(</span><span class="s">"="</span><span class="p">)</span>
|
||||
</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">"+"</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"Get random music"</span><span class="p">,</span><span class="w"> </span><span class="s">"random"</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>
|
||||
</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>
|
||||
</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">"Send music to friends"</span><span class="p">,</span><span class="w"> </span><span class="s">"https://some.link"</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>
|
||||
</span></code></pre></div>
|
||||
|
||||
|
||||
|
||||
|
@ -1268,49 +1268,45 @@
|
||||
<h2 id="handlelivelocation">handleLiveLocation<a class="headerlink" href="#handlelivelocation" title="Permanent link">¶</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>
|
||||
<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">"Cancel"</span><span class="p">,</span><span class="w"> </span><span class="s">"cancel"</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"><</span><span class="n">ContentMessage</span><span class="o"><</span><span class="n">LocationContent</span><span class="o">>?></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>
|
||||
<pre><code class="language-kotlin">flow {
|
||||
var i = 0
|
||||
while (isActive) {
|
||||
val newInfo = EditLiveLocationInfo(
|
||||
latitude = i.toDouble(),
|
||||
longitude = i.toDouble(),
|
||||
replyMarkup = flatInlineKeyboard {
|
||||
dataButton("Cancel", "cancel")
|
||||
}
|
||||
)
|
||||
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<ContentMessage<LocationContent>?>(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>
|
||||
|
||||
|
||||
|
@ -1170,21 +1170,21 @@
|
||||
|
||||
<h1 id="text">Text<a class="headerlink" href="#text" title="Permanent link">¶</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>
|
||||
<pre><code class="language-kotlin">val sources = "Regular start of text " + bold("with bold part") + italic("and italic ending")
|
||||
</code></pre>
|
||||
<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">"Regular start of text "</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">"with bold part"</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">"and italic ending"</span><span class="p">)</span>
|
||||
</span></code></pre></div>
|
||||
<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() }
|
||||
buildEntities(" ") {// optional " " auto separator which will be pasted between text sources
|
||||
+"It is regular start too" + bold("it is bold as well")
|
||||
items.forEachIndexed { i, item ->
|
||||
if (i % 2) {
|
||||
italic(item)
|
||||
} else {
|
||||
strikethrough(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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">" "</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="c1">// optional " " auto separator which will be pasted between text sources</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="o">+</span><span class="s">"It is regular start too"</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">"it is bold as well"</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="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">-></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="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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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 “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~~”.</p>
|
||||
|
||||
|
||||
|
@ -1222,12 +1222,12 @@ A lot of examples with using of Telegram Bot API you can find in <a href="https:
|
||||
</div>
|
||||
<h3 id="the-most-simple-bot">The most simple bot<a class="headerlink" href="#the-most-simple-bot" title="Permanent link">¶</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>
|
||||
<pre><code class="language-kotlin">suspend fun main(vararg args: String) {
|
||||
val botToken = args.first()
|
||||
val bot = telegramBot(botToken)
|
||||
println(bot.getMe())
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
<p>So, let’s get understanding, about what is going on:</p>
|
||||
<ol>
|
||||
<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>
|
||||
</ol>
|
||||
<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)
|
||||
</code></pre>
|
||||
<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>
|
||||
</span></code></pre></div>
|
||||
|
||||
|
||||
|
||||
|
@ -1516,38 +1516,34 @@
|
||||
<h3 id="notice-about-repository">Notice about repository<a class="headerlink" href="#notice-about-repository" title="Permanent link">¶</a></h3>
|
||||
<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">¶</a></h6>
|
||||
<pre><code class="language-groovy">mavenCentral()
|
||||
</code></pre>
|
||||
<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>
|
||||
</span></code></pre></div>
|
||||
<h6 id="pomxml">pom.xml<a class="headerlink" href="#pomxml" title="Permanent link">¶</a></h6>
|
||||
<pre><code class="language-xml"><repository>
|
||||
<id>central</id>
|
||||
<name>mavenCentral</name>
|
||||
<url>https://repo1.maven.org/maven2</url>
|
||||
</repository>
|
||||
</code></pre>
|
||||
<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"><repository></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="nt"><id></span>central<span class="nt"></id></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="nt"><name></span>mavenCentral<span class="nt"></name></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="nt"><url></span>https://repo1.maven.org/maven2<span class="nt"></url></span>
|
||||
</span><span id="__span-1-5"><a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="nt"></repository></span>
|
||||
</span></code></pre></div>
|
||||
<h6 id="dev-channel">Dev channel<a class="headerlink" href="#dev-channel" title="Permanent link">¶</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>
|
||||
<details><summary>Gradle</summary>
|
||||
|
||||
|
||||
<pre><code class="language-groovy">maven {
|
||||
url "https://git.inmo.dev/api/packages/InsanusMokrassar/maven"
|
||||
}
|
||||
</code></pre>
|
||||
|
||||
<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>
|
||||
</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">"https://git.inmo.dev/api/packages/InsanusMokrassar/maven"</span>
|
||||
</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>
|
||||
|
||||
</details>
|
||||
|
||||
<details><summary>Maven</summary>
|
||||
|
||||
|
||||
<pre><code class="language-xml"><repository>
|
||||
<id>dev.inmo</id>
|
||||
<name>InmoDev</name>
|
||||
<url>https://git.inmo.dev/api/packages/InsanusMokrassar/maven</url>
|
||||
</repository>
|
||||
</code></pre>
|
||||
|
||||
<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"><repository></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="nt"><id></span>dev.inmo<span class="nt"></id></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="nt"><name></span>InmoDev<span class="nt"></name></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="nt"><url></span>https://git.inmo.dev/api/packages/InsanusMokrassar/maven<span class="nt"></url></span>
|
||||
</span><span id="__span-3-5"><a id="__codelineno-3-5" name="__codelineno-3-5" href="#__codelineno-3-5"></a><span class="nt"></repository></span>
|
||||
</span></code></pre></div>
|
||||
|
||||
</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><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">¶</a></h6>
|
||||
<pre><code class="language-groovy">implementation "dev.inmo:tgbotapi:$tgbotapi_version"
|
||||
</code></pre>
|
||||
<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">"dev.inmo:tgbotapi:$tgbotapi_version"</span>
|
||||
</span></code></pre></div>
|
||||
<h6 id="pomxml_1">pom.xml<a class="headerlink" href="#pomxml_1" title="Permanent link">¶</a></h6>
|
||||
<pre><code class="language-xml"><dependency>
|
||||
<groupId>dev.inmo</groupId>
|
||||
<artifactId>tgbotapi</artifactId>
|
||||
<version>${tgbotapi_version}</version>
|
||||
</dependency>
|
||||
</code></pre>
|
||||
<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"><dependency></span>
|
||||
</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"><groupId></span>dev.inmo<span class="nt"></groupId></span>
|
||||
</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"><artifactId></span>tgbotapi<span class="nt"></artifactId></span>
|
||||
</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"><version></span>${tgbotapi_version}<span class="nt"></version></span>
|
||||
</span><span id="__span-5-5"><a id="__codelineno-5-5" name="__codelineno-5-5" href="#__codelineno-5-5"></a><span class="nt"></dependency></span>
|
||||
</span></code></pre></div>
|
||||
<h3 id="telegrambotapi-core">TelegramBotAPI Core<a class="headerlink" href="#telegrambotapi-core" title="Permanent link">¶</a></h3>
|
||||
<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>
|
||||
<h6 id="buildgradle_2">build.gradle<a class="headerlink" href="#buildgradle_2" title="Permanent link">¶</a></h6>
|
||||
<pre><code class="language-groovy">implementation "dev.inmo:tgbotapi.core:$tgbotapi_version"
|
||||
</code></pre>
|
||||
<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">"dev.inmo:tgbotapi.core:$tgbotapi_version"</span>
|
||||
</span></code></pre></div>
|
||||
<h6 id="pomxml_2">pom.xml<a class="headerlink" href="#pomxml_2" title="Permanent link">¶</a></h6>
|
||||
<pre><code class="language-xml"><dependency>
|
||||
<groupId>dev.inmo</groupId>
|
||||
<artifactId>tgbotapi.core</artifactId>
|
||||
<version>${tgbotapi_version}</version>
|
||||
</dependency>
|
||||
</code></pre>
|
||||
<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"><dependency></span>
|
||||
</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"><groupId></span>dev.inmo<span class="nt"></groupId></span>
|
||||
</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"><artifactId></span>tgbotapi.core<span class="nt"></artifactId></span>
|
||||
</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"><version></span>${tgbotapi_version}<span class="nt"></version></span>
|
||||
</span><span id="__span-7-5"><a id="__codelineno-7-5" name="__codelineno-7-5" href="#__codelineno-7-5"></a><span class="nt"></dependency></span>
|
||||
</span></code></pre></div>
|
||||
<h3 id="telegrambotapi-api-extensions">TelegramBotAPI API Extensions<a class="headerlink" href="#telegrambotapi-api-extensions" title="Permanent link">¶</a></h3>
|
||||
<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>
|
||||
<h6 id="buildgradle_3">build.gradle<a class="headerlink" href="#buildgradle_3" title="Permanent link">¶</a></h6>
|
||||
<pre><code class="language-groovy">implementation "dev.inmo:tgbotapi.api:$tgbotapi_version"
|
||||
</code></pre>
|
||||
<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">"dev.inmo:tgbotapi.api:$tgbotapi_version"</span>
|
||||
</span></code></pre></div>
|
||||
<h6 id="pomxml_3">pom.xml<a class="headerlink" href="#pomxml_3" title="Permanent link">¶</a></h6>
|
||||
<pre><code class="language-xml"><dependency>
|
||||
<groupId>dev.inmo</groupId>
|
||||
<artifactId>tgbotapi.api</artifactId>
|
||||
<version>${tgbotapi_version}</version>
|
||||
</dependency>
|
||||
</code></pre>
|
||||
<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"><dependency></span>
|
||||
</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"><groupId></span>dev.inmo<span class="nt"></groupId></span>
|
||||
</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"><artifactId></span>tgbotapi.api<span class="nt"></artifactId></span>
|
||||
</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"><version></span>${tgbotapi_version}<span class="nt"></version></span>
|
||||
</span><span id="__span-9-5"><a id="__codelineno-9-5" name="__codelineno-9-5" href="#__codelineno-9-5"></a><span class="nt"></dependency></span>
|
||||
</span></code></pre></div>
|
||||
<h3 id="telegrambotapi-utils-extensions">TelegramBotAPI Utils Extensions<a class="headerlink" href="#telegrambotapi-utils-extensions" title="Permanent link">¶</a></h3>
|
||||
<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>
|
||||
<h6 id="buildgradle_4">build.gradle<a class="headerlink" href="#buildgradle_4" title="Permanent link">¶</a></h6>
|
||||
<pre><code class="language-groovy">implementation "dev.inmo:tgbotapi.utils:$tgbotapi_version"
|
||||
</code></pre>
|
||||
<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">"dev.inmo:tgbotapi.utils:$tgbotapi_version"</span>
|
||||
</span></code></pre></div>
|
||||
<h6 id="pomxml_4">pom.xml<a class="headerlink" href="#pomxml_4" title="Permanent link">¶</a></h6>
|
||||
<pre><code class="language-xml"><dependency>
|
||||
<groupId>dev.inmo</groupId>
|
||||
<artifactId>tgbotapi.utils</artifactId>
|
||||
<version>${tgbotapi_version}</version>
|
||||
</dependency>
|
||||
</code></pre>
|
||||
<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"><dependency></span>
|
||||
</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"><groupId></span>dev.inmo<span class="nt"></groupId></span>
|
||||
</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"><artifactId></span>tgbotapi.utils<span class="nt"></artifactId></span>
|
||||
</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"><version></span>${tgbotapi_version}<span class="nt"></version></span>
|
||||
</span><span id="__span-11-5"><a id="__codelineno-11-5" name="__codelineno-11-5" href="#__codelineno-11-5"></a><span class="nt"></dependency></span>
|
||||
</span></code></pre></div>
|
||||
<h2 id="next-steps">Next steps<a class="headerlink" href="#next-steps" title="Permanent link">¶</a></h2>
|
||||
<ul>
|
||||
<li><a href="proxy-setup.html">Proxy setup</a></li>
|
||||
|
@ -1230,20 +1230,20 @@
|
||||
|
||||
<h1 id="proxy-setup">Proxy setup<a class="headerlink" href="#proxy-setup" title="Permanent link">¶</a></h1>
|
||||
<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 "main" java.net.ConnectException: Connection refused
|
||||
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
|
||||
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
|
||||
at io.ktor.network.sockets.SocketImpl.connect$ktor_network(SocketImpl.kt:36)
|
||||
at io.ktor.network.sockets.SocketImpl$connect$1.invokeSuspend(SocketImpl.kt)
|
||||
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
|
||||
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
|
||||
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
|
||||
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)
|
||||
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
|
||||
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
|
||||
|
||||
Process finished with exit code 1
|
||||
</code></pre>
|
||||
<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">"main"</span><span class="w"> </span>java.net.ConnectException:<span class="w"> </span>Connection<span class="w"> </span>refused
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
<p>There are several ways to solve this problem:</p>
|
||||
<ul>
|
||||
<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">¶</a></h3>
|
||||
<p>First of all, you will need to use one more library:</p>
|
||||
<p><strong>build.gradle</strong>:</p>
|
||||
<pre><code class="language-groovy">implementation "io.ktor:ktor-client-okhttp:2.0.1"
|
||||
</code></pre>
|
||||
<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">"io.ktor:ktor-client-okhttp:2.0.1"</span>
|
||||
</span></code></pre></div>
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">Dependency note</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>
|
||||
</div>
|
||||
<p>For configuring proxy for your bot inside your program, you can use next snippet:</p>
|
||||
<pre><code class="language-kotlin">val botToken = "HERE MUST BE YOUR TOKEN"
|
||||
val bot = telegramBot(botToken) {
|
||||
ktorClientEngineFactory = OkHttp
|
||||
proxy = ProxyBuilder.socks("127.0.0.1", 1080)
|
||||
}
|
||||
</code></pre>
|
||||
<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">"HERE MUST BE YOUR TOKEN"</span>
|
||||
</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>
|
||||
</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>
|
||||
</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">"127.0.0.1"</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>
|
||||
</span></code></pre></div>
|
||||
<p>Explanation line by line:</p>
|
||||
<ol>
|
||||
<li><code>val botToken = "HERE MUST BE YOUR TOKEN"</code> - here we are just creating variable <code>botToken</code></li>
|
||||
|
@ -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>
|
||||
<h2 id="bot-builder">Bot builder<a class="headerlink" href="#bot-builder" title="Permanent link">¶</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>
|
||||
<pre><code class="language-kotlin">buildBot(
|
||||
"TOKEN"
|
||||
) {
|
||||
proxy = ProxyBuilder.socks(host = "127.0.0.1", port = 4001) // just an example, more info on https://ktor.io/docs/proxy.html
|
||||
ktorClientConfig = {
|
||||
// configuring of ktor client
|
||||
}
|
||||
ktorClientEngineFactory = {
|
||||
// configuring of ktor client engine
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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">"TOKEN"</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>
|
||||
</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">"127.0.0.1"</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
<h2 id="downloading-of-files">Downloading of files<a class="headerlink" href="#downloading-of-files" title="Permanent link">¶</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>
|
||||
<h2 id="live-location">Live location<a class="headerlink" href="#live-location" title="Permanent link">¶</a></h2>
|
||||
|
@ -1242,12 +1242,12 @@
|
||||
<li><code>startChain</code> which will add new state for handling</li>
|
||||
</ul>
|
||||
<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<TrafficLightState> {
|
||||
strictlyOn<SomeState> {
|
||||
// state handling
|
||||
}
|
||||
}.start(CoroutineScope(...)).join()
|
||||
</code></pre>
|
||||
<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"><</span><span class="n">TrafficLightState</span><span class="o">></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="n">strictlyOn</span><span class="o"><</span><span class="n">SomeState</span><span class="o">></span><span class="w"> </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="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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">Full example</p>
|
||||
<p>
|
||||
@ -1273,11 +1273,11 @@ You may find full example of FSM usage in <a href="https://github.com/InsanusMok
|
||||
</li>
|
||||
</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>
|
||||
<pre><code class="language-kotlin">telegramBotWithBehaviourAndFSMAndStartLongPolling<YourStateType>("BOT_TOKEN") {
|
||||
// here you may use any operations from BehaviourBuilder
|
||||
// here you may use any operations from BehaviourContextWithFSMBuilder like strictlyOn and others
|
||||
}
|
||||
</code></pre>
|
||||
<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"><</span><span class="n">YourStateType</span><span class="o">></span><span class="p">(</span><span class="s">"BOT_TOKEN"</span><span class="p">)</span><span class="w"> </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="c1">// here you may use any operations from BehaviourBuilder</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="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>
|
||||
</span></code></pre></div>
|
||||
<h2 id="examples">Examples<a class="headerlink" href="#examples" title="Permanent link">¶</a></h2>
|
||||
<ul>
|
||||
<li><a href="https://github.com/InsanusMokrassar/TelegramBotAPI-examples/blob/master/FSMBot/src/main/kotlin/SimpleFSMBot.kt">TelegramBotAPI-examples/FSMBot</a></li>
|
||||
|
@ -1253,50 +1253,50 @@
|
||||
</ul>
|
||||
<h2 id="initialization">Initialization<a class="headerlink" href="#initialization" title="Permanent link">¶</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’s see an example:</p>
|
||||
<pre><code class="language-kotlin">val bot = telegramBot("TOKEN")
|
||||
|
||||
bot.buildBehaviour {
|
||||
onCommand("start") { // creating of trigger
|
||||
val message = it
|
||||
val content = message.content
|
||||
|
||||
reply(message, "Ok, send me one photo") // send text message with replying on incoming message
|
||||
|
||||
val photoContent = waitPhoto().first() // waitPhoto will return List, so, get first element
|
||||
|
||||
val photo = downloadFile(photoContent) // ByteArray of photo
|
||||
|
||||
// some logic with saving of photos
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<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">"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>
|
||||
</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>
|
||||
</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">"start"</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// creating of trigger</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="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>
|
||||
</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>
|
||||
</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">"Ok, send me one photo"</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
<h2 id="filters">Filters<a class="headerlink" href="#filters" title="Permanent link">¶</a></h2>
|
||||
<p>In most cases there are opportunity to filter some of messages before starting of main logic. Let’s look at this using the example above:</p>
|
||||
<pre><code class="language-kotlin">val bot = telegramBot("TOKEN")
|
||||
|
||||
bot.buildBehaviour {
|
||||
onCommand(
|
||||
"start",
|
||||
initialFilter = {
|
||||
it.content.textSources.size == 1 // make sure that user has sent /start without any additions
|
||||
}
|
||||
) {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<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">"TOKEN"</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><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>
|
||||
</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>
|
||||
</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">"start"</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="w"> </span><span class="n">initialFilter</span><span class="w"> </span><span class="o">=</span><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="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>
|
||||
</span></code></pre></div>
|
||||
<p>OR</p>
|
||||
<pre><code class="language-kotlin">val bot = telegramBot("TOKEN")
|
||||
|
||||
bot.buildBehaviour {
|
||||
onCommand(
|
||||
"start",
|
||||
requireOnlyCommandInMessage = true // it is default, but you can overwrite it with `requireOnlyCommandInMessage = false`
|
||||
) {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<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">"TOKEN"</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><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>
|
||||
</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>
|
||||
</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">"start"</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="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>
|
||||
</span></code></pre></div>
|
||||
|
||||
|
||||
|
||||
|
@ -1292,73 +1292,71 @@
|
||||
</ul>
|
||||
<h3 id="catching-with-result">Catching with result<a class="headerlink" href="#catching-with-result" title="Permanent link">¶</a></h3>
|
||||
<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 {
|
||||
// do something
|
||||
}.onSuccess { // will be called if everything is right
|
||||
// handle success
|
||||
}.onFailure { // will be called if something went wrong
|
||||
// handle error
|
||||
it.printStackTrace()
|
||||
}.getOrThrow() // will return value or throw exception
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
<h3 id="catching-with-callback">Catching with callback<a class="headerlink" href="#catching-with-callback" title="Permanent link">¶</a></h3>
|
||||
<p>Also there is more simple (in some cases) way to handle exceptions with callbacks:</p>
|
||||
<pre><code class="language-kotlin">safely(
|
||||
{
|
||||
// handle error
|
||||
it.printStackTrace()
|
||||
null // return value
|
||||
}
|
||||
) {
|
||||
// do something
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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">¶</a></h3>
|
||||
<p>There are two types of handling:</p>
|
||||
<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">"error"</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">"Hi :)"</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">"ok"</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>
|
||||
<pre><code class="language-kotlin">safely(
|
||||
{
|
||||
it.printStackTrace()
|
||||
"error"
|
||||
}
|
||||
) {
|
||||
error("Hi :)") // emulate exception throwing
|
||||
"ok"
|
||||
} // 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">¶</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>
|
||||
<pre><code class="language-kotlin">val bot = telegramBot("TOKEN")
|
||||
|
||||
bot.buildBehaviour (
|
||||
scope = scope,
|
||||
defaultExceptionsHandler = {
|
||||
it.printStackTrace()
|
||||
}
|
||||
) {
|
||||
// ...
|
||||
}
|
||||
</code></pre>
|
||||
<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">"TOKEN"</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><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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
<p>OR</p>
|
||||
<pre><code class="language-kotlin">val bot = telegramBotWithBehaviour (
|
||||
"TOKEN",
|
||||
scope = scope,
|
||||
defaultExceptionsHandler = {
|
||||
it.printStackTrace()
|
||||
}
|
||||
) {
|
||||
// ...
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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">"TOKEN"</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><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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
|
||||
|
||||
|
@ -1183,10 +1183,10 @@
|
||||
</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">¶</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>
|
||||
<pre><code class="language-kotlin">val message: Message;
|
||||
|
||||
val fileId = message.asCommonMessage() ?.withContent<MediaContent>() ?.content ?.media ?.fileId;
|
||||
</code></pre>
|
||||
<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>
|
||||
</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"><</span><span class="n">MediaContent</span><span class="o">></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>
|
||||
</span></code></pre></div>
|
||||
<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<MediaContent>() ?.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">¶</a></h2>
|
||||
@ -1198,61 +1198,61 @@ val fileId = message.asCommonMessage() ?.withContent<MediaContent>() ?.con
|
||||
</ul>
|
||||
<h3 id="downloading-with-api-extensions">Downloading with <code>API</code> extensions<a class="headerlink" href="#downloading-with-api-extensions" title="Permanent link">¶</a></h3>
|
||||
<h4 id="files-jvmandroid">Files (JVM/Android)<a class="headerlink" href="#files-jvmandroid" title="Permanent link">¶</a></h4>
|
||||
<pre><code class="language-kotlin">val bot: TelegramBot;
|
||||
val fileId: FileId;
|
||||
val outputFile: File;
|
||||
|
||||
bot.downloadFile(fileId, outputFile)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>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;
|
||||
val fileId: FileId;
|
||||
|
||||
val tempFile: File = bot.downloadFileToTemp(fileId)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
<h4 id="byte-read-channel">Byte read channel<a class="headerlink" href="#byte-read-channel" title="Permanent link">¶</a></h4>
|
||||
<pre><code class="language-kotlin">val bot: TelegramBot;
|
||||
val fileId: FileId;
|
||||
|
||||
val bytes: ByteReadChannelAllocator = bot.downloadFileStream(fileId)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
<h4 id="byte-read-channel-allocator">Byte read channel allocator<a class="headerlink" href="#byte-read-channel-allocator" title="Permanent link">¶</a></h4>
|
||||
<pre><code class="language-kotlin">val bot: TelegramBot;
|
||||
val fileId: FileId;
|
||||
|
||||
val bytes: ByteReadChannelAllocator = bot.downloadFileStreamAllocator(fileId)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
<h4 id="byte-arrays">Byte arrays<a class="headerlink" href="#byte-arrays" title="Permanent link">¶</a></h4>
|
||||
<pre><code class="language-kotlin">val bot: TelegramBot;
|
||||
val fileId: FileId;
|
||||
|
||||
val bytes: ByteArray = bot.downloadFile(fileId)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
<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">¶</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>
|
||||
<pre><code class="language-kotlin">val bot: TelegramBot;
|
||||
val fileId: FileId;
|
||||
|
||||
val pathedFile: PathedFile = bot.execute(GetFile(fileId))
|
||||
|
||||
val downloadedBytes: ByteArray = bot.execute(DownloadFile(pathedFile.filePath))
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>You may use almost the same way but with byte read channel allocator:</p>
|
||||
<pre><code class="language-kotlin">val bot: TelegramBot;
|
||||
val fileId: FileId;
|
||||
|
||||
val pathedFile: PathedFile = bot.execute(GetFile(fileId))
|
||||
|
||||
val channelAllocator: ByteReadChannelAllocator = bot.execute(DownloadFileStream(pathedFile.filePath))
|
||||
|
||||
val byteReadChannel: ByteReadChannel = channelAllocator()
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
<div class="admonition note">
|
||||
<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>
|
||||
</ul>
|
||||
<p>In most cases, sending via files looks like in the next snippet:</p>
|
||||
<pre><code class="language-kotlin">val file: File;
|
||||
|
||||
bot.sendDocument(chatId, file.asMultipartFile())
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
|
||||
|
||||
|
||||
|
@ -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>
|
||||
<h2 id="how-to-handle-updates">How to handle updates<a class="headerlink" href="#how-to-handle-updates" title="Permanent link">¶</a></h2>
|
||||
<p>As was written above, it will require some request:</p>
|
||||
<pre><code class="language-kotlin">val updates = bot.execute(GetUpdates())
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
<h2 id="what-is-next">What is next?<a class="headerlink" href="#what-is-next" title="Permanent link">¶</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>
|
||||
|
@ -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>
|
||||
</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>
|
||||
<pre><code class="language-kotlin">telegramBotWithBehaviourAndLongPolling(
|
||||
"token"
|
||||
) {
|
||||
onVisualGallery { // it: CommonMessage<MediaGroupContent<VisualMediaGroupPartContent>>
|
||||
it.content // MediaGroupContent<VisualMediaGroupPartContent>
|
||||
it.content.group // List<MediaGroupCollectionContent.PartWrapper<VisualMediaGroupPartContent>>
|
||||
it.content.group.forEach { // it: MediaGroupCollectionContent.PartWrapper<VisualMediaGroupPartContent>
|
||||
it.messageId // source message id for current media group part
|
||||
it.sourceMessage // source message for current media group part
|
||||
it.content // VisualMediaGroupPartContent
|
||||
println(it.content) // will print current content part info
|
||||
}
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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">"token"</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>
|
||||
</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<MediaGroupContent<VisualMediaGroupPartContent>></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="nb">it</span><span class="p">.</span><span class="na">content</span><span class="w"> </span><span class="c1">// MediaGroupContent<VisualMediaGroupPartContent></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="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<MediaGroupCollectionContent.PartWrapper<VisualMediaGroupPartContent>></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="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<VisualMediaGroupPartContent></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="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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
<p><strong>KDocs:</strong></p>
|
||||
<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>
|
||||
|
@ -1247,23 +1247,23 @@
|
||||
<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">¶</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>
|
||||
<pre><code class="language-kotlin">val message: Message;
|
||||
println(message.asContentMessage() ?.content)
|
||||
</code></pre>
|
||||
<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><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>
|
||||
</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>
|
||||
<h2 id="require">Require<a class="headerlink" href="#require" title="Permanent link">¶</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>
|
||||
<pre><code class="language-kotlin">val message: Message;
|
||||
println(message.requireContentMessage().content)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
<h2 id="when">When<a class="headerlink" href="#when" title="Permanent link">¶</a></h2>
|
||||
<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;
|
||||
message.whenContentMessage {
|
||||
println(it.content)
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
|
||||
|
||||
|
@ -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>
|
||||
</ul>
|
||||
<p>Let’s look how it works with the factory above:</p>
|
||||
<pre><code class="language-kotlin">// Step 1 - create filter
|
||||
val filter = flowsUpdatesFilter {
|
||||
// Step 2 - set up handling. In this case we will print any message from group or user in console
|
||||
messageFlow.onEach {
|
||||
println(it)
|
||||
}.launchIn(someCoroutineScope)
|
||||
}
|
||||
|
||||
// Step 3 - passing updates to filter
|
||||
bot.getUpdates().forEach {
|
||||
filter.asUpdatesReceiver(it)
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
<h2 id="long-polling">Long polling<a class="headerlink" href="#long-polling" title="Permanent link">¶</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>
|
||||
<pre><code class="language-kotlin">val bot = telegramBot("TOKEN")
|
||||
|
||||
bot.longPolling {
|
||||
messageFlow.onEach {
|
||||
println(it)
|
||||
}.launchIn(someCoroutineScope)
|
||||
}.join()
|
||||
</code></pre>
|
||||
<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">"TOKEN"</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><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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
<p>This example looks like the example above with three steps, but there are several important things here:</p>
|
||||
<ul>
|
||||
<li>You <strong>do not</strong> manage retrieving of updates by hands</li>
|
||||
|
@ -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>
|
||||
</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">¶</a></h3>
|
||||
<pre><code class="language-kotlin">suspend fun main {
|
||||
// This subroute will be used as random webhook subroute to improve security according to the recommendations of Telegram
|
||||
val subroute = uuid4().toString()
|
||||
// Input/Output coroutines scope more info here: https://kotlinlang.org/docs/coroutines-guide.html
|
||||
val scope = CoroutineScope(Dispatchers.IO)
|
||||
// Here will be automatically created bot and available inside of lambda where you will setup your bot behaviour
|
||||
telegramBotWithBehaviour(
|
||||
// Pass TOKEN inside of your application environment variables
|
||||
System.getenv("TOKEN"),
|
||||
scope = scope
|
||||
) {
|
||||
// Set up webhooks and start to listen them
|
||||
setWebhookInfoAndStartListenWebhooks(
|
||||
// Automatic env which will be passed by heroku to the app
|
||||
System.getenv("PORT").toInt(),
|
||||
// Server engine. More info here: https://ktor.io/docs/engines.html
|
||||
Tomcat,
|
||||
// Pass URL environment variable via settings of application. It must looks like https://<app name>.herokuapp.com
|
||||
SetWebhook("${System.getenv("URL").removeSuffix("/")}/$subroute"),
|
||||
// Just callback which will be called when exceptions will happen inside of webhooks
|
||||
{
|
||||
it.printStackTrace()
|
||||
},
|
||||
// Set up listen requests from outside
|
||||
"0.0.0.0",
|
||||
// Set up subroute to listen webhooks to
|
||||
subroute,
|
||||
// BehaviourContext is the CoroutineScope and it is recommended to pass it inside of webhooks server
|
||||
scope = this,
|
||||
// BehaviourContext is the FlowsUpdatesFilter and it is recommended to pass its asUpdateReceiver as a block to retrieve all the updates
|
||||
block = asUpdateReceiver
|
||||
)
|
||||
// Test reaction on each command with reply and text `Got it`
|
||||
onUnhandledCommand {
|
||||
reply(it, "Got it")
|
||||
}
|
||||
}
|
||||
// Just potentially infinite await of bot completion
|
||||
scope.coroutineContext.job.join()
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"TOKEN"</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="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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"PORT"</span><span class="p">).</span><span class="na">toInt</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="c1">// Server engine. More info here: https://ktor.io/docs/engines.html</span>
|
||||
</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>
|
||||
</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://<app name>.herokuapp.com</span>
|
||||
</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">"</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">"</span><span class="n">URL</span><span class="s">"</span><span class="p">).</span><span class="na">removeSuffix</span><span class="p">(</span><span class="s">"</span><span class="o">/</span><span class="s">"</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">"</span><span class="p">),</span>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"0.0.0.0"</span><span class="p">,</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="c1">// Set up subroute to listen webhooks to</span>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"Got it"</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>
|
||||
</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>
|
||||
</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>
|
||||
</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">¶</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
|
||||
val subroute = uuid4().toString()
|
||||
val bot = telegramBot(TOKEN)
|
||||
val scope = CoroutineScope(Dispatchers.Default)
|
||||
|
||||
val filter = flowsUpdatesFilter {
|
||||
messageFlow.onEach {
|
||||
println(it) // will be printed
|
||||
}.launchIn(scope)
|
||||
}
|
||||
|
||||
val subroute = UUID.randomUUID().toString() // It will be used as subpath for security target as recommended by https://core.telegram.org/bots/api#setwebhook
|
||||
|
||||
val server = bot.setWebhookInfoAndStartListenWebhooks(
|
||||
// Automatic env which will be passed by heroku to the app
|
||||
System.getenv("PORT").toInt(),
|
||||
// Server engine. More info here: https://ktor.io/docs/engines.html
|
||||
Tomcat,
|
||||
// Pass URL environment variable via settings of application. It must looks like https://<app name>.herokuapp.com
|
||||
SetWebhook("${System.getenv("URL").removeSuffix("/")}/$subroute"),
|
||||
// Just callback which will be called when exceptions will happen inside of webhooks
|
||||
{
|
||||
it.printStackTrace()
|
||||
},
|
||||
// Set up listen requests from outside
|
||||
"0.0.0.0",
|
||||
// Set up subroute to listen webhooks to
|
||||
subroute,
|
||||
scope = scope,
|
||||
block = filter.asUpdateReceiver
|
||||
)
|
||||
|
||||
server.environment.connectors.forEach {
|
||||
println(it)
|
||||
}
|
||||
server.start(false)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"PORT"</span><span class="p">).</span><span class="na">toInt</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><span class="w"> </span><span class="c1">// Server engine. More info here: https://ktor.io/docs/engines.html</span>
|
||||
</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>
|
||||
</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://<app name>.herokuapp.com</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="n">SetWebhook</span><span class="p">(</span><span class="s">"</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">"</span><span class="n">URL</span><span class="s">"</span><span class="p">).</span><span class="na">removeSuffix</span><span class="p">(</span><span class="s">"</span><span class="o">/</span><span class="s">"</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">"</span><span class="p">),</span>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"0.0.0.0"</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="w"> </span><span class="c1">// Set up subroute to listen webhooks to</span>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
|
||||
|
||||
|
||||
|
@ -1305,28 +1305,28 @@
|
||||
</ul>
|
||||
<h3 id="longpolling">longPolling<a class="headerlink" href="#longpolling" title="Permanent link">¶</a></h3>
|
||||
<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)
|
||||
bot.longPolling(
|
||||
textMessages().subscribe(scope) { // here "scope" is a CoroutineScope
|
||||
println(it) // will be printed each update from chats with messages
|
||||
}
|
||||
)
|
||||
</code></pre>
|
||||
<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><span class="n">bot</span><span class="p">.</span><span class="na">longPolling</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="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 "scope" is a CoroutineScope</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="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>
|
||||
</span></code></pre></div>
|
||||
<h3 id="startgettingofupdatesbylongpolling">startGettingOfUpdatesByLongPolling<a class="headerlink" href="#startgettingofupdatesbylongpolling" title="Permanent link">¶</a></h3>
|
||||
<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)
|
||||
bot.startGettingOfUpdatesByLongPolling(
|
||||
{
|
||||
println(it) // will be printed each update from chats with messages
|
||||
}
|
||||
)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
<p>The other way is to use the most basic <code>startGettingOfUpdatesByLongPolling</code> extension:</p>
|
||||
<pre><code class="language-kotlin">val bot = telegramBot(token)
|
||||
bot.startGettingOfUpdatesByLongPolling {
|
||||
println(it) // will be printed each update
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
<h2 id="see-also">See also<a class="headerlink" href="#see-also" title="Permanent link">¶</a></h2>
|
||||
<ul>
|
||||
<li><a href="webhooks.html">Webhooks</a></li>
|
||||
|
@ -1319,90 +1319,86 @@
|
||||
<p>Anyway, this filter can’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">¶</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>
|
||||
<pre><code class="language-kotlin">SimpleUpdatesFilter {
|
||||
println(it)
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
<h3 id="flowsupdatesfilter">FlowsUpdatesFilter<a class="headerlink" href="#flowsupdatesfilter" title="Permanent link">¶</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<...></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)
|
||||
flowsUpdatesFilter {
|
||||
messageFlow.onEach {
|
||||
println(it)
|
||||
}.launchIn(scope)
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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><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>
|
||||
</span></code></pre></div>
|
||||
<h4 id="combining-of-flows">Combining of flows<a class="headerlink" href="#combining-of-flows" title="Permanent link">¶</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>
|
||||
<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>
|
||||
<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">¶</a></h4>
|
||||
<p><code>FlowsUpdatesFilter</code> have a lot of extensions for messages types filtering:</p>
|
||||
<pre><code class="language-kotlin">flowsUpdatesFilter {
|
||||
textMessages(scope).onEach {
|
||||
println(it) // will be printed each message from channels and chats both with content only `TextContent`
|
||||
}.launchIn(scope)
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
<p>The same things were created for media groups:</p>
|
||||
<pre><code class="language-kotlin">flowsUpdatesFilter {
|
||||
mediaGroupMessages(scope).onEach {
|
||||
println(it) // will be printed each media group messages list from both channels and chats without filtering of content
|
||||
}.launchIn(scope)
|
||||
|
||||
mediaGroupPhotosMessages(scope).onEach {
|
||||
println(it) // will be printed each media group messages list from both channels and chats with PhotoContent only
|
||||
}.launchIn(scope)
|
||||
|
||||
mediaGroupVideosMessages(scope).onEach {
|
||||
println(it) // will be printed each media group messages list from both channels and chats with VideoContent only
|
||||
}.launchIn(scope)
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
<pre><code class="language-kotlin">flowsUpdatesFilter {
|
||||
sentMessagesWithMediaGroups(scope).onEach {
|
||||
println(it) // will be printed each message including each separated media group message from both channels and chats without filtering of content
|
||||
}.launchIn(scope)
|
||||
|
||||
photoMessagesWithMediaGroups(scope).onEach {
|
||||
println(it) // will be printed each message including each separated media group message from both channels and chats with PhotoContent only
|
||||
}.launchIn(scope)
|
||||
|
||||
videoMessagesWithMediaGroups(scope).onEach {
|
||||
println(it) // will be printed each message including each separated media group message from both channels and chats with VideoContent only
|
||||
}.launchIn(scope)
|
||||
}
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</span></code></pre></div>
|
||||
<h2 id="see-also">See also<a class="headerlink" href="#see-also" title="Permanent link">¶</a></h2>
|
||||
<ul>
|
||||
<li><a href="long-polling.html">Long polling</a></li>
|
||||
|
@ -1266,98 +1266,98 @@
|
||||
</ul>
|
||||
<h3 id="setwebhookinfoandstartlistenwebhooks"><code>setWebhookInfoAndStartListenWebhooks</code><a class="headerlink" href="#setwebhookinfoandstartlistenwebhooks" title="Permanent link">¶</a></h3>
|
||||
<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)
|
||||
|
||||
val filter = flowsUpdatesFilter {
|
||||
// ...
|
||||
}
|
||||
|
||||
bot.setWebhookInfoAndStartListenWebhooks(
|
||||
8080, // listening port. It is required for cases when your server hidden by some proxy or other system like Heroku
|
||||
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
|
||||
SetWebhook(
|
||||
"address.com/webhook_route",
|
||||
File("/path/to/certificate").toInputFile(), // certificate file. More info here: https://core.telegram.org/bots/webhooks#a-certificate-where-do-i-get-one-and-how
|
||||
40, // max allowed updates, by default is null
|
||||
filter.allowedUpdates
|
||||
),
|
||||
{
|
||||
it.printStackTrace() // optional handling of exceptions
|
||||
},
|
||||
"0.0.0.0", // listening host which will be used to bind by server
|
||||
"subroute", // Optional subroute, if null - will listen root of address
|
||||
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
|
||||
"/path/to/keystore.jks",
|
||||
"KeystorePassword",
|
||||
"Keystore key alias name",
|
||||
"KeystoreAliasPassword"
|
||||
),
|
||||
scope, // Kotlin coroutine scope for internal transforming of media groups
|
||||
filter.asUpdateReceiver
|
||||
)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"address.com/webhook_route"</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">File</span><span class="p">(</span><span class="s">"/path/to/certificate"</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"0.0.0.0"</span><span class="p">,</span><span class="w"> </span><span class="c1">// listening host which will be used to bind by server</span>
|
||||
</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">"subroute"</span><span class="p">,</span><span class="w"> </span><span class="c1">// Optional subroute, if null - will listen root of address</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="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>
|
||||
</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">"/path/to/keystore.jks"</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="s">"KeystorePassword"</span><span class="p">,</span>
|
||||
</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">"Keystore key alias name"</span><span class="p">,</span>
|
||||
</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">"KeystoreAliasPassword"</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
<pre><code class="language-kotlin">SetWebhook(
|
||||
"address.com/webhook_route",
|
||||
"some_file_bot_id".toInputFile(),
|
||||
40, // max allowed updates, by default is null
|
||||
filter.allowedUpdates
|
||||
)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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">"address.com/webhook_route"</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="s">"some_file_bot_id"</span><span class="p">.</span><span class="na">toInputFile</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="m">40</span><span class="p">,</span><span class="w"> </span><span class="c1">// max allowed updates, by default is null</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="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>
|
||||
</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>
|
||||
<h3 id="startlistenwebhooks"><code>startListenWebhooks</code><a class="headerlink" href="#startlistenwebhooks" title="Permanent link">¶</a></h3>
|
||||
<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 {
|
||||
// ...
|
||||
}
|
||||
|
||||
startListenWebhooks(
|
||||
8080, // listening port. It is required for cases when your server hidden by some proxy or other system like Heroku
|
||||
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
|
||||
{
|
||||
it.printStackTrace() // optional handling of exceptions
|
||||
},
|
||||
"0.0.0.0", // listening host which will be used to bind by server
|
||||
"subroute", // Optional subroute, if null - will listen root of address
|
||||
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
|
||||
"/path/to/keystore.jks",
|
||||
"KeystorePassword",
|
||||
"Keystore key alias name",
|
||||
"KeystoreAliasPassword"
|
||||
),
|
||||
scope, // Kotlin coroutine scope for internal transforming of media groups
|
||||
filter.asUpdateReceiver
|
||||
)
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"0.0.0.0"</span><span class="p">,</span><span class="w"> </span><span class="c1">// listening host which will be used to bind by server</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="s">"subroute"</span><span class="p">,</span><span class="w"> </span><span class="c1">// Optional subroute, if null - will listen root of address</span>
|
||||
</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>
|
||||
</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">"/path/to/keystore.jks"</span><span class="p">,</span>
|
||||
</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">"KeystorePassword"</span><span class="p">,</span>
|
||||
</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">"Keystore key alias name"</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><span class="w"> </span><span class="s">"KeystoreAliasPassword"</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
<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">¶</a></h3>
|
||||
<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)
|
||||
|
||||
val filter = flowsUpdatesFilter {
|
||||
// ...
|
||||
}
|
||||
|
||||
val environment = applicationEngineEnvironment {
|
||||
module {
|
||||
routing {
|
||||
includeWebhookHandlingInRoute(
|
||||
scope,
|
||||
{
|
||||
it.printStackTrace()
|
||||
},
|
||||
filter.asUpdateReceiver
|
||||
)
|
||||
}
|
||||
}
|
||||
connector {
|
||||
host = "0.0.0.0"
|
||||
port = 8080
|
||||
}
|
||||
}
|
||||
|
||||
embeddedServer(CIO, environment).start(true) // will start server and wait its stoping
|
||||
</code></pre>
|
||||
<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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
</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">"0.0.0.0"</span>
|
||||
</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>
|
||||
</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>
|
||||
</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>
|
||||
<h2 id="see-also">See also<a class="headerlink" href="#see-also" title="Permanent link">¶</a></h2>
|
||||
<ul>
|
||||
|
Loading…
Reference in New Issue
Block a user