· 4 years ago · Jun 03, 2021, 06:08 AM
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
25
26def gen_markup(ncol,items):
27 #lista oggetti di tipo 'cibo' per il confronto e associazione tipo elemento
28 listFoods = ["mela","banana","fragole","Ciliegie","lampone","mirtillo"]
29
30 print("funzione richiamata, provo a generare markup")
31 markup = InlineKeyboardMarkup(row_width=ncol)
32 print(f"numero colonne generate: {markup.row_width}, avvio costruzione query")
33 #avvio sequenza di riempimento delle celle
34 for i in items:
35 print(f"è stato trovato {i}, associazione tipo elemento...")
36 if i in listFoods:
37 typeItem = "cb_food"
38 print("elemento di tipo 'cibo'")
39 else:
40 typeItem = "cb_other"
41 print("elemento di tipo 'altro'")
42 markup.add(InlineKeyboardButton(f"{i}", callback_data=typeItem))
43 return markup
44
45'''
46il metodo simile per la gestioe delle colonne è:
472-row, 2+1 columns
48[[{“text”:”Text 1″,”callback_data”:”1″},{“text”:”Link 1″,”url”:”https://botpress.org”}],[{“text”:”Text2″,”callback_data”:”2″}]]
49
50effettua ciclo for incrementale con confronto finale se uguale a n colonne calcolato
51'''
52
53
54@bot.callback_query_handler(func=lambda call: True)
55def callback_query(call):
56 if call.data == "cb_food":
57 bot.answer_callback_query(call.id, "Pasto consumato")
58 elif call.data == "cb_other":
59 bot.answer_callback_query(call.id, "oggetto non presente in lista 'frutta'")
60
61
62@bot.message_handler(commands=['inventario'])
63def handle_command_adminwindow(message):
64 # aggiungi una colonna per ogni 7 elementi in matrice
65 rootInventory = ref.child("inventario").get()
66 userPool = []
67 userId = message.from_user.id
68 items = []
69 for i in rootInventory:
70 userPool.append(i)
71 print(f"userId = {userId}")
72 userId = f"{userId}"
73 if userId in userPool:
74 print("Utente trovato, inizio a listare gli oggetti presenti")
75 refItems = ref.child("inventario").child(userId).get()
76 for i in refItems:
77 items.append(i)
78 nitem = len(items)
79 print(f"\nTotale quantità oggetti presenti: {nitem}")
80 while(nitem%7!=0):
81 nitem += 1
82 print(f"\narrotondamento quantità per suddivisione in colonne: {nitem}")
83 print(f"{nitem} / 7 =")
84 ncol = nitem / 7
85 ncol = int(ncol)
86 print(f"{ncol}")
87 #carica query con i nuovi dati
88 bot.send_message(message.chat.id, "Inventario", reply_markup=gen_markup(ncol,items))
89
90
91
92
93bot.polling()