Created
May 14, 2022 10:06
-
-
Save FriggB/f96da4c88e20773c1483b9a5e7c8815e to your computer and use it in GitHub Desktop.
Skill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TOKEN = "5298932751:AAHA2d35PBcfF9VDRKkRPINxRrS89CHujao" | |
access_key = "9736c4721d0b9b4f0b79c8ab23f15d9e" | |
exchanges = { | |
'доллар': 'USD', | |
'евро': 'EUR', | |
"иен": "JPY", | |
"тенге": "KZT", | |
"леев":"MDL", | |
'руб':'RUB'} | |
keys = { | |
'бит':'BTC', | |
'эте':'ETH', | |
'дол':'USD', | |
'евро': 'EUR', | |
'руб':'RUB'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
import telebot | |
from config import * | |
class ConvertionException (Exception): | |
pass | |
class APIException(Exception): | |
pass | |
class Convertor: | |
@staticmethod | |
def get_price(base, quote, amount): | |
try: | |
base_key = exchanges[base.lower()] | |
except KeyError: | |
raise APIException(f"Валюта {base} не найдена!") | |
try: | |
quote_key = exchanges[quote.lower()] | |
except KeyError: | |
raise APIException(f"Валюта {quote} не найдена!") | |
if base_key == quote_key: | |
raise APIException(f'Невозможно перевести одинаковые валюты {base}!') | |
try: | |
amount = float(amount) | |
except ValueError: | |
raise APIException(f'Не удалось обработать количество {amount}!') | |
r = requests.get(f"https://min-api.cryptocompare.com/data/price?fsym={base_key}&tsyms={quote_key}") | |
resp = json.loads(r.content) | |
new_price = resp[quote_key] * amount | |
new_price = round(new_price, 3) | |
message = f"Цена {amount} {base} в {quote} : {new_price}" | |
return message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import telebot | |
from extensions import APIException, Convertor | |
from config import TOKEN, exchanges | |
import traceback | |
from config import * | |
bot = telebot.TeleBot(TOKEN) | |
@bot.message_handler(commands=["start" , "help"]) | |
def rules (message: telebot.types.Message): | |
text = "Для перевода валюты ведите <Что, куда, сколько> \n Для просмотра доступных к обмену валют /values" | |
bot.reply_to(message,text) | |
@bot.message_handler(commands=["values"]) | |
def values (message: telebot.types.Message): | |
text = "Доступные валюты:" | |
for key in exchanges.keys(): | |
text = '\n'.join((text, key )) | |
bot.reply_to(message, text) | |
@bot.message_handler(content_types=['text']) | |
def converter(message: telebot.types.Message): | |
values = message.text.split(' ') | |
try: | |
if len(values) != 3: | |
raise APIException('Неверное количество параметров!') | |
answer = Convertor.get_price(*values) | |
except APIException as e: | |
bot.reply_to(message, f"Ошибка в команде:\n{e}") | |
except Exception as e: | |
traceback.print_tb(e.__traceback__) | |
bot.reply_to(message, f"Неизвестная ошибка:\n{e}") | |
else: | |
bot.reply_to(message, answer) | |
bot.polling() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment