Skip to content

Commit

Permalink
Merge pull request #24 from dcr-guys/feature/dcr
Browse files Browse the repository at this point in the history
  • Loading branch information
fguisso authored Dec 16, 2019
2 parents 5d5c387 + fb8f900 commit 934829e
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 3 deletions.
5 changes: 3 additions & 2 deletions bot/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from bot.commands.base import config_handlers as base_handlers
from bot.commands.subscription import config_handlers as subscription_handlers
from bot.commands.callback import config_handlers as callback_handlers

from bot.commands.exchange import config_handlers as exchange_handlers

handlers = [
base_handlers,
subscription_handlers,
callback_handlers
callback_handlers,
exchange_handlers
]
64 changes: 64 additions & 0 deletions bot/commands/exchange.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import logging

from telegram import Update
from telegram.ext import CommandHandler, CallbackContext

from bot.core import BotTelegramCore
from bot.utils import convert_dcr
from bot.exceptions import DcrDataAPIError, ExchangeAPIError


logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)

logger = logging.getLogger(__name__)


def dcr(update: Update, context: CallbackContext):
target_currency = None
dcr_amount = None

try:
arg_0 = context.args[0]
if arg_0.isdigit():
dcr_amount = float(arg_0)
else:
target_currency = arg_0.upper()
except IndexError:
dcr_amount = 1
target_currency = "USD"

try:
arg_1 = context.args[1]
if arg_1.isdigit():
dcr_amount = float(arg_1)
else:
target_currency = arg_1.upper()
except IndexError:
if target_currency is None:
target_currency = "USD"
if dcr_amount is None:
dcr_amount = 1

try:
target_value = convert_dcr(dcr_amount, target_currency)
except ExchangeAPIError as e:
update.effective_message.reply_text(f"{e}")
return
except DcrDataAPIError as e:
update.effective_message.reply_text("Error requests data from "
"DCRData API.\n "
"Please contact my managers!")
update.effective_message.reply_text(f"{e}")
return

message = f"{dcr_amount} DCR => {target_value:.2f} {target_currency}"

update.effective_message.reply_text(message)


def config_handlers(instance: BotTelegramCore):
logger.info('Setting exchange commands...')

instance.add_handler(CommandHandler("dcr", dcr))
7 changes: 7 additions & 0 deletions bot/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

class ExchangeAPIError(Exception):
pass


class DcrDataAPIError(Exception):
pass
30 changes: 30 additions & 0 deletions bot/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import json

import requests

from bot.exceptions import DcrDataAPIError, ExchangeAPIError


def build_menu(buttons,
Expand All @@ -10,3 +15,28 @@ def build_menu(buttons,
if footer_buttons:
menu.append([footer_buttons])
return menu


def convert_dcr(dcr_amount: float, target_currency: str):
dcrdata_response = requests.get("https://dcrdata.decred.org/api/exchanges")
if dcrdata_response.status_code != 200:
raise DcrDataAPIError(dcrdata_response.content)

dcr_to_usd_value = json.loads(dcrdata_response.content)
dcr_to_usd_value = dcr_to_usd_value.get("price")

if target_currency == 'USD':
return dcr_amount*dcr_to_usd_value

exchangerate_response = requests.get(f"https://api.exchangeratesapi.io/"
f"latest?base=USD"
f"&symbols={target_currency}")
if exchangerate_response.status_code != 200:
raise ExchangeAPIError(f"Currency {target_currency} is not valid!\n"
f"Choose one from "
f"https://api.exchangeratesapi.io/latest")

usd_to_target_value = json.loads(exchangerate_response.content)
usd_to_target_value = usd_to_target_value.get("rates").get(target_currency)

return dcr_amount*dcr_to_usd_value*usd_to_target_value
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ python-decouple==3.3
mongoengine==0.18.2
python-telegram-bot==12.2.0
websocket-client==0.56.0
pendulum==2.0.5
pendulum==2.0.5
requests==2.22.0

0 comments on commit 934829e

Please sign in to comment.