-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainBot.py
102 lines (80 loc) · 4.95 KB
/
mainBot.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
import telebot
import db
import BOT_KEY
bot = telebot.TeleBot(BOT_KEY.KEY)
# main menu keyboard creation
menu_keyboard = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True).add(
telebot.types.KeyboardButton("Exchange"),
telebot.types.KeyboardButton("Rates"),
# telebot.types.KeyboardButton("Graphs"), # will be added later, when code for visualisation is made
)
# takes keys from rates dictionary made in db.py file and uses them to create keys for currencies
currency_keyboard = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True).add(
*[telebot.types.KeyboardButton(currency) for currency in db.rates.keys()]
)
# used to start/reset bot
@bot.message_handler(commands=['start'])
def handle_start(message):
bot.send_message(message.chat.id, "Welcome to the currency converter bot! Please select an option:",
reply_markup=menu_keyboard)
# after "Exchange" is either sent by a button click or manually user is asked question below
@bot.message_handler(func=lambda message: message.text == "Exchange")
def handle_exchange(message):
bot.send_message(message.chat.id, "How much do you want to convert?")
bot.register_next_step_handler(message, handle_exchange_amount)
# user gave a number, now this function sends a message asking [...] and showing a keyboard with currency codes
def handle_exchange_amount(message):
amount = float(message.text)
bot.send_message(message.chat.id,
f"From which currency do you want to convert {amount:.2f}? Please select from the options below:",
reply_markup=currency_keyboard)
# passing all we got so far to the function below
bot.register_next_step_handler(message, handle_exchange_source, amount)
# again a currency codes keyboard is shown, user picks one
def handle_exchange_source(message, amount):
source_currency = message.text
bot.send_message(message.chat.id,
f"What currency do you want to convert {amount:.2f} {source_currency} to? Please select from the options below:",
reply_markup=currency_keyboard)
# and everything is passed to the function below
bot.register_next_step_handler(message, handle_exchange_target, amount, source_currency)
# here all collected data is converted into what user will see in the end
def handle_exchange_target(message, amount, source_currency):
target_currency = message.text
conversion_rates = db.rates
rate = conversion_rates[target_currency] / conversion_rates[source_currency] # conversion rate is calculated here
converted_amount = amount * rate
# final response with converted value
bot.send_message(message.chat.id,
f"{amount:.2f} {source_currency} is equivalent to {converted_amount:.2f} {target_currency}")
# after the work is done user sees menu keys again so there's no need to use /start
bot.send_message(message.chat.id, "What else can I help you with?", reply_markup=menu_keyboard)
# handling user picking "Rates" option and is shown a currency code keyboard, after clicking one function bellow is triggered
@bot.message_handler(func=lambda message: message.text == "Rates")
def handle_rates(message):
bot.send_message(message.chat.id, "Please select the source currency:", reply_markup=currency_keyboard)
bot.register_next_step_handler(message, handle_rates_source)
# user clicks a button for their target currency
def handle_rates_source(message):
source_currency = message.text
bot.send_message(message.chat.id, f"Please select the target currency for {source_currency}:",
reply_markup=currency_keyboard)
bot.register_next_step_handler(message, handle_rates_target, source_currency)
# previous function sends both target and source here, this function calculates rate
def handle_rates_target(message, source_currency):
target_currency = message.text
conversion_rates = db.rates
# here
exchange_rate = conversion_rates[target_currency] / conversion_rates[source_currency]
# and sends it to the chat
bot.send_message(message.chat.id,
f"The exchange rate between {source_currency} and {target_currency} is {exchange_rate:.4f}")
# and shows menu keyboard again
bot.send_message(message.chat.id, "What else can I help you with?", reply_markup=menu_keyboard)
# bot start
bot.polling()
# keys basically send set messages to the chat and message.text collects them and puts in variables, so they can be used
# that's why user can also just send a message manually and if for some reason keyboard doesn't show, if the message
# is correct everything will work if not it'll be just ignored until correct message is provided. What I used for buttons
# works only on mobile, stuff that theoretically should work both on mobile and web app doesn't work in neither of those
# places (and didn't work in class too) and this version looks nicer.