examples: improvements and printf conversion

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2021-01-01 22:48:13 +01:00
parent a93dc564f1
commit 81e4fa6c81
8 changed files with 215 additions and 55 deletions

View File

@ -6,10 +6,143 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>Bashobot Documentation - examples/README.html</title> <title>Bashobot Documentation - examples/README.html</title>
<style> <style>
html {
line-height: 1.5;
font-family: Georgia, serif;
font-size: 20px;
color: #1a1a1a;
background-color: #fdfdfd;
}
body {
margin: 0 auto;
max-width: 36em;
padding-left: 50px;
padding-right: 50px;
padding-top: 50px;
padding-bottom: 50px;
hyphens: auto;
word-wrap: break-word;
text-rendering: optimizeLegibility;
font-kerning: normal;
}
@media (max-width: 600px) {
body {
font-size: 0.9em;
padding: 1em;
}
}
@media print {
body {
background-color: transparent;
color: black;
font-size: 12pt;
}
p, h2, h3 {
orphans: 3;
widows: 3;
}
h2, h3, h4 {
page-break-after: avoid;
}
}
p {
margin: 1em 0;
}
a {
color: #1a1a1a;
}
a:visited {
color: #1a1a1a;
}
img {
max-width: 100%;
}
h1, h2, h3, h4, h5, h6 {
margin-top: 1.4em;
}
h5, h6 {
font-size: 1em;
font-style: italic;
}
h6 {
font-weight: normal;
}
ol, ul {
padding-left: 1.7em;
margin-top: 1em;
}
li > ol, li > ul {
margin-top: 0;
}
blockquote {
margin: 1em 0 1em 1.7em;
padding-left: 1em;
border-left: 2px solid #e6e6e6;
color: #606060;
}
code {
font-family: Menlo, Monaco, 'Lucida Console', Consolas, monospace;
font-size: 85%;
margin: 0;
}
pre {
margin: 1em 0;
overflow: auto;
}
pre code {
padding: 0;
overflow: visible;
}
.sourceCode {
background-color: transparent;
overflow: visible;
}
hr {
background-color: #1a1a1a;
border: none;
height: 1px;
margin: 1em 0;
}
table {
margin: 1em 0;
border-collapse: collapse;
width: 100%;
overflow-x: auto;
display: block;
font-variant-numeric: lining-nums tabular-nums;
}
table caption {
margin-bottom: 0.75em;
}
tbody {
margin-top: 0.5em;
border-top: 1px solid #1a1a1a;
border-bottom: 1px solid #1a1a1a;
}
th {
border-top: 1px solid #1a1a1a;
padding: 0.25em 0.5em 0.25em 0.5em;
}
td {
padding: 0.125em 0.5em 0.25em 0.5em;
}
header {
margin-bottom: 4em;
text-align: center;
}
#TOC li {
list-style: none;
}
#TOC a:not(:hover) {
text-decoration: none;
}
code{white-space: pre-wrap;} code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;} span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;} span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;} div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
.display.math{display: block; text-align: center; margin: 0.5rem auto;}
</style> </style>
<!--[if lt IE 9]> <!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
@ -35,8 +168,7 @@
run_diskusage.sh - shows disk usage every 100 seconds run_diskusage.sh - shows disk usage every 100 seconds
run_filename.sh - shown the name of new files in a named dir run_filename.sh - shown the name of new files in a named dir
run_filecontent.sh - shown the content of new files in a named dir run_filecontent.sh - shown the content of new files in a named dir
run_notify.sh - same as notify.sh run_notify.sh - same as notify.sh</code></pre>
</code></pre>
<p><strong>Note:</strong> Output of system commands often contains newlines, each newline results in a telegram message, the function 'send_telegram' in mycommands.sh avoids this by converting each newline to ' mynewlinestartshere ' before output the string.</p> <p><strong>Note:</strong> Output of system commands often contains newlines, each newline results in a telegram message, the function 'send_telegram' in mycommands.sh avoids this by converting each newline to ' mynewlinestartshere ' before output the string.</p>
<h3>System Status</h3> <h3>System Status</h3>
<p><strong>send-system-status</strong> contains an example for commands showing status of different subsystems. This example is adapted from https://github.com/RG72/telegram-bot-bash to current bashbot commands, but not fully tested. This will show how easy you can convert existing bots.</p> <p><strong>send-system-status</strong> contains an example for commands showing status of different subsystems. This example is adapted from https://github.com/RG72/telegram-bot-bash to current bashbot commands, but not fully tested. This will show how easy you can convert existing bots.</p>
@ -52,10 +184,9 @@
/lvsd *Datailed lvm status* /lvsd *Datailed lvm status*
/df *disk space* /df *disk space*
/ifconfig *ifconfig output* /ifconfig *ifconfig output*
/smart *sda* _smart status for sda drive_ /smart *sda* _smart status for sda drive_</code></pre>
</code></pre> <h3>jsonDB and Keyboards</h3>
<h3>External scripts</h3> <p><strong>jsonDB-keybords</strong> contains a stripped down real world example from my bot showing the usage of jsonDB to store and retrieve values plus use of keyboards in private chats. It's an extended version of mycommands.sh.dist. Messages and help are in german.</p>
<p><strong>external-use</strong> will contain some examples on how to send messages from external scripts to Telegram chats or users.</p> <h4>$$VERSION$$ v1.20-0-g2ab00a2</h4>
<h4>$$VERSION$$ v0.98-pre2-0-ga597303</h4>
</body> </body>
</html> </html>

View File

@ -56,6 +56,6 @@ convert existing bots.
**jsonDB-keybords** contains a stripped down real world example from my bot showing the usage of jsonDB to store and retrieve values **jsonDB-keybords** contains a stripped down real world example from my bot showing the usage of jsonDB to store and retrieve values
plus use of keyboards in private chats. It's an extended version of mycommands.sh.dist. Messages and help are in german. plus use of keyboards in private chats. It's an extended version of mycommands.sh.dist. Messages and help are in german.
#### $$VERSION$$ v1.20-0-g2ab00a2 #### $$VERSION$$ v1.21-pre-14-ga93dc56

View File

@ -6,7 +6,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# shellcheck disable=SC1117 # shellcheck disable=SC1117
#### $$VERSION$$ v1.20-0-g2ab00a2 #### $$VERSION$$ v1.21-pre-14-ga93dc56
# adjust your language setting here # adjust your language setting here
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment

View File

@ -2,7 +2,7 @@
# file. multibot.sh # file. multibot.sh
# description: run multiple telegram bots from one installation # description: run multiple telegram bots from one installation
# #
#### $$VERSION$$ v1.20-0-g2ab00a2 #### $$VERSION$$ v1.21-pre-14-ga93dc56
if [ "${2}" = "" ] || [ "${2}" = "-h" ]; then if [ "${2}" = "" ] || [ "${2}" = "-h" ]; then
echo "Usage: $0 botname command" echo "Usage: $0 botname command"

View File

@ -7,7 +7,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v1.20-0-g2ab00a2 #### $$VERSION$$ v1.21-pre-14-ga93dc56
SHELL=/bin/sh SHELL=/bin/sh

View File

@ -1,11 +1,18 @@
#!/bin/bash #!/bin/bash
# file: calc.sh ########################################################################
# example for an interactive chat, run with startproc calc.sh #
# File: calc.sh
#
# Description: example for an background job, see mycommands.sh.dist
#
# Usage: runback calc example/calc.sh - or run in terminal
# killback calc - to stop background job
# #
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v1.20-0-g2ab00a2 #### $$VERSION$$ v1.21-pre-14-ga93dc56
########################################################################
###### ######
# parameters # parameters
@ -23,22 +30,21 @@ export 'LANGUAGE=C.UTF-8'
unset IFS unset IFS
# set -f # if you are paranoid use set -f to disable globbing # set -f # if you are paranoid use set -f to disable globbing
echo 'Starting Calculator ...' printf 'Starting Calculator ...\n'
echo 'Enter first number.' printf 'Enter first number.\n'
read -r A <"${INPUT}" read -r A <"${INPUT}"
echo 'Enter second number.' printf 'Enter second number.\n'
read -r B <"${INPUT}" read -r B <"${INPUT}"
echo 'Select Operation: mykeyboardstartshere [ "Addition" , "Subtraction" , "Multiplication" , "Division" , "Cancel" ]' printf 'Select Operation: mykeyboardstartshere [ "Addition" , "Subtraction" , "Multiplication" , "Division" , "Cancel" ]\n'
read -r opt <"${INPUT}" read -r opt <"${INPUT}"
echo -n 'Result: ' printf 'Result: '
case $opt in case ${opt,,} in
'add'* | 'Add'* ) res="$(( A + B ))" ;; 'a'*) res="$(( A + B ))" ;;
'sub'* | 'Sub'* ) res="$(( A - B ))" ;; 's'*) res="$(( A - B ))" ;;
'mul'* | 'Mul'* ) res="$(( A * B ))" ;; 'm'*) res="$(( A * B ))" ;;
'div'* | 'Div'* ) res="$(( A / B ))" ;; 'd'*) res="$(( A / B ))" ;;
'can'* | 'Can'* ) res="abort!" ;; 'c'*) res="abort!" ;;
* ) echo "unknown operator!";; * ) printf "unknown operator!\n";;
esac esac
echo "$res" printf "%s\nBye ...\n" "${res}"
echo "Bye .."

View File

@ -1,17 +1,26 @@
#!/bin/bash #!/bin/bash
# file: notify.sh ########################################################################
# example for an background job, run with startback notify.sh #
# File: notify.sh
#
# Description: example for an background job, see mycommands.sh.dist
#
# Usage: runback notify example/notify.sh [seconds] - or run in terminal
# killback notify - to stop background job
#
# Options: seconds - time to sleep between output, default 10
# #
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#### $$VERSION$$ v1.20-0-g2ab00a2 #
#### $$VERSION$$ v1.21-pre-14-ga93dc56
########################################################################
###### ######
# parameters # parameters
# $1 $2 args as given to starct_proc chat script arg1 arg2 # $1 $2 args as given to starct_proc chat script arg1 arg2
# $3 path to named pipe/log # $3 path to named pipe/log
# adjust your language setting here # adjust your language setting here
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
export 'LC_ALL=C.UTF-8' export 'LC_ALL=C.UTF-8'
@ -24,18 +33,20 @@ unset IFS
# discard STDIN for background jobs! # discard STDIN for background jobs!
cat >/dev/null & cat >/dev/null &
# check if $1 is a number # $1 = time between time notifications
re='^[0-9]+$' # check if $1 is a valid number
if [[ $1 =~ $re ]] ; then if [[ "$1" =~ ^[0-9]+$ ]] ; then
SLEEP="$1" SLEEP="$1"
else else
SLEEP=10 # time between time notifications SLEEP=10
fi fi
# output current time every $1 seconds # output current time every $1 seconds
date "+* It's %k:%M:%S o' clock ..." printf "Output time every %s seconds ...\n" "${SLEEP}"
while sleep $SLEEP
while true
do do
date "+* It's %k:%M:%S o' clock ..." date "+* It's %k:%M:%S o'clock ..."
sleep $SLEEP
done done

View File

@ -1,11 +1,17 @@
#!/bin/bash #!/bin/bash
# file: question.sh ########################################################################
# example for an interactive chat, run with startproc question.sh #
# File: question.sh
#
# Usage: runproc example/question.sh - or run in terminal
#
# Description: example for an interactive chat, see mycommands.sh.dist
# #
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v1.20-0-g2ab00a2 #### $$VERSION$$ v1.21-pre-14-ga93dc56
########################################################################
###### ######
# parameters # parameters
@ -14,7 +20,6 @@
INPUT="${3:-/dev/stdin}" INPUT="${3:-/dev/stdin}"
# adjust your language setting here # adjust your language setting here
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
export 'LC_ALL=C.UTF-8' export 'LC_ALL=C.UTF-8'
@ -24,18 +29,25 @@ export 'LANGUAGE=C.UTF-8'
unset IFS unset IFS
# set -f # if you are paranoid use set -f to disable globbing # set -f # if you are paranoid use set -f to disable globbing
echo "Hi, hello there. # simple yes/no question, defaults to no
Would you like some tea (y/n)?" printf "Hi, hello there\nWould you like some tea (y/n)?\n"
read -r answer <"${INPUT}" read -r answer <"${INPUT}"
[[ $answer =~ ^([yY][eE][sS]|[yY])$ ]] && echo "OK then, here you go: http://www.rivertea.com/blog/wp-content/uploads/2013/12/Green-Tea.jpg" || echo "OK then." if [[ ${answer,,} == "y"* ]]; then
printf "OK then, here you go: http://www.rivertea.com/blog/wp-content/uploads/2013/12/Green-Tea.jpg\n"
else
printf "OK then, no tea ...\n"
fi
# question with Keyboard, repeating until correct answer given
until [ "$SUCCESS" = "y" ] ;do until [ "$SUCCESS" = "y" ] ;do
echo 'Do you like Music? mykeyboardstartshere "Yass!" , "No"' printf 'Do you like Music? mykeyboardstartshere "Yass!" , "No"\n'
read -r answer <"${INPUT}" read -r answer <"${INPUT}"
case $answer in case ${answer,,} in
'Yass!') echo "Goody! mykeyboardendshere";SUCCESS=y;; '') printf "empty answer! Try again\n";;
'No') echo "Well that's weird. mykeyboardendshere";SUCCESS=y;; 'yass'*) printf "Goody! mykeyboardendshere\n";SUCCESS=y;;
'') echo "empty answer! Try again";; 'no'*) printf "Well that's weird. mykeyboardendshere\n";SUCCESS=y;;
*) SUCCESS=n;; *) SUCCESS=n;;
esac esac
done done
printf "OK, Done!\n"