-
-
Save lonevetad/41f6b95e6273515740bf049a172712eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import telegram, datetime, calendar, locale | |
from telegram import InlineKeyboardMarkup, InlineKeyboardButton, InputMediaPhoto | |
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, MessageHandler, Filters | |
def start(update, context): | |
keyboard = [[ | |
InlineKeyboardButton('Calendario', callback_data='calendar'), | |
InlineKeyboardButton('💶 Prezzi', callback_data='prices') | |
], [ | |
InlineKeyboardButton('Info Canale', callback_data='channel_info'), | |
InlineKeyboardButton('❔ Aiuto', callback_data='help') | |
]] | |
reply_markup = InlineKeyboardMarkup(keyboard) | |
update.message.reply_text( | |
'🧠Ciao! Ti guideró passo passo all\'acquisto di uno spazio pubblicitario sul canale @Sapiens3.\nPremi il tasto "Aiuto" per una spiegazione approfondita del funzionamento.\n\nℹ️ Chat: @Sapiens3Manager', | |
reply_markup=reply_markup) | |
# Inizializza la chiave 'ad_message_sent' con il valore False se non è già presente per inoltrare solo il messaggio di sponsor | |
if 'ad_message_sent' not in context.user_data: | |
context.user_data['ad_message_sent'] = False | |
previous_button = InlineKeyboardButton('Precedente', callback_data='previous') | |
next_button = InlineKeyboardButton('Successivo', callback_data='next') | |
inline_keyboard = [[previous_button, next_button]] | |
reply_markup_inline = InlineKeyboardMarkup(inline_keyboard) | |
def handle_inline_button(update, context): | |
query = update.callback_query | |
button = query.data | |
if button == 'calendar': | |
context.user_data['keyboard_state'] = 'calendar' | |
show_calendar(update, context) | |
elif button == 'channel_info': | |
infocanale = "Il canale invierà un messaggio divulgativo proprietario ogni giorno alle ore 10:00, 15:00 e 20:00. In caso di eventi straordinari, verrà inviato un messaggio divulgativo senza preavviso." | |
query.answer() # Aggiunto per chiudere la notifica di callback | |
query.message.reply_text(infocanale) | |
elif button == 'help': | |
aiuto = "messaggio che spiega come funziona il bot e i comandi" | |
query.answer() # Aggiunto per chiudere la notifica di callback | |
query.message.reply_text(aiuto) | |
elif button == 'prices': | |
prezzi = "🕒 1h 💵 4€\n🕒 3h 💵 6€\n🕒 15.01 - 20.01 💵 7.50€\n🕒 21.00 - 10.00💵 11.00€\n\nPer altre richieste: @Sapiens3Manager" | |
query.answer() # Aggiunto per chiudere la notifica di callback | |
query.message.reply_text(prezzi) | |
context.user_data['keyboard_state'] = 'menu' | |
def show_calendar(update, context): | |
now = datetime.datetime.now() | |
locale.setlocale(locale.LC_TIME, 'it_IT') | |
month_name = now.strftime('%B') | |
year = now.year | |
previous_month = now.month - 1 | |
previous_year = now.year if previous_month != 0 else now.year - 1 | |
next_month = now.month + 1 | |
next_year = now.year if next_month != 13 else now.year + 1 | |
previous_button = InlineKeyboardButton('Precedente', callback_data='previous') | |
next_button = InlineKeyboardButton('Successivo', callback_data='next') | |
days_of_week = ['Lun', 'Mar', 'Mer', 'Gio', 'Ven', 'Sab', 'Dom'] | |
keyboard = [ | |
[InlineKeyboardButton(day, callback_data='empty') for day in days_of_week] | |
] | |
first_day_of_month = datetime.datetime(year, now.month, 1) | |
first_weekday = first_day_of_month.weekday() | |
row = [ | |
InlineKeyboardButton('🧠', callback_data='empty') | |
for _ in range(first_weekday) | |
] | |
for day in range(1, 32): | |
button_text = str(day) | |
if day == now.day: | |
button_data = 'day_' + button_text | |
elif datetime.datetime(year, now.month, day) < now or day == 1: | |
button_data = 'invalid_day' | |
else: | |
button_data = 'day_' + button_text | |
button = InlineKeyboardButton(button_text, callback_data=button_data) | |
row.append(button) | |
if len(row) == 7: | |
keyboard.append(row) | |
row = [] | |
if len(row) > 0: | |
while len(row) < 7: | |
row.append(InlineKeyboardButton('🧠', callback_data='empty')) | |
keyboard.append(row) | |
keyboard.append([previous_button, next_button]) | |
reply_markup = InlineKeyboardMarkup(keyboard) | |
update.callback_query.message.reply_text(f'Calendario di {month_name} {year}:', reply_markup=reply_markup) | |
def handle_callback_query(update, context): | |
query = update.callback_query | |
callback_data = query.data | |
if callback_data == 'next': | |
next_month(update, context) | |
elif callback_data == 'previous': | |
previous_month(update, context) | |
elif callback_data.startswith(('10_15', '15_20', '21_10', '3_ore')): | |
handle_selected_time(update, context, callback_data) | |
else: | |
handle_selected_day(update, context) | |
query.answer() | |
def next_month(update, context): | |
current_month = context.user_data.get('current_month', datetime.datetime.now().month) | |
current_year = context.user_data.get('current_year', datetime.datetime.now().year) | |
current_month += 1 | |
if current_month > 12: | |
current_month = 1 | |
current_year += 1 | |
context.user_data['current_month'] = current_month | |
context.user_data['current_year'] = current_year | |
update_calendar(update, context, current_month, current_year) | |
def previous_month(update, context): | |
current_month = context.user_data.get('current_month', datetime.datetime.now().month) | |
current_year = context.user_data.get('current_year', datetime.datetime.now().year) | |
current_month -= 1 | |
if current_month < 1: | |
current_month = 12 | |
current_year -= 1 | |
context.user_data['current_month'] = current_month | |
context.user_data['current_year'] = current_year | |
update_calendar(update, context, current_month, current_year) | |
def update_calendar(update, context, current_month, current_year): | |
month_name = calendar.month_name[current_month] | |
year = current_year | |
days_of_week = ['Lun', 'Mar', 'Mer', 'Gio', 'Ven', 'Sab', 'Dom'] | |
keyboard = [ | |
[InlineKeyboardButton(day, callback_data='empty') for day in days_of_week] | |
] | |
_, last_day = calendar.monthrange(current_year, current_month) | |
first_day_of_month = datetime.datetime(year, current_month, 1) | |
first_weekday = first_day_of_month.weekday() | |
now = datetime.datetime.now() | |
row = [ | |
InlineKeyboardButton('🧠', callback_data='empty') | |
for _ in range(first_weekday) | |
] | |
for day in range(1, last_day + 1): | |
button_text = str(day) | |
if current_month == now.month and day == now.day: | |
button_data = 'day_' + button_text | |
elif datetime.datetime(year, current_month, day) < now or (current_month == now.month and day < now.day): | |
button_data = 'invalid_day' | |
else: | |
button_data = 'day_' + button_text | |
button = InlineKeyboardButton(button_text, callback_data=button_data) | |
row.append(button) | |
if len(row) == 7: | |
keyboard.append(row) | |
row = [] | |
if len(row) > 0: | |
while len(row) < 7: | |
row.append(InlineKeyboardButton('🧠', callback_data='empty')) | |
keyboard.append(row) | |
previous_button = InlineKeyboardButton('Precedente', callback_data='previous') | |
next_button = InlineKeyboardButton('Successivo', callback_data='next') | |
keyboard.append([previous_button, next_button]) | |
reply_markup = InlineKeyboardMarkup(keyboard) | |
query = update.callback_query | |
context.bot.edit_message_text(f'Calendario di {month_name} {year}:', chat_id=query.message.chat_id, | |
message_id=query.message.message_id, reply_markup=reply_markup) | |
def handle_selected_day(update, context): | |
selected_day = update.callback_query.data | |
context.user_data['selected_day'] = selected_day | |
keyboard_state = context.user_data.get('keyboard_state') | |
if selected_day.startswith('day_'): | |
context.user_data['keyboard_state'] = 'time_selection' | |
keyboard = [ | |
[ | |
InlineKeyboardButton('10.00 - 15.00', callback_data='10_15'), | |
InlineKeyboardButton('15.00 - 20.00', callback_data='15_20') | |
], | |
[ | |
InlineKeyboardButton('21.00 - 10.00', callback_data='21_10'), | |
InlineKeyboardButton('3 ore', callback_data='3_ore') | |
], | |
[ | |
InlineKeyboardButton('Indietro', callback_data='back_to_calendar') | |
] | |
] | |
reply_markup = InlineKeyboardMarkup(keyboard) | |
query = update.callback_query | |
query.message.edit_text('Seleziona una fascia oraria:', reply_markup=reply_markup) | |
elif selected_day == 'empty': | |
query = update.callback_query | |
query.answer("Pulsante non valido selezionato.") | |
elif selected_day == 'invalid_day': | |
query = update.callback_query | |
query.answer("Giorno non valido selezionato.") | |
elif selected_day == 'back_to_calendar': | |
current_month = context.user_data.get('current_month', datetime.datetime.now().month) | |
current_year = datetime.datetime.now().year | |
update_calendar(update, context, current_month, current_year) | |
else: | |
context.user_data['keyboard_state'] = 'menu' | |
handle_inline_button(update, context) | |
def handle_selected_time(update, context, callback_data): | |
query = update.callback_query | |
selected_time = query.data | |
user_id = query.from_user.id | |
if context.user_data.get('ad_message_sent', False): | |
# L'utente ha già inviato un messaggio di sponsor e in attesa di approvazione | |
message_text = "Hai già richiesto uno sponsor e il messaggio è in attesa di approvazione. " \ | |
"Attendi l'approvazione del messaggio precedente prima di selezionare una nuova fascia oraria." | |
context.bot.send_message(chat_id=user_id, text=message_text) | |
return | |
# Recupera l'orario scelto dal callback_data | |
if selected_time == '10_15': | |
time_text = '10.00 - 15.00' | |
elif selected_time == '15_20': | |
time_text = '15.00 - 20.00' | |
elif selected_time == '21_10': | |
time_text = '21.00 - 10.00' | |
elif selected_time == '3_ore': | |
time_text = '3 ore' | |
else: | |
time_text = 'Orario non valido' | |
# Salva l'orario selezionato nel contesto dell'utente | |
context.user_data['selected_time'] = selected_time | |
context.user_data['time_text'] = time_text | |
# Rimuovi il gestore 'handle_selected_time' dal dispatcher | |
dispatcher = context.dispatcher | |
dispatcher.remove_handler(update.callback_query.message.reply_markup) | |
# Verifica se è stato selezionato un messaggio di sponsor | |
if 'sponsor_message' in context.user_data: | |
# Stampa il messaggio di avviso | |
message_text = "Hai già selezionato un messaggio di sponsor. " \ | |
"Invia il messaggio precedente per l'approvazione prima di selezionare una nuova fascia oraria." | |
context.bot.send_message(chat_id=user_id, text=message_text) | |
else: | |
# Costruisci il messaggio di richiesta del messaggio di sponsor con l'orario scelto | |
context.user_data['sponsor_time_text'] = time_text | |
message_text = f"Hai selezionato l'orario {time_text} per la richiesta di sponsor. " \ | |
"Invia il messaggio di sponsor che desideri pubblicare nel canale. " \ | |
"Il messaggio sarà inviato agli amministratori per l'approvazione." | |
context.bot.send_message(chat_id=user_id, text=message_text) | |
print("pronto per ricevere il messaggio di sponsor") | |
def handle_sponsor_message(update, context): | |
user_id = update.message.chat_id | |
sponsor_message = update.message | |
if context.user_data.get('ad_message_sent', False): | |
if context.user_data.get('ad_message_sent') == 'pending_approval': | |
# Il messaggio di sponsor è già stato inviato e in attesa di approvazione | |
handle_approved_sponsor_message(update, context) | |
else: | |
# Il messaggio di sponsor è stato inviato e approvato | |
handle_inline_button(update, context) | |
else: | |
if 'selected_time' in context.user_data: | |
# Salva il messaggio di sponsor nel contesto dell'utente | |
context.user_data['sponsor_message'] = sponsor_message | |
context.user_data['ad_message_sent'] = 'pending_approval' | |
# Crea la tastiera inline solo se è presente | |
inline_keyboard = sponsor_message.reply_markup.inline_keyboard if sponsor_message.reply_markup else None | |
if sponsor_message.photo: | |
# Cattura l'immagine e la didascalia separatamente | |
photo = sponsor_message.photo[-1] | |
caption = sponsor_message.caption if sponsor_message.caption else None | |
context.bot.send_photo(chat_id=user_id, photo=photo.file_id, caption=caption, reply_markup=InlineKeyboardMarkup(inline_keyboard) if inline_keyboard else None) | |
# Invia un messaggio di conferma all'utente | |
message_text = "Hai inviato un messaggio di sponsor. Il messaggio è in attesa di approvazione." | |
context.bot.send_message(chat_id=user_id, text=message_text) | |
else: | |
# Nessun orario selezionato, considera il messaggio come una richiesta generica | |
handle_inline_button(update, context) | |
def handle_approved_sponsor_message(update, context): | |
return | |
def main(): | |
TOKEN = '6106675582:AAH0OvVIQ9heyQysGMgsXiXx8P5veuf3c1E' | |
updater = Updater(TOKEN, use_context=True) | |
dispatcher = updater.dispatcher | |
dispatcher.add_handler(CommandHandler('start', start)) | |
callback_handler = CallbackQueryHandler(handle_callback_query) | |
dispatcher.add_handler(callback_handler) | |
dispatcher.add_handler(MessageHandler(Filters.text, handle_sponsor_message)) | |
dispatcher.add_handler(CallbackQueryHandler(handle_callback_query, pass_user_data=True, pass_chat_data=True)) | |
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_inline_button)) | |
sponsor_message_handler = MessageHandler(Filters.text | Filters.photo, handle_sponsor_message) | |
dispatcher.add_handler(sponsor_message_handler) | |
updater.start_polling() | |
updater.idle() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment