· 4 years ago · Jun 06, 2021, 11:32 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:AAEYyR1wiI_KLNxoqQFhgFb3kIXN5YLUzEY'
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 #carica query con i nuovi dati
42 inventario(message, items)
43
44def inventario(message, items):
45 # Lista oggetti scaricati dall'inventario in cloud, pronti al confronto per l'associazione per tipo elemento
46 markup = InlineKeyboardMarkup()
47 listFruits = ["mela", "banana", "fragole", "Ciliegie", "lampone", "mirtillo"]
48 idGiocatore = message.from_user.id
49 idMsg = message.message_id
50 i = 0
51 while i < len(items):
52 row = []
53 if items[i] in listFruits:
54 row.append(InlineKeyboardButton(text=items[i], callback_data=f"{idGiocatore}-cb_f{items[i]}"))
55 else:
56 row.append(InlineKeyboardButton(text=items[i], callback_data=f"cb_other"))
57 i += 1
58 if items[i] in listFruits:
59 row.append(InlineKeyboardButton(text=items[i], callback_data=f"{idGiocatore}-cb_f{items[i]}"))
60 else:
61 row.append(InlineKeyboardButton(text=items[i], callback_data=f"cb_other"))
62 i += 1
63 markup.row(*row)
64
65
66
67
68 '''#aggiungi il pasto e scala oggetto
69 fame = ref.child("Pasti").child(idGiocatore).get()
70 ref.child("Pasti").child(idGiocatore).set(fame + 1)
71 bot.answer_callback_query(call.id, f"Hai consumato 1 {oggetto}")'''
72
73 bot.send_message(message.chat.id, "Puoi controllare il tuo inventario nella chat privata con il bot\nlink al bot da prendere sul sito")
74 bot.send_message(message.from_user.id, 'Inventario', reply_markup=markup)
75
76
77
78@bot.callback_query_handler(func=lambda call: True)
79def callback_query(call):
80 markup = InlineKeyboardMarkup()
81 row = []
82 row.append(InlineKeyboardButton(text="usa", callback_data=f"cb_use"))
83 row.append(InlineKeyboardButton(text="scarta", callback_data=f"cb_trash"))
84 markup.row(*row)
85
86 row = []
87 row.append(InlineKeyboardButton(text="< Torna indietro", callback_data=f"cb_main"))
88 markup.row(*row)
89 if "cb_f" in call.data:
90 item = call.data
91 item = item.split('-')
92 idGiocatore = item[0]
93 oggetto = item[1]
94 oggetto = oggetto[4:]
95
96 qta = ref.child("inventario").child(idGiocatore).child(oggetto).child("qta").get()
97
98 # modifica query definendo usa, scarta e torna indietro
99 cid = call.message.chat.id
100 mid = call.message.message_id
101 try:
102 bot.edit_message_text(f"{oggetto} [x{qta}]", cid, mid, reply_markup=markup)
103 except:
104 pass
105
106
107 elif call.data == "cb_other":
108 bot.answer_callback_query(call.id, "È stato selezionato altro")
109
110 # Menu usa, scarta, torna indietro
111 elif call.data == "cb_main":
112 bot.answer_callback_query(call.id, "Torna indietro in costruzione...")
113 elif call.data == "cb_use":
114 bot.answer_callback_query(call.id, "Utilizzo oggetti in costruzione...")
115 elif call.data == "cb_trash":
116 bot.answer_callback_query(call.id, "Scarto oggetti in costruzione...")
117
118bot.polling()