mirror of
https://github.com/octoleo/telegram-bot-bash.git
synced 2024-11-25 00:37:34 +00:00
try recover from block in update loop
This commit is contained in:
parent
3496a21d62
commit
3c1ea93168
26
README.html
26
README.html
@ -331,15 +331,16 @@ It features background tasks and interactive chats, and can serve as an interfac
|
||||
<div class="sourceCode" id="cb5"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a> <span class="co"># very simple</span></span>
|
||||
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> <span class="bu">echo</span> <span class="st">"text with variables. PWD=</span><span class="va">$PWD</span><span class="st">"</span></span>
|
||||
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="bu">printf</span> <span class="st">'%s\n'</span> <span class="st">"text with variables. PWD=</span><span class="va">$PWD</span><span class="st">"</span></span>
|
||||
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> <span class="ex">-</span><span class="op">></span> text with variables. PWD=/home/xxx</span>
|
||||
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># more advanced</span></span>
|
||||
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a> <span class="va">FLOAT=</span><span class="st">"1.2346777892864"</span> <span class="va">INTEGER=</span><span class="st">"12345.123"</span></span>
|
||||
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> <span class="bu">echo</span> <span class="st">"text with variabeles. float=</span><span class="va">$FLOAT</span><span class="st">, integer=</span><span class="va">$INTEGER</span><span class="st">, PWD=</span><span class="va">$PWD</span><span class="st">"</span></span>
|
||||
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="ex">-</span><span class="op">></span>text with variables. float=1.2346777892864, integer=12345.123, PWD=/home/xxx</span>
|
||||
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a> <span class="bu">printf</span> <span class="st">"text with variables. float=%.2f, integer=%d, PWD=%s\n"</span> <span class="st">""</span> <span class="st">"</span><span class="va">$INTEGER</span><span class="st">"</span> <span class="st">"</span><span class="va">$PWD</span><span class="st">"</span></span>
|
||||
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a> <span class="ex">-</span><span class="op">></span>text with variables. float=1.23, integer=12345, PWD=/home/xxx</span></code></pre></div>
|
||||
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">printf</span> <span class="st">'text with variables. PWD=%s\n'</span> <span class="st">"</span><span class="va">$PWD</span><span class="st">"</span></span>
|
||||
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> <span class="ex">-</span><span class="op">></span> text with variables. PWD=/home/xxx</span>
|
||||
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a> <span class="co"># more advanced</span></span>
|
||||
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> <span class="va">FLOAT=</span><span class="st">"1.2346777892864"</span> <span class="va">INTEGER=</span><span class="st">"12345.123"</span></span>
|
||||
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="bu">echo</span> <span class="st">"float=</span><span class="va">$FLOAT</span><span class="st">, integer=</span><span class="va">$INTEGER</span><span class="st">, PWD=</span><span class="va">$PWD</span><span class="st">"</span></span>
|
||||
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="ex">-</span><span class="op">></span> float=1.2346777892864, integer=12345.123, PWD=/home/xxx</span>
|
||||
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a> <span class="bu">printf</span> <span class="st">"text with variables. float=%.2f, integer=%d, PWD=%s\n"</span> <span class="st">"</span><span class="va">$FLOAT</span><span class="st">"</span> <span class="st">"</span><span class="va">$INTEGER</span><span class="st">"</span> <span class="st">"</span><span class="va">$PWD</span><span class="st">"</span></span>
|
||||
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a> <span class="ex">-</span><span class="op">></span> float=1.23, integer=12345, PWD=/home/xxx</span></code></pre></div>
|
||||
<h3>Do not use #!/usr/bin/env bash</h3>
|
||||
<p><strong>We stay with /bin/bash shebang, because it's more save from security perspective.</strong></p>
|
||||
<p>Use of a fixed path to the system provided bash makes it harder for attackers or users to place alternative versions of bash and avoids using a possibly broken, mangled or compromised bash executable.</p>
|
||||
@ -386,7 +387,10 @@ It features background tasks and interactive chats, and can serve as an interfac
|
||||
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a><span class="co">#curl: (28) Connection timed out after 10001 milliseconds</span></span>
|
||||
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a><span class="fu">wget</span> <span class="at">-t</span> 1 <span class="at">-T</span> 10 https://api.telegram.org/bot</span>
|
||||
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a><span class="co">#Connecting to api.telegram.org (api.telegram.org)|46.38.243.234|:443... failed: Connection timed out.</span></span></code></pre></div>
|
||||
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a><span class="co">#Connecting to api.telegram.org (api.telegram.org)|46.38.243.234|:443... failed: Connection timed out.</span></span>
|
||||
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a></span>
|
||||
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a><span class="ex">nc</span> <span class="at">-w</span> 2 api.telegram.org 443 <span class="kw">||</span> <span class="bu">echo</span> <span class="st">"your IP seems blocked by telegram"</span></span>
|
||||
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a><span class="co">#your IP seems blocked by telegram</span></span></code></pre></div>
|
||||
<p>Since Version 0.96 bashbot offers the option to recover from broken connections (aka blocked). Therefore you can provide a function named <code>bashbotBlockRecover()</code> in <code>mycommands.sh</code>. If the function exists it is called every time when a broken connection is detected.</p>
|
||||
<p>Possible actions are: Check if network is working, change IP-Adress or simply wait some time.</p>
|
||||
<p>If everything seems OK return 0 for retry or any non 0 value to give up.</p>
|
||||
@ -400,6 +404,6 @@ It features background tasks and interactive chats, and can serve as an interfac
|
||||
<p>@Gnadelwartz</p>
|
||||
<h2>That's it all guys!</h2>
|
||||
<p>If you feel that there's something missing or if you found a bug, feel free to submit a pull request!</p>
|
||||
<h4>$$VERSION$$ v1.2-0-gc50499c</h4>
|
||||
<h4>$$VERSION$$ v1.2-dev2-15-g3496a21</h4>
|
||||
</body>
|
||||
</html>
|
||||
|
14
README.md
14
README.md
@ -152,15 +152,16 @@ you shouldn't use echo to display it. [Use printf whenever possible](https://uni
|
||||
# very simple
|
||||
echo "text with variables. PWD=$PWD"
|
||||
printf '%s\n' "text with variables. PWD=$PWD"
|
||||
printf 'text with variables. PWD=%s\n' "$PWD"
|
||||
-> text with variables. PWD=/home/xxx
|
||||
|
||||
# more advanced
|
||||
FLOAT="1.2346777892864" INTEGER="12345.123"
|
||||
echo "text with variabeles. float=$FLOAT, integer=$INTEGER, PWD=$PWD"
|
||||
->text with variables. float=1.2346777892864, integer=12345.123, PWD=/home/xxx
|
||||
echo "float=$FLOAT, integer=$INTEGER, PWD=$PWD"
|
||||
-> float=1.2346777892864, integer=12345.123, PWD=/home/xxx
|
||||
|
||||
printf "text with variables. float=%.2f, integer=%d, PWD=%s\n" "" "$INTEGER" "$PWD"
|
||||
->text with variables. float=1.23, integer=12345, PWD=/home/xxx
|
||||
printf "text with variables. float=%.2f, integer=%d, PWD=%s\n" "$FLOAT" "$INTEGER" "$PWD"
|
||||
-> float=1.23, integer=12345, PWD=/home/xxx
|
||||
```
|
||||
|
||||
### Do not use #!/usr/bin/env bash
|
||||
@ -238,6 +239,9 @@ curl -m 10 https://api.telegram.org/bot
|
||||
|
||||
wget -t 1 -T 10 https://api.telegram.org/bot
|
||||
#Connecting to api.telegram.org (api.telegram.org)|46.38.243.234|:443... failed: Connection timed out.
|
||||
|
||||
nc -w 2 api.telegram.org 443 || echo "your IP seems blocked by telegram"
|
||||
#your IP seems blocked by telegram
|
||||
```
|
||||
|
||||
Since Version 0.96 bashbot offers the option to recover from broken connections (aka blocked). Therefore you can provide a function
|
||||
@ -265,4 +269,4 @@ bashbotBlockRecover() {
|
||||
|
||||
If you feel that there's something missing or if you found a bug, feel free to submit a pull request!
|
||||
|
||||
#### $$VERSION$$ v1.2-0-gc50499c
|
||||
#### $$VERSION$$ v1.2-dev2-15-g3496a21
|
||||
|
16
README.txt
16
README.txt
@ -204,16 +204,17 @@ possible](https://unix.stackexchange.com/a/6581)
|
||||
# very simple
|
||||
echo "text with variables. PWD=$PWD"
|
||||
printf '%s\n' "text with variables. PWD=$PWD"
|
||||
printf 'text with variables. PWD=%s\n' "$PWD"
|
||||
-> text with variables. PWD=/home/xxx
|
||||
|
||||
# more advanced
|
||||
FLOAT="1.2346777892864" INTEGER="12345.123"
|
||||
echo "text with variabeles. float=$FLOAT, integer=$INTEGER, PWD=$PWD"
|
||||
->text with variables. float=1.2346777892864, integer=12345.123, PWD=/home/xxx
|
||||
echo "float=$FLOAT, integer=$INTEGER, PWD=$PWD"
|
||||
-> float=1.2346777892864, integer=12345.123, PWD=/home/xxx
|
||||
|
||||
printf "text with variables. float=%.2f, integer=%d, PWD=%s\n" "" "$INTEGER"
|
||||
"$PWD"
|
||||
->text with variables. float=1.23, integer=12345, PWD=/home/xxx
|
||||
printf "text with variables. float=%.2f, integer=%d, PWD=%s\n" "$FLOAT"
|
||||
"$INTEGER" "$PWD"
|
||||
-> float=1.23, integer=12345, PWD=/home/xxx
|
||||
```
|
||||
|
||||
### Do not use #!/usr/bin/env bash
|
||||
@ -323,6 +324,9 @@ curl -m 10 https://api.telegram.org/bot
|
||||
wget -t 1 -T 10 https://api.telegram.org/bot
|
||||
#Connecting to api.telegram.org (api.telegram.org)|46.38.243.234|:443...
|
||||
failed: Connection timed out.
|
||||
|
||||
nc -w 2 api.telegram.org 443 || echo "your IP seems blocked by telegram"
|
||||
#your IP seems blocked by telegram
|
||||
```
|
||||
|
||||
Since Version 0.96 bashbot offers the option to recover from broken connections
|
||||
@ -355,4 +359,4 @@ wait
|
||||
If you feel that there's something missing or if you found a bug, feel free to
|
||||
submit a pull request!
|
||||
|
||||
#### $$VERSION$$ v1.2-0-gc50499c
|
||||
#### $$VERSION$$ v1.2-dev2-15-g3496a21
|
||||
|
13
bashbot.sh
13
bashbot.sh
@ -11,7 +11,7 @@
|
||||
# This file is public domain in the USA and all free countries.
|
||||
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||
#
|
||||
#### $$VERSION$$ v1.2-dev2-5-gda7a3f1
|
||||
#### $$VERSION$$ v1.2-dev2-15-g3496a21
|
||||
#
|
||||
# Exit Codes:
|
||||
# - 0 success (hopefully)
|
||||
@ -1055,8 +1055,15 @@ start_bot() {
|
||||
else
|
||||
# oops, something bad happened, wait maxsleep*10
|
||||
(( nextsleep=nextsleep*2 , nextsleep= nextsleep>maxsleep*10 ?maxsleep*10:nextsleep ))
|
||||
[ "${OFFSET}" = "-999" ] &&\
|
||||
log_error "Repeated timeout/broken/no connection on telegram update, sleep $(_round_float "${nextsleep}e-3")s"
|
||||
# second time, report problem
|
||||
if [ "${OFFSET}" = "-999" ]; then
|
||||
log_error "Repeated timeout/broken/no connection on telegram update, sleep $(_round_float "${nextsleep}e-3")s"
|
||||
# try to recover
|
||||
if _is_function bashbotBlockRecover && [ -z "$(getJson "${ME_URL}")" ]; then
|
||||
log_error "Try to recover, calling bashbotBlockRecover ..."
|
||||
bashbotBlockRecover
|
||||
fi
|
||||
fi
|
||||
OFFSET="-999"
|
||||
fi
|
||||
done
|
||||
|
@ -223,7 +223,7 @@ send_inline_keyboard "${CHAT[ID]}" "" '[{"text":"b 1", url"":"u 1"}, {"text":"b
|
||||
|
||||
### Edit / Replace Messages
|
||||
|
||||
Edit a message means replace the content of the message in place. The message stay on the same position in the chat ank keep the same
|
||||
Edit a message means replace the content of the message in place. The message stay on the same position in the chat and keep the same
|
||||
message id.
|
||||
|
||||
There is no need to use the same format when replace a message, e.g. a message sent with `send_normal_message` can be replaced with
|
||||
@ -1143,5 +1143,5 @@ The name of your bot is available as bash variable "$ME", there is no need to ca
|
||||
#### [Prev Best Practice](5_practice.md)
|
||||
#### [Next Notes for Developers](7_develop.md)
|
||||
|
||||
#### $$VERSION$$ v1.2-dev2-14-g78ddc1f
|
||||
#### $$VERSION$$ v1.2-dev2-15-g3496a21
|
||||
|
||||
|
@ -9,7 +9,7 @@
|
||||
# #### mycommands.clean
|
||||
#
|
||||
# shellcheck disable=SC1117
|
||||
#### $$VERSION$$ v1.2-dev2-5-gda7a3f1
|
||||
#### $$VERSION$$ v1.2-dev2-15-g3496a21
|
||||
#
|
||||
|
||||
# uncomment the following lines to overwrite info and help messages
|
||||
@ -285,7 +285,9 @@ else
|
||||
# return 0 to retry, return non 0 to give up
|
||||
bashbotBlockRecover() {
|
||||
# place your commands to unblock here, e.g. change IP or simply wait
|
||||
sleep 60 && return 0 # may be temporary
|
||||
sleep 60 # may be temporary
|
||||
# check connection working
|
||||
[ -n "$(getJson "${ME_URL}")" ] && return 0
|
||||
return 1
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user