1

I'm working on a script where I monitor the reading and calculation of some data, after executing this script I would like to print the output and send it directly to a telegram channel, but for that I need to execute a command after the awk output.

Messages are arriving on telegram, but they are arriving incomplete (only the first line, in total there are 4 lines per message)

Original output from AWK:

#Price_Notification_ProductABC Price: 0.007023 change: +1.8% Count Number: 6

Exit that is coming on telegram:

#Price_Notification_ProductABC

Could you help me to solve what is wrong? Thanks

Here are my scripts:

awk.sh

#!/bin/bash

awk '
/^#Price_Notification_Product/ {
  prod = $0;
}
/^change: / {
  gsub(/[+%]/,"",$2);
  products[prod] += $2;
  $2 = "+" products[prod] "%"
}
1' input.txt | xargs -l ./telegram-send.sh

telegram-send.sh

#!/bin/bash
    
GROUP_ID=-myid
BOT_TOKEN=mytoken

# this 3 checks (if) are not necessary but should be convenient
if [ "$1" == "-h" ]; then
  echo "Usage: `basename $0` \"text message\""
  exit 0
fi

if [ -z "$1" ]
  then
    echo "Add message text as second arguments"
    exit 0
fi

if [ "$#" -ne 1 ]; then
    echo "You can pass only one argument. For string with spaces put it on quotes"
    exit 0
fi

curl -s --data "text=$1" --data "chat_id=$GROUP_ID" 'https://api.telegram.org/bot'$BOT_TOKEN'/sendMessage' > /dev/null

Doom
  • 17
  • 4
  • 1
    `./telegram-send.sh "$(awk.sh)"` -- remove the `| xargs -l ./telegram-send.sh` from the end of `awk.sh`. Double-check if `curl` requires DOS line ending ... if it does make that ``./telegram-send.sh "$(awk.sh | unix2dos)"`` – David C. Rankin Jul 10 '23 at 05:04

1 Answers1

1

telegram-send.sh

Simply you can save the output of awk and read it back to your telegram-send.sh file.

here is the way I send notification from a server to a channel

declare -r server_path=IF-YOU-HAVE-PATH
declare -r shmlog_super_group=YOUR-GROUP
declare -r shmdevbot=YOUR-BOT-TOKEN
declare tsm_body

...
...
...

tsm_body="$(< $server_path/tsm_body.txt)"

curl -sL  \
    -o ${server_path}/${0}.log \
    -X POST \
    -H 'Content-Type: application/json' \
    -d '{ "parse_mode": "HTML", "chat_id": "'"$shmlog_super_group"'", "text": "'"${tsm_body}"'", "disable_notification": "false" }' \
    https://api.telegram.org/bot$shmdevbot/sendMessage;

please notice the tsm_body which we have to wrap it with "'" in order to preserver the tsm_body.txt format and style

"'"${tsm_body}"'"

awk.sh

Since we should read the output of awk.sh from a file, no longer you can use xargs. Simply use at to make your script independent of awk.sh

for example:

#!/bin/bash

awk '
/^#Price_Notification_Product/ {
  prod = $0;
}
/^change: / {
  gsub(/[+%]/,"",$2);
  products[prod] += $2;
  $2 = "+" products[prod] "%"
}
1' > input.txt

echo /path/to/telegram-send.sh /path/to/input.txt | at now +1 minute

You may need reading this question/answer


If you insist your own way, please try wrapping your $1 in "'"

curl -s --data text="'"$1"'" ...
Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44
  • @Shabiba Moshiri Thank you, your telegram script and the necessary guidelines helped me, I managed to redirect the messages to my channel, but a new question arose... I believe that some messages will be redirected in groups, making the message sent to telegram very large, there is any way to break/split the message? For example, I would like to send messages with a maximum of 4 lines, if the original message has, for example, 12 lines, I would like to split the message into 3, sending 3 messages containing 4 lines, is this possible? – Doom Jul 10 '23 at 10:48
  • Yeh, sure you can, just split them, which you have to count them, then send each chunk after `sleep 1` -- use a loop – Shakiba Moshiri Jul 13 '23 at 05:24