From e0a6d388843f23a9ddf6f164722770144c17a5f4 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 30 Apr 2019 10:40:07 +0200 Subject: [PATCH] fix newlines in added text, add version to all examples --- examples/README.md | 16 +++++++++------- examples/background-scripts/mycommands.sh | 4 +++- examples/background-scripts/run_filecontent.sh | 15 +++++++++------ examples/background-scripts/run_filename.sh | 5 +++-- modules/background.sh | 5 +++-- .../d-send_message-test.input | 2 +- .../d-send_message-test.result | 4 +++- 7 files changed, 31 insertions(+), 20 deletions(-) diff --git a/examples/README.md b/examples/README.md index 317858c..dbd9cc3 100644 --- a/examples/README.md +++ b/examples/README.md @@ -6,30 +6,32 @@ An example crontab is provided in ```examples/bashbot.cron```, see [Expert use](../doc/4_expert.md#Scedule-bashbot-from-Cron) -### Interactive chat examples -Two examples for interactive scripts are provided as 'calc.sh' and 'question.sh', see [Advanced use](../doc/3_advanced.md#Interactive-Chats) +### Interactive chats +Two examples for interactive scripts are provided as **calc.sh** and **question.sh**, see [Advanced use](../doc/3_advanced.md#Interactive-Chats) ### Background jobs Background jobs are an easy way to provide sceduled messages or alerts if something happens. -'notify.sh' shows you a simple examples to send a message ervery x seonds, e.g. actual time. +**notify.sh** is a simple example how to send a message ervery x seonds, e.g. actual time. -'background-scripts' contains a more complex example on how to start and kill many scripts sending messages to a chat. +**background-scripts** contains a more concrete example on how to start and kill diffrent background scripts plus some example backgound scripts. ``` mycommands.sh - /run_xxx and /kill-xxx wil start any script named run_xxx.sh run_diskusage.sh - shows disk usage every 100 seconds - run_filename.sh - shown the namei 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_notify.sh - same as notify.sh ``` +**Note:** Output of system commands often contains newlines, each newline results in a a sepperate telegram message, see function send_telegram in +mycommands.sh on how to avoid this. ### Use bashbot from external scripts -In 'external-use' you will find some examples on how to send messages from external scripts to send messages to Telegram chats or users. +**external-use** will contain some examples on how to send messages from external scripts to Telegram chats or users. -#### $$VERSION$$ v0.7-pre2-1-g4b83377 +#### $$VERSION$$ v0.7-pre2-2-g68afdbf diff --git a/examples/background-scripts/mycommands.sh b/examples/background-scripts/mycommands.sh index 307c9c9..c9f62ae 100644 --- a/examples/background-scripts/mycommands.sh +++ b/examples/background-scripts/mycommands.sh @@ -46,6 +46,7 @@ _is_function() # $1 dir to wtach for new files watch_dir_loop() { local newfile old + [ ! -d "$1" ] && echo "ERROR: no directory $1 found!" >&2 && exit 1 # wait for new files in WATCHDIR inotifywait -q -m "$1" -e create --format "%f" \ | while true @@ -58,12 +59,13 @@ watch_dir_loop() { sleep 0.2 # process content and output message - echo "$(date) found ${newfile}" >&2 + echo "$(date): new file: ${newfile}" >>"$0.log" # note: loop callback must a function in the calling script! if _is_function loop_callback ; then loop_callback "$1/$newfile" else echo "ERROR: loop_callback not found!" >&2 + exit 1 fi done } # 2>>"$0.log" diff --git a/examples/background-scripts/run_filecontent.sh b/examples/background-scripts/run_filecontent.sh index fd9215d..c9d9551 100755 --- a/examples/background-scripts/run_filecontent.sh +++ b/examples/background-scripts/run_filecontent.sh @@ -2,6 +2,7 @@ # file: run_filename # background job to display content of all new files in WATCHDIR # +#### $$VERSION$$ v0.7-pre2-2-g68afdbf # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment @@ -15,18 +16,20 @@ unset IFS # discard STDIN for background jobs! cat >/dev/null & -# watch for new logfiles -WATCHDIR="/my_special/dir_to_watch" +# watch for new files created by a trusted programm +WATCHDIR="/my_trusted/dir_to_watch" source "./mycommands.sh" # test your script and the remove ... -WATCHDIR="/tmp" +WATCHDIR="/tmp/bottest" -# this is calles by watch loop +NEWLINE='mynewlinestartshere' + +# this is called by watch dir loop # $1 is name of the new file loop_callback() { - # output content of file, you MUST trust creator of the file because it contest are sent as message to you! - output_telegram "Contents of ${1}: mynewlinestartshere $(cat "${1}")" + # output content of file, you must trust creator because content is sent as message! + output_telegram "Contents of ${1}: ${NEWLINE} $(cat "${1}")" } watch_dir_loop "$WATCHDIR" diff --git a/examples/background-scripts/run_filename.sh b/examples/background-scripts/run_filename.sh index 53bd926..18a7356 100755 --- a/examples/background-scripts/run_filename.sh +++ b/examples/background-scripts/run_filename.sh @@ -2,6 +2,7 @@ # file: run_filename # background job to display all new files in WATCHDIR # +#### $$VERSION$$ v0.7-pre2-2-g68afdbf # adjust your language setting here # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment @@ -20,9 +21,9 @@ WATCHDIR="/var/log" source "./mycommands.sh" # test your script and the remove ... -WATCHDIR="/tmp" +WATCHDIR="/tmp/bottest" -# this is calles by watch loop +# this is called by watch dir loop # $1 is name of the new file loop_callback() { # output one simple line ... diff --git a/modules/background.sh b/modules/background.sh index e4c3832..e571094 100644 --- a/modules/background.sh +++ b/modules/background.sh @@ -5,7 +5,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.7-pre2-0-g7ce2c5b +#### $$VERSION$$ v0.7-pre2-2-g68afdbf # source from commands.sh if you want ro use interactive or background jobs @@ -17,7 +17,8 @@ export res send_message() { [ "$2" = "" ] && return local text keyboard btext burl no_keyboard file lat long title address sent - text="$(sed <<< "${2}" 's/ *mynewlinestartshere */\r\n/g;s/ mykeyboardend.*//;s/ *my[a-z]\{3,15\}startshere.*//')$(sed <<< "${2}" -n '/mytextstartshere/ s/.*mytextstartshere//p')" + text="$(sed <<< "${2}" 's/ mykeyboardend.*//;s/ *my[kfltab][a-z]\{2,13\}startshere.*//')$(sed <<< "${2}" -n '/mytextstartshere/ s/.*mytextstartshere//p')" + text="$(sed <<< "${text}" 's/ *mynewlinestartshere */\r\n/g')" [ "$3" != "safe" ] && { no_keyboard="$(sed <<< "${2}" '/mykeyboardendshere/!d;s/.*mykeyboardendshere.*/mykeyboardendshere/')" keyboard="$(sed <<< "${2}" '/mykeyboardstartshere /!d;s/.*mykeyboardstartshere *//;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')" diff --git a/test/d-send_message-test/d-send_message-test.input b/test/d-send_message-test/d-send_message-test.input index f00eb65..0a1c6b2 100644 --- a/test/d-send_message-test/d-send_message-test.input +++ b/test/d-send_message-test/d-send_message-test.input @@ -15,4 +15,4 @@ All in one will appear in chat mykeyboardstartshere [ "Yep, sure" , "No, highly # test for new inline button Text plus keyboard will appear in chat mybtextstartshere Button Text myburlstartshere https://www... -STABILO 88/240 Fineliner point 88 mynewlinestartshere mynewlinestartshere [https://images-na.ssl-images-amazon.com/images/I/41oypA3kmHL.l_SX300.jpg] mynewlinestartshere mybtextstartshere Bei Amazon ansehen ... myburlstartshere https://www.amazon.de/dp/B014TN3JYW mytextstartshere second part of text +STABILO 88/240 Fineliner point 88 mynewlinestartshere mynewlinestartshere [https://images-na.ssl-images-amazon.com/images/I/41oypA3kmHL.l_SX300.jpg] mynewlinestartshere mybtextstartshere Bei Amazon ansehen ... myburlstartshere https://www.amazon.de/dp/B014TN3JYW mytextstartshere second part of text mynewlinestartshere plus newline. diff --git a/test/d-send_message-test/d-send_message-test.result b/test/d-send_message-test/d-send_message-test.result index 5ddf545..575d856 100644 --- a/test/d-send_message-test/d-send_message-test.result +++ b/test/d-send_message-test/d-send_message-test.result @@ -54,6 +54,8 @@ URL:https://api.telegram.org/botbashbottestscript/sendMessage chat:123456 JSON:"text":"STABILO 88/240 Fineliner point 88 -[https://images-na.ssl-images-amazon.com/images/I/41oypA3kmHL.l_SX300.jpg] second part of text", "reply_markup": {"inline_keyboard": [ [ {"text":"Bei Amazon ansehen ...", "url":"https://www.amazon.de/dp/B014TN3JYW"}] ]} +[https://images-na.ssl-images-amazon.com/images/I/41oypA3kmHL.l_SX300.jpg] +second part of text +plus newline.", "reply_markup": {"inline_keyboard": [ [ {"text":"Bei Amazon ansehen ...", "url":"https://www.amazon.de/dp/B014TN3JYW"}] ]} URL:https://api.telegram.org/botbashbottestscript/sendMessage