Commit iniziale
This commit is contained in:
104
main.py
Normal file
104
main.py
Normal file
@@ -0,0 +1,104 @@
|
||||
import requests
|
||||
import time
|
||||
import os
|
||||
|
||||
# CONFIGURAZIONE
|
||||
BOT_TOKEN = os.getenv("BOT_TOKEN")
|
||||
BASE_URL = f"https://api.telegram.org/bot{BOT_TOKEN}"
|
||||
|
||||
# Funzione per ottenere l'IP pubblico
|
||||
def get_public_ip():
|
||||
try:
|
||||
response = requests.get("https://ifconfig.me")
|
||||
response.raise_for_status()
|
||||
return response.text.strip()
|
||||
except requests.exceptions.RequestException:
|
||||
return None
|
||||
|
||||
# Funzione per ottenere aggiornamenti
|
||||
def get_updates(offset=None):
|
||||
url = f"{BASE_URL}/getUpdates"
|
||||
params = {"offset": offset} if offset else {}
|
||||
try:
|
||||
response = requests.get(url, params=params)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
except requests.exceptions.RequestException:
|
||||
return None
|
||||
|
||||
# Funzione per configurare i comandi del bot
|
||||
def set_bot_commands():
|
||||
url = f"{BASE_URL}/setMyCommands"
|
||||
commands = [
|
||||
{"command": "ip", "description": "Mostra l'indirizzo IP pubblico"}
|
||||
]
|
||||
payload = {"commands": commands}
|
||||
try:
|
||||
response = requests.post(url, json=payload)
|
||||
response.raise_for_status()
|
||||
except requests.exceptions.RequestException:
|
||||
pass
|
||||
|
||||
# Funzione per inviare messaggi
|
||||
def send_telegram_message(chat_id, message, keyboard=None):
|
||||
url = f"{BASE_URL}/sendMessage"
|
||||
payload = {"chat_id": chat_id, "text": message}
|
||||
if keyboard:
|
||||
payload["reply_markup"] = keyboard
|
||||
try:
|
||||
response = requests.post(url, json=payload)
|
||||
response.raise_for_status()
|
||||
return response.json().get("result", {}).get("message_id")
|
||||
except requests.exceptions.RequestException:
|
||||
return None
|
||||
|
||||
# Funzione per processare comandi
|
||||
def process_command(chat_id, text):
|
||||
# Tastiera persistente con il pulsante /ip
|
||||
keyboard = {
|
||||
"keyboard": [[{"text": "/ip"}]],
|
||||
"resize_keyboard": True,
|
||||
"persistent": True
|
||||
}
|
||||
|
||||
if text == "/ip":
|
||||
ip_address = get_public_ip()
|
||||
if ip_address:
|
||||
message = f"IP pubblico: {ip_address}"
|
||||
else:
|
||||
message = "Errore nel recupero dell'IP pubblico."
|
||||
send_telegram_message(chat_id, message, keyboard)
|
||||
elif text == "/start":
|
||||
send_telegram_message(chat_id, "Bot attivo. Usa /ip per ottenere l'indirizzo IP pubblico.", keyboard)
|
||||
else:
|
||||
send_telegram_message(chat_id, "Comando non riconosciuto.", keyboard)
|
||||
|
||||
# Funzione principale del bot
|
||||
def main():
|
||||
# Configura i comandi del bot all'avvio
|
||||
set_bot_commands()
|
||||
|
||||
last_update_id = None
|
||||
while True:
|
||||
try:
|
||||
updates = get_updates(last_update_id + 1 if last_update_id else None)
|
||||
if updates and updates.get("ok"):
|
||||
for update in updates["result"]:
|
||||
update_id = update["update_id"]
|
||||
message = update.get("message")
|
||||
|
||||
if message:
|
||||
chat_id = message["chat"]["id"]
|
||||
text = message.get("text", "")
|
||||
process_command(chat_id, text)
|
||||
|
||||
last_update_id = update_id
|
||||
time.sleep(2)
|
||||
except KeyboardInterrupt:
|
||||
break
|
||||
except Exception:
|
||||
time.sleep(5)
|
||||
|
||||
# Avvia il bot
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user