ServerBot/func.sh

55 lines
1.3 KiB
Bash
Raw Normal View History

2019-05-02 20:41:03 +02:00
#!/bin/bash
2019-09-16 13:20:14 +02:00
fname="lastmsg.bot"
#1. arg= ident, 2. arg = text
2019-05-02 20:41:03 +02:00
send() {
2019-09-16 13:20:14 +02:00
#check file
mapfile -t file < $fname
fid=${file[0]}
msgid=${file[1]}
count=${file[2]}
params="chat_id=${userid}&text=$2"
#echo "read: fid: ${fid}, msgid: ${msgid}, count: ${count}"
if [ "$fid" == "$1" ]; then
#update only
method="editMessageText"
date=$(date '+%F %H:%M')
params="message_id=${msgid}&${params}"
count=$((count+1))
printf -v params "%b\n\n(%b x wiederholt am %b)" "${params}" "${count}" "${date}"
else
method="sendMessage"
count="0"
fi
#echo "Sending $2 to ${userid}"
#request
2021-01-24 22:08:44 +01:00
#raw=$(curl --data "${params}" "https://api.telegram.org/bot${token}/${method}" 2> /dev/null)
request="${apiurl}${token}/${method}"
#echo "request: $request - $params"
raw=$(curl --data "${params}" "${request}" -s 2> /dev/null)
2019-09-16 13:20:14 +02:00
#parse response
2021-01-24 22:08:44 +01:00
echo "recieved: $raw"
2019-09-16 13:20:14 +02:00
msgid=$(python3 -c "import sys, json; print(json.loads('$raw'.replace('\n',''))['result']['message_id'])")
2021-01-24 22:08:44 +01:00
if [ "$?" -ne "0" ]; then
resetFile
else
echo "msgid: ${msgid}, count: ${count}"
2019-09-16 13:20:14 +02:00
2021-01-24 22:08:44 +01:00
#write to file
echo "$1" > $fname #ident
echo "${msgid}" >> $fname #msgid
echo "${count}" >> $fname #count
fi
2019-09-16 13:20:14 +02:00
}
#used to rset the file, when nothing is sent
resetFile() {
echo "..." > $fname #ident -> never match
echo "-100" >> $fname #msgid
echo "-1000" >> $fname #count
2019-05-02 20:41:03 +02:00
}