-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
300 lines (244 loc) · 10.9 KB
/
main.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import os
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import (
Application,
CommandHandler,
ContextTypes,
MessageHandler,
filters,
CallbackQueryHandler,
)
from pixel_art_gen import generate_and_save_pixel_art
from web3_wrapper import is_valid_ethereum_address, mint_nft
"""
This is where we handle the Telegram bot logic.
Run this script to start the bot.
"""
# Retrieve token from environment variable
TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
if not TOKEN:
raise ValueError(
"No token provided. Set the TELEGRAM_BOT_TOKEN environment variable."
)
"""
These secret numbers are associated with locations in Mallorca,
which the user has to visit to find our NFC-powered QR plaques.
Alternatively, the user can enter the number manually (should be on the plaque).
If the user enters the correct number, the bot displays the location and an image
as a confirmation.
"""
SECRET_NUMBERS = {
"12345": (
"The cliffs of Santaní",
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/51/Mallorca_Santany%C3%AD_Es_Pont%C3%A0s.jpg/640px-Mallorca_Santany%C3%AD_Es_Pont%C3%A0s.jpg",
),
"23456": (
"The bay Caló des Moro ",
"https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/004_2016_11_29_Menschen_im_Urlaub.jpg/574px-004_2016_11_29_Menschen_im_Urlaub.jpg",
),
"34567": (
"Cave on the east coast of Majorca",
"https://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/003_2014_03_15_Hoehlen_Bergwerke_und_Dolinen.jpg/640px-003_2014_03_15_Hoehlen_Bergwerke_und_Dolinen.jpg",
),
"45678": (
"The statue of the beach Na Ferradura",
"https://upload.wikimedia.org/wikipedia/commons/thumb/7/7f/Cala_Rajada_na_Ferradura_escultura_Joan_Benn%C3%A0ssar-1371.jpg/640px-Cala_Rajada_na_Ferradura_escultura_Joan_Benn%C3%A0ssar-1371.jpg",
),
"56789": (
"Cathedral of Palma de Mallorca",
"https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Consulado_del_Mar%2C_Palma_de_Mallorca%2C_Espa%C3%B1a%2C_2022-10-06%2C_DD_28-30_HDR.jpg/640px-Consulado_del_Mar%2C_Palma_de_Mallorca%2C_Espa%C3%B1a%2C_2022-10-06%2C_DD_28-30_HDR.jpg",
),
}
USER_SCORES = {}
WINNERS = set()
AWAITING_IMAGE_DESCRIPTION = set()
AWAITING_DONATION_DETAILS = set()
async def minting_route(update, text):
"""This function handles the minting of NFTs and the corresponding messages.
Args:
update (Update): The Telegram update object.
text (str): The text message from the user.
"""
success7, report = mint_nft(text)
await update.message.reply_text(report)
if success7:
await update.message.reply_text(
"Thank you for your contribution! You will receive your NFT shortly."
)
else:
await update.message.reply_text(
"Sorry, something went wrong. Please try again later."
)
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""The main handler function for the interaction with the bot.
It handles the user messages, obtaining args from deep linking,
and pretty much everything else except the reset command.
Args:
update (Update): The Telegram update object.
context (ContextTypes.DEFAULT_TYPE): The context object.
"""
print("In the start function...")
args = context.args
user = update.effective_user
if args:
args_zero = args[0]
if args_zero in SECRET_NUMBERS:
await handle_number_guess(update, user.id, args[0])
elif is_valid_ethereum_address(args_zero):
await minting_route(update, args_zero)
else:
keyboard = [
[InlineKeyboardButton("Start the Game", callback_data="start_game")]
]
reply_markup = InlineKeyboardMarkup(keyboard)
welcome_message = f"Hello {user.first_name}, I am your bot! Click the button below to start the game."
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=welcome_message,
reply_markup=reply_markup,
)
async def start_new_game(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
text = "Game started! Guess the secret 5-digit numbers associated with locations in Mallorca."
if update.message:
await update.message.reply_text(text)
else:
await update.callback_query.edit_message_text(text)
async def start_game_callback(
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
"""A callback query handler that starts a new game when the user clicks the "Start game" button.
Args:
update (Update): The Telegram update object.
context (ContextTypes.DEFAULT_TYPE): The context object.
"""
query = update.callback_query
await query.answer()
await start_new_game(update, context)
async def handle_donation_details(update: Update, user_id: int, text: str) -> None:
"""Calculates the donation amount based on the user input and sends a message with how to make the donation.
Args:
update (Update): The Telegram update object.
user_id (int): The user ID.
text (str): The user input text. The expected format is two numbers separated by a space.
"""
try:
area, months = map(int, text.split())
donation_amount = area * months * 1 # €1 per square meter per month
text = f"""
The total donation amount would be €{donation_amount}.
If you're happy with the amount, please contribute here:
https://bytemeblockheads.tiiny.site/
"""
await update.message.reply_text(text)
AWAITING_DONATION_DETAILS.remove(user_id)
text = f"""If you're one of the cool crypto guys, you can also donate by purchasing our NFT.
We will mint a unique NFT for you, with the cool image you just generated! Enter your Ethereum address to receive the NFT (for example, 0x6e339091198CdfbAfE5942e8d4198aC7F84b470e):
"""
await update.message.reply_text(text)
except ValueError:
await update.message.reply_text(
"Please enter two numbers: the area in square meters and the number of months."
)
async def handle_pixel_art_request(update: Update, user_id: int, text: str) -> None:
"""Generates a pixel art image based on the user input and sends it to the user.
Args:
update (Update): The Telegram update object.
user_id (int): The user ID.
text (str): The user input text. The text can be anything. E.g. "funny puppy"
"""
await update.message.reply_text(
"Got it. Generating your unique pixel art... (takes about 10 sec)"
)
object_to_paint = text
GENERATED_IMAGE_URL = generate_and_save_pixel_art(object_to_paint)
await update.message.reply_photo(
photo=GENERATED_IMAGE_URL, caption="Here is your custom pixel art!"
)
AWAITING_IMAGE_DESCRIPTION.remove(user_id)
AWAITING_DONATION_DETAILS.add(user_id)
text = f"""Do you want to support the restoration of Mallorca beaches? If yes, please enter the area (in square meters) you want to take stuardship for, and for how many months. For exampe:
1 12"""
await update.message.reply_text(text)
async def handle_number_guess(update: Update, user_id: int, text: str) -> None:
"""Checks if the user input is a valid secret number and updates the user score.
Args:
update (Update): The Telegram update object.
user_id (int): The user ID.
text (str): The user input text. The expected format is a str of a 5-digit number.
"""
if text in SECRET_NUMBERS:
if user_id not in USER_SCORES:
USER_SCORES[user_id] = 0
USER_SCORES[user_id] += 1
location, image_url = SECRET_NUMBERS[text]
congratulations_message = f"Correct! You found our secret plaque in {location}. Your score is now {USER_SCORES[user_id]}."
await update.message.reply_photo(
photo=image_url, caption=congratulations_message
)
if USER_SCORES[user_id] == 2:
WINNERS.add(user_id)
AWAITING_IMAGE_DESCRIPTION.add(user_id)
await update.message.reply_text(
"Congratulations! You won! As a prize, enter a description of anything, and I'll generate a cool pixel art image of it!"
)
else:
await update.message.reply_text("Wrong number, try again!")
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Processes user messages during the conversation, according to the state of the user.
Args:
update (Update): The Telegram update object.
context (ContextTypes.DEFAULT_TYPE): The context object.
"""
text = update.message.text
user_id = update.effective_user.id
if user_id not in USER_SCORES:
USER_SCORES[user_id] = 0
if user_id in AWAITING_DONATION_DETAILS:
await handle_donation_details(update, user_id, text)
return
if user_id in WINNERS and user_id in AWAITING_IMAGE_DESCRIPTION:
await handle_pixel_art_request(update, user_id, text)
return
if text.isdigit() and len(text) == 5:
await handle_number_guess(update, user_id, text)
else:
if user_id not in WINNERS:
await update.message.reply_text("Please enter a valid 5-digit number.")
text = text.strip()
if is_valid_ethereum_address(text):
await minting_route(update, text)
async def reset_score(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Resets the user score and starts a new game. Useful for testing.
Args:
update (Update): The Telegram update object.
context (ContextTypes.DEFAULT_TYPE): The context object.
"""
user_id = update.effective_user.id
# remove the user from the winners list
if user_id in WINNERS:
WINNERS.remove(user_id)
# remove the user from the awaiting image description list
if user_id in AWAITING_IMAGE_DESCRIPTION:
AWAITING_IMAGE_DESCRIPTION.remove(user_id)
# remove the user from the awaiting donation details list
if user_id in AWAITING_DONATION_DETAILS:
AWAITING_DONATION_DETAILS.remove(user_id)
if user_id in USER_SCORES:
USER_SCORES[user_id] = 0
await update.message.reply_text(
"Your score has been reset. Starting a new game..."
)
else:
await update.message.reply_text(
"You don't have a score to reset. Starting a new game..."
)
await start_new_game(update, context)
def main():
app = Application.builder().token(TOKEN).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(CallbackQueryHandler(start_game_callback, pattern="^start_game$"))
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
app.add_handler(CommandHandler("reset", reset_score))
app.run_polling()
if __name__ == "__main__":
main()