· 4 years ago · Jun 04, 2021, 09:08 PM
1import telebot
2from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton
3import firebase_admin
4from firebase_admin import credentials
5from firebase_admin import db
6
7
8# Inizializza Telebot
9#API App Beta
10API_TOKEN = '1609350352:AAF2CW-1pMBGqveh2FNZMislMXzn1_aN0ag'
11bot = telebot.TeleBot(API_TOKEN)
12
13# Inizializza Firebase
14# Fetch the service account key JSON file contents
15cred = credentials.Certificate('FirebaseSDK.json')
16# Initialize the app with a service account, granting admin privileges
17firebase_admin.initialize_app(cred, {
18 'databaseURL': 'https://pqlbeta-default-rtdb.firebaseio.com/'
19})
20# As an admin, the app has access to read and write all data, regradless of Security Rules
21ref = db.reference('/')
22
23
24@bot.message_handler(commands=['inventario'])
25def handle_command_adminwindow(message):
26 # aggiungi una colonna per ogni 7 elementi in matrice
27 rootInventory = ref.child("inventario").get()
28 userPool = []
29 userId = message.from_user.id
30 items = []
31 for i in rootInventory:
32 userPool.append(i)
33 userId = f"{userId}"
34 if userId in userPool:
35 refItems = ref.child("inventario").child(userId).get()
36 for i in refItems:
37 items.append(i)
38 nitem = len(items)
39 while(nitem%7!=0):
40 nitem += 1
41 ncol = nitem / 7
42 ncol = int(ncol)
43 #carica query con i nuovi dati
44 show_info(message, ncol, items)
45
46def show_info(message, ncol, items):
47 # Lista oggetti scaricati dall'inventario in cloud, pronti al confronto per l'associazione per tipo elemento
48 markup = InlineKeyboardMarkup()
49 listFruits = ["mela", "banana", "fragole", "Ciliegie", "lampone", "mirtillo"]
50
51
52 '''while ncol != 1:
53 # inserisci nuova colonna
54 row = []
55 for i in range(0, len(items)):
56 if items[i] in listFruits:
57 row.append(InlineKeyboardButton(text=items[i], callback_data=f"cb_fruit"))
58 else:
59 row.append(InlineKeyboardButton(text=items[i], callback_data=f"cb_other"))
60 # finisce la colonna ricomincia dall'inizio
61 markup.row(*row)
62 ncol -= 1'''
63
64 i = 0
65 while i < len(items):
66 row = []
67 if items[i] in listFruits:
68 row.append(InlineKeyboardButton(text=items[i], callback_data=f"cb_fruit"))
69 else:
70 row.append(InlineKeyboardButton(text=items[i], callback_data=f"cb_other"))
71 i += 1
72 if items[i] in listFruits:
73 row.append(InlineKeyboardButton(text=items[i], callback_data=f"cb_fruit"))
74 else:
75 row.append(InlineKeyboardButton(text=items[i], callback_data=f"cb_other"))
76 i += 1
77 markup.row(*row)
78
79 bot.send_message(message.chat.id, 'Inventario', reply_markup=markup)
80
81
82@bot.callback_query_handler(func=lambda call: True)
83def callback_query(call):
84 if call.data == "cb_fruit":
85 bot.answer_callback_query(call.id, "È stato selezionato un frutto")
86 elif call.data == "cb_other":
87 bot.answer_callback_query(call.id, "È stato selezionato altro")
88
89
90
91bot.polling()