-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBashBot.sh
executable file
·104 lines (86 loc) · 2.67 KB
/
BashBot.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
#================ CODIGOS DE ESTILOS ============|
Bold=$(tput bold)
Norm=$(tput sgr0)
Yellow=$(tput setaf 3)
Red=$(tput setaf 1)
Green=$(tput setaf 2)
#=============================================
setToken(){
TOKEN=$2
}
sendMessage(){
curl -s -X POST "https://api.telegram.org/bot$TOKEN/sendMessage" -d chat_id=$1 -d text="$2" -d parse_mode="HTML"
}
getUpdates(){
result=$(curl -s --request POST \
--url https://api.telegram.org/bot$TOKEN/getUpdates \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '
{
"offset": '$1',
"limit": '$2',
"timeout": '$3'
}
')
ID=$(echo $result | jq -r '.result[-1].update_id')
MESSAGE=$(echo $result | jq -r '.result[-1].message.text')
CHAT_ID=$(echo $result | jq -r '.result[-1].message.chat.id')
}
BashBot.getMessage(){
[[ $( echo $MESSAGE) == null ]] && {
echo ""
} ||{
echo $MESSAGE
}
}
BashBot.sendMessage(){
#echo $@ | egrep '^\-\-chat_id'
text=$(echo "$@" | egrep -o '\-\-text.*' | sed 's/--text//g')
chat_id=$(echo "$@" | egrep -o '^\-\-chat_id\s\w+' | sed 's/--chat_id//')
[[ -n $text ]] && [[ -n $chat_id ]] && sendMessage $chat_id "$text" || echo -e "${Red} Erro ao executar a função BashBot.sendMessage ${Norm}"
}
BashBot.token(){
echo $TOKEN
}
BashBot.offsetNext(){
(( OFFSET = ID + 1 ))
echo $OFFSET
}
BashBot.getUpdate(){
offset=$(echo "$@" | egrep -o '^\-\-offset\s\w+' | sed 's/--offset//')
limit=$(echo "$@" | egrep -o '\-\-limit\s\w+' | sed 's/--limit//')
timeout=$(echo "$@" | egrep -o '\-\-timeout\s\w+' | sed 's/--timeout//g')
[[ -n $offset ]] && [[ -n $limit ]] && [[ -n $timeout ]] && {
getUpdates $offset $limit $timeout
} || {
echo "${Red} Erro na função BashBot.getUpdate parametro não pode ficar vazio${Norm} "
}
}
BashBot.init(){
for args in "$@"; do
case $args in
"--token") setToken $@;;
esac
done
}
BashBot.id(){
echo $CHAT_ID
}
help(){
cat << FECHA
${Bold}opções:${Norm}
${Bold}BashBot.init${Norm} BashBot.init --token <token> ${Green}# Comando necessário para inicinar o bot${Norm}
${Bold}BashBot.token${Norm} BashBot.token ${Green}# Não precisa de parametro. Retorna o token do bot${Norm}
${Bold}BashBot.getUpdate${Norm}
BashBot.getUpdate --offset <numero> --limit <numero> --timeout <numero>
${Bold}BashBot.sendMessage${Norm} BashBot.sendMessage --chat_id <id> --text <mensagem> ${Green}# <id> Identificador do chat - <text> Mensagem a ser enviada${Norm}
FECHA
}
for args in "$@"; do
case $args in
"-h") help; exit 1
;;
esac
done