-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
47 lines (36 loc) · 1.94 KB
/
app.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
import telebot
from config import keys, TOKEN
from extensions import ConvertionException, CryptoConverter
# Через pip install в терминале устанавливаем библиотеки requests для запросов. И pytelegrambotapi для взаимодействием с апи телеграма
bot = telebot.TeleBot(TOKEN)
@bot.message_handler(commands=['start','help']) # обработчик
def help(message: telebot.types.Message):
#bot.send_message(message.chat.id, 'hello') #- отправляет hello в ответ
text = '''Шаблон запроса на расчет:
<Название валюты, цену которой хотите узнать:>
<Название валюты, в которой хотите узнать цену:>
<Колличество рассчитываемой валюты:>
Для просмотра доступных валют, нажмите /values'''
bot.reply_to(message, text)
@bot.message_handler(commands=['values'])
def values(message: telebot.types.Message):
text = 'Доступные валюты:'
for key in keys.keys():
text = '\n'.join((text, key, ))
bot.reply_to(message, text)
@bot.message_handler(content_types=['text', ])
def convert(message: telebot.types.Message):
try:
values = message.text.split(' ')
if len(values) != 3:
raise ConvertionException("Слишком много параметров")
quote, base, amount = values
total_base = CryptoConverter.get_price(quote, base, amount)
except ConvertionException as e:
bot.reply_to(message, f'Ошибка пользователя \n{e}')
except Exception as e:
bot.reply_to(message, f'Не удалось обработать команду \n{e}')
else:
text = f'Цена {amount} {quote} в {base} - {total_base}'
bot.send_message(message.chat.id, text)
bot.polling() # запускает бота