-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
174 lines (132 loc) · 4.8 KB
/
bot.py
File metadata and controls
174 lines (132 loc) · 4.8 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
import logging
import asyncio
from dotenv import load_dotenv
from supabase import create_client
from telegram import Bot
import requests
logging.basicConfig(
format="%(asctime)s [%(levelname)s] %(message)s",
level=logging.INFO,
datefmt="%H:%M:%S",
)
load_dotenv()
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_SERVICE_ROLE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY")
CHAT_ID = os.getenv("CHAT_ID")
CLOUDFLARE_ACCOUNT_ID = os.getenv("CLOUDFLARE_ACCOUNT_ID")
CLOUDFLARE_API_TOKEN = os.getenv("CLOUDFLARE_API_TOKEN")
API_BASE_URL = f"https://api.cloudflare.com/client/v4/accounts/{CLOUDFLARE_ACCOUNT_ID}/ai/run/"
if not TELEGRAM_BOT_TOKEN:
raise Exception("Variabile TELEGRAM_BOT_TOKEN mancante nel file .env.")
if not SUPABASE_URL or not SUPABASE_SERVICE_ROLE_KEY:
raise Exception("Variabili Supabase mancanti nel file .env.")
if not CHAT_ID:
raise Exception("CHAT_ID mancante nel file .env.")
if not CLOUDFLARE_ACCOUNT_ID or not CLOUDFLARE_API_TOKEN:
raise Exception("CLOUDFLARE_ACCOUNT_ID o CLOUDFLARE_API_TOKEN mancanti nel file .env.")
supabase = create_client(SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY)
MODEL = "@cf/leonardo/lucid-origin"
headers = {
"Authorization": f"Bearer {CLOUDFLARE_API_TOKEN}",
"Content-Type": "application/json"
}
def generate_image(prompt: str):
"""
Genera un'immagine usando il modello Cloudflare AI Lucid Origin.
Restituisce i byte dell'immagine (utilizzabili da Telegram come `photo=image_data`).
"""
if not CLOUDFLARE_ACCOUNT_ID or not CLOUDFLARE_API_TOKEN:
logging.error("CLOUDFLARE_ACCOUNT_ID o CLOUDFLARE_API_TOKEN mancanti nel file .env.")
return None
payload = {"prompt": prompt}
try:
logging.info(f"Generazione immagine con prompt: '{prompt}'")
response = requests.post(f"{API_BASE_URL}{MODEL}", headers=headers, json=payload)
logging.info(f"Cloudflare AI status: {response.status_code}")
if response.headers.get("Content-Type", "").startswith("image/"):
return response.content
try:
data = response.json()
except Exception as e:
logging.error(f"Errore nel parsing JSON: {e}")
logging.debug(response.text[:500])
return None
if "result" in data and "image" in data["result"]:
import base64
return base64.b64decode(data["result"]["image"])
logging.error(f"Formato risposta inatteso: {data}")
return None
except Exception as e:
logging.error(f"Errore nella generazione immagine: {e}")
return None
def get_latest_question():
response = (
supabase.table("questions")
.select("question")
.order("id", desc=True)
.limit(1)
.execute()
)
if response.data and len(response.data) > 0:
return response.data[0]["question"]
return None
def get_latest_image_prompt():
response = (
supabase.table("questions")
.select("image_prompt")
.order("id", desc=True)
.limit(1)
.execute()
)
if response.data and len(response.data) > 0:
return response.data[0]["image_prompt"]
return None
async def create_poll():
bot = Bot(token=TELEGRAM_BOT_TOKEN)
question = get_latest_question()
if not question:
logging.error("Nessuna domanda trovata nel database.")
return
image_prompt = get_latest_image_prompt()
if not image_prompt:
logging.error("Nessuna immagine trovata nel database.")
return
image_data = generate_image(image_prompt)
if not image_data:
logging.error("Errore nella generazione dell'immagine.")
return
bot_info = await bot.get_me()
bot_name = bot_info.first_name
admins = await bot.get_chat_administrators(CHAT_ID)
options = [
admin.user.first_name
for admin in admins
if not admin.user.is_bot and admin.user.first_name != bot_name
]
if len(options) < 2:
options.append("Nessun altro disponibile")
# Aggiungi Miro, Elena, Gian solo se ci sono meno di 11 opzioni
if len(options) < 11:
options.extend(["Miro", "Elena", "Gian"])
await bot.send_photo(
chat_id=CHAT_ID,
photo=image_data,
caption=question
)
logging.info("Immagine inviata con la domanda come didascalia.")
await bot.send_poll(
chat_id=CHAT_ID,
question="Opzioni:",
options=options,
is_anonymous=False,
allows_multiple_answers=True,
)
logging.info(f"Sondaggio creato con opzioni: {options}")
def main():
logging.info("Bot Telegram avviato. Creazione automatica sondaggio...")
asyncio.run(create_poll())
logging.info("Sondaggio inviato. Spegnimento bot.")
if __name__ == "__main__":
main()