forked from shellscriptx/shellbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBotDownloadFile2.sh
More file actions
executable file
·89 lines (77 loc) · 2.91 KB
/
Copy pathBotDownloadFile2.sh
File metadata and controls
executable file
·89 lines (77 loc) · 2.91 KB
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
#!/bin/bash
#
# SCRIPT: BotDownloadFile2.sh
#
# DESCRIÇÃO: Efetua download dos arquivos enviados para o privado, grupo ou canal.
# Em grupos/canais o bot precisa ser administrador para ter acesso a
# todas mensagens enviadas.
#
# Para melhor compreensão foram utilizados parâmetros longos nas funções; Podendo
# ser substituidos pelos parâmetros curtos respectivos.
# Importando API
source ShellBot.sh
# Token do bot
bot_token='<TOKEN_AQUI>'
# Inicializando o bot
ShellBot.init --token "$bot_token" --monitor
ShellBot.username
while :
do
# Obtem as atualizações
ShellBot.getUpdates --limit 100 --offset $(ShellBot.OffsetNext) --timeout 30
# Lista o índice das atualizações
for id in $(ShellBot.ListUpdates)
do
# Inicio thread
(
# Desativa download
download_file=0
# Lê a atualização armazenada em 'id'
if [ "${update_id[$id]}" ]; then
# Monitora o envio de arquivos do tipo:
#
# * Documento
# * Foto
# * Sticker
# * Musica
#
# Se a variável do tipo for inicializada, salva o ID do arquivo enviado em 'file_id' e ativa o download.
[[ ${message_document_file_id[$id]} ]] && file_id=${message_document_file_id[$id]} && download_file=1
[[ ${message_photo_file_id[$id]} ]] && file_id=${message_photo_file_id[$id]} && download_file=1
[[ ${message_sticker_file_id[$id]} ]] && file_id=${message_sticker_file_id[$id]} && download_file=1
[[ ${message_audio_file_id[$id]} ]] && file_id=${message_audio_file_id[$id]} && download_file=1
# Verifica se o download está ativado.
[[ $download_file -eq 1 ]] && {
# Inicializa um array se houver mais de um ID salvo em 'file_id'.
# (É recomendado para fotos e vídeos, pois haverá o mesmo arquivo com diversas resoluções e ID's)
file_id=($file_id)
# Executa o método 'ShellBot.downloadFile', captura os dados de retorno e salva em 'file_path'
file_path=$(ShellBot.downloadFile --file_id "${file_id[0]}" --dir "$HOME") && {
# Se o download foi bem-sucedido
#
# Separa as informações armazenadas em 'file_path'
id=$(echo $file_path | cut -d'|' -f1) # ID do arquivo
size=$(echo $file_path | cut -d'|' -f2) # Tamanho
dir=$(echo $file_path | cut -d'|' -f3) # Diretório de destino
# Layout da mensagem
msg='*Download do arquivo realizado com sucesso.*\n\n'
msg+="*Informações*\n\n"
msg+="*ID*: $id\n"
msg+="*Tamanho*: $size bytes\n"
msg+="*Salvo em*: $dir"
# Envia a mensagem de confirmação e anexa as informações
ShellBot.sendMessage --chat_id ${message_chat_id[$id]} \
--text "$(echo -e "$msg")" \
--parse_mode markdown
}
}
case ${message_text[$id]} in
'/teste') # comando teste
ShellBot.sendMessage --chat_id "${message_chat_id[$id]}" --text "Mensagem de teste."
;;
esac
fi
) & # Utilize a thread se deseja que o bot responda a várias requisições simultâneas.
done
done
#FIM