added gettimestamp function

This commit is contained in:
celso 2023-03-31 01:51:04 -03:00
parent 3182b639dd
commit f26401ec1f
2 changed files with 25 additions and 8 deletions

View File

@ -1,14 +1,19 @@
# bashbot-lib
bashbot-lib provides generic functionality for telegram bots made in bash<br>
with this library you will be able to run a telegram bot with few dependencies<br>
**IMPORTANT**: bashbot-lib uses the getUpdates method with tcp_keepalive, not webhooks<br>
**IMPORTANT**: bashbot-lib is not made for asynchronous use, only one message is received at a time and is done so in order<br>
bashbot-lib provides generic functionality for telegram bots made in bash
with this library you will be able to run a telegram bot with few dependencies
**IMPORTANT**: bashbot-lib uses the getUpdates method with tcp_keepalive, not webhooks
**IMPORTANT**: bashbot-lib is not made for asynchronous use, only one message is received at a time and is done so in order
[TOC2]
## license
this program is licensed under the **Affero GNU Public License v3**, you can read the copy that comes along with this program or read it at [gnu.org's website](http://www.gnu.org/licenses/agpl-3.0.html)
## dependencies
the following is all that's needed
the following is all that's needed:
* bash 4+
* GNU coreutils (sed, grep, tail, head, etc.)
* curl
@ -17,7 +22,7 @@ the following is all that's needed
Bash 4+ is obligatory for this library to work as features introduced at this point are heavily used.
## installation
drop it wherever you like, although I recommend either of these options
drop it wherever you like, although I recommend either of these options:
* copying the files to /usr/lib/bashbot-lib/
* add it as a submodule in your project (git submodules are very annoying to deal with)
* simply keep it in a subfolder in your project
@ -28,7 +33,7 @@ source the library, and optionally the viewer, in your bash shellscript
. /usr/lib/bashbot-lib/bashbot-lib.sh
. /usr/lib/bashbot-lib/viewer.sh
```
you will also need the following
you will also need the following:
* a variable named api_url with the following content (mind the trailing forward slash): `api_url="https:/api.telegram.org/bot${YOUR_TOKEN_HERE}/"`
* the following files to read and write from: `updates.txt` `sentmsgs.txt` `delmsgs.txt`
* these files must be placed in the same directory you run your bot from or in the directory defined in the following bullet poiint
@ -51,7 +56,7 @@ with every update, getupd calculates the new offset required to request new upda
### reading message contents
use the `getmsg_content` function to parse the contents of a message and save them to a bash associative array
the `getmsg_content` receives two arguments
the `getmsg_content` receives two arguments:
* the **name** (not a reference to) of a bash associative array
* the filename of a file with telegram JSON responses formatted by this library's functions
@ -80,6 +85,7 @@ the bash associative array will hold the following contents
| msg_id | a number to reference this message |
| text | may contain special symbols |
| callback | callback_data, empty if there is none |
| timestamp | timestamp in dd/mm/yy hh:mm format |
if you sourced the viewer, you can view these contents with `view_content "curmsg"` where `curmsg` is the name of the bash associative array populated by `getmsg_content`

View File

@ -36,6 +36,9 @@ getupd() {
[ ! -z "${update}" ] && printf "%s\n" "${update}" >> "${bot_tmpdir}updates.txt"
}
# when another comment refers to "getX functions", the following are what is being refered to
# $1 is the filename of a file with telegram JSON responses to the getupd function
# if ${bot_tmpdir} is set, it will be appended before the filename
getmsg_id() {
tail -n1 "${bot_tmpdir}${1}" | grep -om1\
"\"message\":{\"message_id\":[0-9]\+\|^\"message_id\":[0-9]\+" | grep -om1 "[0-9]\+"
@ -108,6 +111,12 @@ getcbk_data() {
[ ! -z "${callbacks}" ] && utf-16-surrogate-pair-decode "${callbacks:8:-2}"
}
gettimestamp() {
local timestamp="$(tail -n 1 "${bot_tmpdir}${1}"\
| grep "\"date\"" | grep -om1 "\"date\":[0-9]\+,")"
[ ! -z "${timestamp}" ] && printf "%s" "${timestamp:7:-1}"
}
# $1 is name of a bash array with the number of columns in every row such as ( 1 1 3 ) for a 3 row keyboard in which the first and second row have 1 column, and the third has 3
# $2 is name of a bash array with as many text entries as there are columns, they are added left to right, up to down, to the rows/columns configuration passed as first arg
# $3 is name of a bash array with as many data entries as there are columns, they are added left to right, up to down, to the rows/columns configuration passed as first arg
@ -199,6 +208,7 @@ delmsg() {
}
# $1 is the name of a bash associative array to be filled with the message contents
# $2 is the filename to be passed to the getX functions
getmsg_content(){
local -n _REF="${1}"
_REF[user_id]="$(getusr_id "${2}")"
@ -209,4 +219,5 @@ getmsg_content(){
_REF[msg_id]="$(getmsg_id "${2}")"
_REF[text]="$(gettext "${2}")"
_REF[callback]="$(getcbk_data "${2}")"
_REF[timestamp]="$(gettimestamp "${2}")"
}