-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
188 lines (124 loc) · 5.18 KB
/
bot.py
File metadata and controls
188 lines (124 loc) · 5.18 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
import telebot
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton
import logging
import requests
import os
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
# Configurazione
QR_API_URL = "https://api.qrcode-monkey.com/qr/custom"
LOGO_URL_W = "https://hknpolito.org/directus/assets/149f57c9-e3c6-4680-ae08-7fa7b83bef4a.png"
LOGO_URL_B = "https://hknpolito.org/directus/assets/50024bd5-76ba-43f8-b058-e6d2aeb3b2cb.png"
QR_COLOR = "#3c506e"
BG_COLOR = "#061e33"
# Logging (utile per il debug)
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
def gen_markup(dict, n):
markup = InlineKeyboardMarkup()
markup.row_width = n
for key in dict:
markup.add(InlineKeyboardButton(key, callback_data=dict[key]))
return markup
bot = telebot.TeleBot(TELEGRAM_BOT_TOKEN, parse_mode=None)
logging.info("Bot avviato con successo!")
def qr_white(message):
bot.send_message(message.chat.id, "Inviami un link")
@bot.message_handler(func=lambda m: True)
def create_qr(message):
text = message.text.strip()
payload = {
"data": text,
"config": {
"logo": LOGO_URL_B,
"logoMode": "clean",
"bodyColor": QR_COLOR,
"eye1Color": QR_COLOR,
"eye2Color": QR_COLOR,
"eye3Color": QR_COLOR,
"eyeBall1Color": QR_COLOR,
"eyeBall2Color": QR_COLOR,
"eyeBall3Color": QR_COLOR,
},
"size": 1000,
"download": False,
"file": "png"
}
r = requests.post(QR_API_URL, json=payload)
bot.send_message(message.chat.id, "L'area IT ha cucinato👨🏻🍳")
bot.send_photo(message.chat.id, r.content, caption="Ecco il tuo QR code!")
keyboard = {"Crea un altro QR code": "new_qr"}
bot.send_message(message.chat.id, "Se vuoi crearne un altro, premi il pulsante qui sotto!", reply_markup=gen_markup(keyboard, 1))
bot.register_next_step_handler(message, create_qr)
def qr_blue(message):
bot.send_message(message.chat.id, "Inviami un link")
@bot.message_handler(func=lambda m: True)
def create_qr(message):
text = message.text.strip()
payload = {
"data": text,
"config": {
"logo": LOGO_URL_W,
"logoMode": "clean",
"bodyColor": "#ffffff",
"eye1Color": "#ffffff",
"eye2Color": "#ffffff",
"eye3Color": "#ffffff",
"eyeBall1Color": "#ffffff",
"eyeBall2Color": "#ffffff",
"eyeBall3Color": "#ffffff",
"bgColor": BG_COLOR,
},
"size": 1000,
"download": False,
"file": "png"
}
r = requests.post(QR_API_URL, json=payload)
bot.send_message(message.chat.id, "L'area IT ha cucinato👨🏻🍳")
bot.send_photo(message.chat.id, r.content, caption="Ecco il tuo QR code!")
bot.send_message(message.chat.id, "Se vuoi crearne un altro, premi il pulsante qui sotto!", reply_markup=gen_markup({"Crea un altro QR code": "new_qr"}, 1))
bot.register_next_step_handler(message, create_qr)
@bot.callback_query_handler(func=lambda call: True)
def callback_query(call):
if call.data == "white":
keyboard = {
"Sfondo Bianco ✅": "pass",
"Sfondo Blu": "pass"
}
reply_markup = gen_markup(keyboard, 2)
bot.edit_message_reply_markup(chat_id = call.message.chat.id, message_id = call.message.message_id, reply_markup=reply_markup)
qr_white(call.message)
elif call.data == "blue":
keyboard = {
"Sfondo Bianco": "pass",
"Sfondo Blu ✅": "pass"
}
reply_markup = gen_markup(keyboard, 2)
bot.edit_message_reply_markup(chat_id = call.message.chat.id, message_id = call.message.message_id, reply_markup=reply_markup)
qr_blue(call.message)
elif call.data == "new_qr":
keyboard = {"Creazione in corso...": "pass"}
reply_markup = gen_markup(keyboard, 1)
bot.edit_message_reply_markup(chat_id = call.message.chat.id, message_id = call.message.message_id, reply_markup=reply_markup)
send_welcome(call.message)
elif call.data == "pass":
pass
else:
bot.edit_message_reply_markup(chat_id = call.message.chat.id, message_id = call.message.message_id)
@bot.message_handler(commands=['start'])
def send_welcome(message):
bot.send_message(message.chat.id, "Benvenuto! Sono il bot per creare QR code personalizzati targati HKN🤳")
# Creazione dei bottoni
keyboard = {
"Sfondo Bianco": "white",
"Sfondo Blu": "blue"
}
reply_markup = gen_markup(keyboard, 2)
# Invio del messaggio con i bottoni
bot.send_message(message.chat.id, "Scegli il colore dello sfondo:", reply_markup=reply_markup)
@bot.message_handler(func=lambda message: True)
def unknown(message):
bot.send_message(message.chat.id, "Scusami, non ho capito il comando.")
send_welcome(message)
bot.infinity_polling()