Week 14, Update 01

This commit is contained in:
Sidney PEPO (aka sysb1n) 2023-12-11 13:43:06 -03:00
parent c6ce1eea2e
commit f79e48bf68
Signed by: sidneypepo
GPG Key ID: A188DCCE6CD96D52
2 changed files with 117 additions and 0 deletions

View File

@ -0,0 +1,30 @@
#+title: README.org
#+author: Sidney Pedro
#+date: [2023-12-10 Sun]
* Atividade avaliativa 04 - Bot Telegram
Crie um programa que funciona como bot do Telegram, criado usando o aplicativo no canal BotFather.
/IMPORTANTE/: use apenas a API de sockets *NÃO* use API de terceiros para acesso ao Telegram.
O usuário deve se cadastrar no seu bot (usando o nome escolhido) que deve oferecer pelo menos cinco serviços. As conversas entre um usuário e o bot devem ser individualizadas, por óbvio; assim um chat entre um usuário e o bot não deve ser vista por outros usuários do bot.
Exemplos de bot (pense em outros):
- Oferecer informações de monitoramento da rede onde o bot se encontra:
+ */info* informações básicas sobre a rede (ip, máscara, gateway);
+ */ping* tempo médio de ping (quatro pacotes) entre a máquina do bot e seu gateway;
+ */active ip* informa se o ip está respondendo;
+ */service ip:port* informa se há um serviço escutando no ip e porta recebidos;
+ */dns* informa o servidor de DNS da máquina do bot;
+ */map* lista as máquinas e portas ativas no segmento de rede local do bot.
- Download de imagens na internet:
+ */download url_image* download de uma imagem da internet no servidor.
- Listar as portas que estão abertas em um determinado host:
+ */scan host* verifica o status das portas de um determinado host. Informa apenas as que estão respondendo.

View File

@ -0,0 +1,87 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# ifrn/2023.2/programacao_para_redes/11_bot_telegram/bot_telegram.py
# Copyright (C) 2023 Sidney Pedro
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
import requests
API_KEY = "6308631803:AAEArbA5TdVhJRtv1xofzjgkWIfpCp-KWMk" # @progredes_dummy_bot
TELEGRAM_API = f"https://api.telegram.org/bot{API_KEY}"
def main():
latest_date = 0
while (True):
try:
updates = requests.get(TELEGRAM_API + "/getUpdates")
except KeyboardInterrupt:
raise KeyboardInterrupt()
except:
continue
retorno = updates.json()
if (updates.status_code == 409):
print("Error: another connection is being established! Trying again...")
continue
elif (updates.status_code != 200):
return
elif (len(retorno["result"]) == 0):
continue
try:
arquivo = open("timestamp.txt", "r")
except:
arquivo = open("timestamp.txt", "w")
arquivo.write(f"{latest_date}\n")
arquivo.close()
arquivo = open("timestamp.txt", "r")
latest_date = int(arquivo.readline())
arquivo.close()
latest_message = retorno["result"][-1]
message_date = latest_message["message"]["date"]
if (not message_date > latest_date):
continue
else:
latest_date = message_date
print(f"{latest_message}")
chat_id = latest_message["message"]["chat"]["id"]
message_id = latest_message["message"]["message_id"]
if (latest_message["message"]["text"] == "/start"):
message = "Comando recebido"
else:
message = "Bom dia 🌞"
dados = {"chat_id": chat_id, "reply_to_message_id": message_id, "text": message}
post = requests.post(TELEGRAM_API + "/sendMessage", data=dados)
arquivo = open("timestamp.txt", "w")
arquivo.write(f"{latest_date}\n")
arquivo.close()
if (__name__ == "__main__"):
try:
main()
except:
print("\nSaindo...")