fix newlines in added text, add version to all examples

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-04-30 10:40:07 +02:00
parent 68afdbf3fc
commit e0a6d38884
7 changed files with 31 additions and 20 deletions

View File

@ -6,30 +6,32 @@
An example crontab is provided in ```examples/bashbot.cron```, see [Expert use](../doc/4_expert.md#Scedule-bashbot-from-Cron) An example crontab is provided in ```examples/bashbot.cron```, see [Expert use](../doc/4_expert.md#Scedule-bashbot-from-Cron)
### Interactive chat examples ### Interactive chats
Two examples for interactive scripts are provided as 'calc.sh' and 'question.sh', see [Advanced use](../doc/3_advanced.md#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
Background jobs are an easy way to provide sceduled messages or alerts if something happens. 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 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_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_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
``` ```
**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 ### 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

View File

@ -46,6 +46,7 @@ _is_function()
# $1 dir to wtach for new files # $1 dir to wtach for new files
watch_dir_loop() { watch_dir_loop() {
local newfile old local newfile old
[ ! -d "$1" ] && echo "ERROR: no directory $1 found!" >&2 && exit 1
# wait for new files in WATCHDIR # wait for new files in WATCHDIR
inotifywait -q -m "$1" -e create --format "%f" \ inotifywait -q -m "$1" -e create --format "%f" \
| while true | while true
@ -58,12 +59,13 @@ watch_dir_loop() {
sleep 0.2 sleep 0.2
# process content and output message # 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! # note: loop callback must a function in the calling script!
if _is_function loop_callback ; then if _is_function loop_callback ; then
loop_callback "$1/$newfile" loop_callback "$1/$newfile"
else else
echo "ERROR: loop_callback not found!" >&2 echo "ERROR: loop_callback not found!" >&2
exit 1
fi fi
done done
} # 2>>"$0.log" } # 2>>"$0.log"

View File

@ -2,6 +2,7 @@
# file: run_filename # file: run_filename
# background job to display content of all new files in WATCHDIR # background job to display content of all new files in WATCHDIR
# #
#### $$VERSION$$ v0.7-pre2-2-g68afdbf
# 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
@ -15,18 +16,20 @@ unset IFS
# discard STDIN for background jobs! # discard STDIN for background jobs!
cat >/dev/null & cat >/dev/null &
# watch for new logfiles # watch for new files created by a trusted programm
WATCHDIR="/my_special/dir_to_watch" WATCHDIR="/my_trusted/dir_to_watch"
source "./mycommands.sh" source "./mycommands.sh"
# test your script and the remove ... # 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 # $1 is name of the new file
loop_callback() { loop_callback() {
# output content of file, you MUST trust creator of the file because it contest are sent as message to you! # output content of file, you must trust creator because content is sent as message!
output_telegram "Contents of ${1}: mynewlinestartshere $(cat "${1}")" output_telegram "Contents of ${1}: ${NEWLINE} $(cat "${1}")"
} }
watch_dir_loop "$WATCHDIR" watch_dir_loop "$WATCHDIR"

View File

@ -2,6 +2,7 @@
# file: run_filename # file: run_filename
# background job to display all new files in WATCHDIR # background job to display all new files in WATCHDIR
# #
#### $$VERSION$$ v0.7-pre2-2-g68afdbf
# 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
@ -20,9 +21,9 @@ WATCHDIR="/var/log"
source "./mycommands.sh" source "./mycommands.sh"
# test your script and the remove ... # 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 # $1 is name of the new file
loop_callback() { loop_callback() {
# output one simple line ... # output one simple line ...

View File

@ -5,7 +5,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$$ v0.7-pre2-0-g7ce2c5b #### $$VERSION$$ v0.7-pre2-2-g68afdbf
# source from commands.sh if you want ro use interactive or background jobs # source from commands.sh if you want ro use interactive or background jobs
@ -17,7 +17,8 @@ export res
send_message() { send_message() {
[ "$2" = "" ] && return [ "$2" = "" ] && return
local text keyboard btext burl no_keyboard file lat long title address sent 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" ] && { [ "$3" != "safe" ] && {
no_keyboard="$(sed <<< "${2}" '/mykeyboardendshere/!d;s/.*mykeyboardendshere.*/mykeyboardendshere/')" 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.*//')" keyboard="$(sed <<< "${2}" '/mykeyboardstartshere /!d;s/.*mykeyboardstartshere *//;s/ *my[nkfltab][a-z]\{2,13\}startshere.*//;s/ *mykeyboardendshere.*//')"

View File

@ -15,4 +15,4 @@ All in one will appear in chat mykeyboardstartshere [ "Yep, sure" , "No, highly
# test for new inline button # test for new inline button
Text plus keyboard will appear in chat mybtextstartshere Button Text myburlstartshere https://www... 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.

View File

@ -54,6 +54,8 @@ URL:https://api.telegram.org/botbashbottestscript/sendMessage
chat:123456 JSON:"text":"STABILO 88/240 Fineliner point 88 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 URL:https://api.telegram.org/botbashbottestscript/sendMessage