-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextensions.py
110 lines (86 loc) · 3.89 KB
/
extensions.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
import json
import requests
import telebot
import config
class APIException(Exception):
def __init__(self, message):
super().__init__(message)
class CurrencyConvertor:
"""
Class for currency convert API service: https://free.currconv.com/api/v7/convert
"""
available_currencies = ('RUB', 'EUR', 'USD', 'GBP', 'AUD', 'JPY')
@staticmethod
def get_price(base: str, quote: str, amount: str) -> str:
if any([base.upper() not in CurrencyConvertor.available_currencies, quote.upper() not in CurrencyConvertor.available_currencies]):
raise APIException(f'Base or quote currency not int available currencies: {CurrencyConvertor.available_currencies}')
try:
amount = float(amount.replace(',', '.'))
except ValueError:
raise APIException(f'Amount must be a number (integer or float) but not {amount}')
params = {
'apiKey': config.CURR_CONV_API_KEY,
'q': f'{base}_{quote}'
}
try:
api_result = requests.get(url=config.CURR_CONV_API_URL, params=params)
except ConnectionError:
raise APIException(f'Connection error to {config.CURR_CONV_API_URL}')
else:
if api_result.status_code == 200:
response = json.loads(api_result.content)['results'][params['q']]['val']
try:
response = float(response)
except ValueError:
raise APIException(f'Something wrong, in API response wrong or empty value: "{response}"')
else:
return f'{response * amount}'
else:
raise APIException(f'Something wrong, API response code is {api_result.status_code}')
class TelegramBot:
"""
Class for Telegram bot service
Note:
With class-based notation don't work @<bot_object>.<handler>
I'm use <bot_object>.register_<handler> as alternative
"""
def __init__(self):
self._bot_name = config.TELEGRAM_BOT_NAME
self._token = config.TELEGRAM_BOT_TOKEN
self._bot = None
try:
self._bot = telebot.TeleBot(self._token)
except Exception as e:
raise APIException('Something wrong... {e.message}')
else:
self._bot.register_message_handler(self.start, commands=['start'])
self._bot.register_message_handler(self.help, commands=['help'])
self._bot.register_message_handler(self.values, commands=['values'])
self._bot.register_message_handler(self.parser, content_types=['text'])
self._bot.infinity_polling()
def start(self, message):
self._bot.send_message(message.chat.id, f"Welcome, {message.chat.username} \nSend /help for usage help")
def help(self, message):
help_msg = 'Send: convert <amount_base> <base_currency> <quote_currency>\n' \
'Example: convert 100 USD RUB\n' \
'Result: 7070.0 RUB\n' \
'\n' \
'Send: /values for view all available currencies'
self._bot.send_message(message.chat.id, f"{help_msg}")
def values(self, message):
self._bot.send_message(message.chat.id, f"Available currencies: {CurrencyConvertor.available_currencies}")
def parser(self, message):
try:
result = ''
command, amount, base, quote = message.text.split()
if command == 'convert':
# result = quote of base * amount
result = CurrencyConvertor.get_price(base=base, quote=quote, amount=amount)
result = f"Result: {result} {quote}"
else:
result = 'Wrong input, send /help for usage help'
except APIException as e:
print(f'{e}')
self._bot.send_message(message.chat.id, f"Error: {e}")
else:
self._bot.send_message(message.chat.id, result)