From 3c1ea93168121431b40041e2f1bb535e9a9a88b6 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 14 Dec 2020 14:00:23 +0100 Subject: [PATCH] try recover from block in update loop --- README.html | 26 +++++++++++++++----------- README.md | 14 +++++++++----- README.txt | 16 ++++++++++------ bashbot.sh | 13 ++++++++++--- doc/6_reference.md | 4 ++-- mycommands.sh | 6 ++++-- 6 files changed, 50 insertions(+), 29 deletions(-) diff --git a/README.html b/README.html index ec005d3..1213c5c 100644 --- a/README.html +++ b/README.html @@ -331,15 +331,16 @@ It features background tasks and interactive chats, and can serve as an interfac
  # very simple
   echo "text with variables. PWD=$PWD"
   printf '%s\n' "text with variables. PWD=$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
-
-  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. PWD=%s\n' "$PWD" + -> text with variables. PWD=/home/xxx + + # more advanced + FLOAT="1.2346777892864" INTEGER="12345.123" + 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" "$FLOAT" "$INTEGER" "$PWD" + -> float=1.23, integer=12345, PWD=/home/xxx

Do not use #!/usr/bin/env bash

We stay with /bin/bash shebang, because it's more save from security perspective.

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.

@@ -386,7 +387,10 @@ It features background tasks and interactive chats, and can serve as an interfac #curl: (28) Connection timed out after 10001 milliseconds 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. +#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 named bashbotBlockRecover() in mycommands.sh. If the function exists it is called every time when a broken connection is detected.

Possible actions are: Check if network is working, change IP-Adress or simply wait some time.

If everything seems OK return 0 for retry or any non 0 value to give up.

@@ -400,6 +404,6 @@ It features background tasks and interactive chats, and can serve as an interfac

@Gnadelwartz

That's it all guys!

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

diff --git a/README.md b/README.md index ea86121..1ce05b6 100644 --- a/README.md +++ b/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 diff --git a/README.txt b/README.txt index 39c1363..832cd85 100644 --- a/README.txt +++ b/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 diff --git a/bashbot.sh b/bashbot.sh index bc964af..b8ab8bf 100755 --- a/bashbot.sh +++ b/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 diff --git a/doc/6_reference.md b/doc/6_reference.md index 9dacc62..7eb05cd 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -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 diff --git a/mycommands.sh b/mycommands.sh index b4778f8..3673d68 100644 --- a/mycommands.sh +++ b/mycommands.sh @@ -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 }