Exemplo n.º 1
0
def cb_coin(update: Update, context: CallbackContext):
    reply_typing(update, context)

    selected_coin = next((coin for coin in context.user_data['coins'] if coin.abbr == update.message.text), None)
    if not selected_coin:
        reply_unknown_coin(update, context)
        reply_cancel_info(update, context)
        return COIN

    rates = api.request_rates_of_coin(selected_coin.id, date.today() - timedelta(days=7))
    if not rates:
        reply_internal_error(update, context, ReplyKeyboardRemove())
        return END

    rates = sorted(rates, key=lambda rate: rate.date)

    plot_path = plot_rates_evolution(
        rate_list=rates,
        bank_name=context.user_data['selected_bank'].registered_name,
        coin_name=selected_coin.abbr,
    )

    update.message.reply_photo(photo=open(plot_path, 'rb'))

    reply_select_coin(update, context)
    reply_cancel_info(update, context)

    return COIN
Exemplo n.º 2
0
def cb_options(update, context):
    reply_typing(update, context)

    coins = api.request_coins_all()
    if not coins:
        reply_internal_error(update, context, ReplyKeyboardRemove())
        return END

    markup = markup_columns(list(set(coin.abbr for coin in coins)), columns=5)
    reply_select_coin(update, context, markup)
    reply_cancel_info(update, context)
    return COIN
Exemplo n.º 3
0
def cb_evolution(update: Update, context: CallbackContext):
    reply_typing(update, context)

    banks = api.request_banks_all()
    if not banks:
        reply_internal_error(update, context, ReplyKeyboardRemove())
        return END

    markup = markup_columns([b.registered_name for b in banks], columns=1)
    reply_select_bank(update, context, markup)
    reply_cancel_info(update, context)

    context.user_data['banks'] = banks

    return BANK
Exemplo n.º 4
0
def cb_best_buy_coins(update, context):
    reply_typing(update, context)

    coins = api.request_coins_with_abbr(update.message.text)
    if not coins:
        reply_unknown_coin(update, context)
        reply_cancel_info(update, context)
        return COIN

    selected_coin_abbr = update.message.text

    rates = api.request_rates_of_coins([coin.id for coin in coins])
    if not rates:
        reply_internal_error(update, context, ReplyKeyboardRemove())
        return END

    banks = api.request_banks_with_ids([coin.bank for coin in coins])
    if not banks:
        reply_internal_error(update, context, ReplyKeyboardRemove())
        return END

    data = list()
    for rate in rates:
        coin = next(coin for coin in coins if coin.id == rate.currency)
        bank = next(bank for bank in banks if bank.id == coin.bank)
        data.append([rate.date, coin.abbr, rate.rate_buy, rate.rate_sell, bank.registered_name])

    # Transpose table
    data = list(map(list, zip(*data)))

    table_path = render_table_options(
        cells=data,
        coin_abbr=selected_coin_abbr,
        actual_date=date.today(),
    )

    update.message.reply_photo(photo=open(table_path, 'rb'))
    reply_cancel_info(update, context)

    return COIN
Exemplo n.º 5
0
def cb_bank(update: Update, context: CallbackContext):
    reply_typing(update, context)

    selected_bank = next((b for b in context.user_data['banks']
                          if b.registered_name == update.message.text), None)
    if not selected_bank:
        reply_unknown_bank(update, context)
        reply_cancel_info(update, context)
        return BANK

    coins = api.request_coins_of_bank(selected_bank.id)
    if not coins:
        reply_internal_error(update, context, ReplyKeyboardRemove())
        return END

    rates = api.request_rates_of_coins([coin.id for coin in coins])
    if not rates:
        reply_internal_error(update, context, ReplyKeyboardRemove())
        return END

    data = list()
    for rate in rates:
        coin = next(coin for coin in coins if coin.id == rate.currency)
        data.append([rate.date, coin.abbr, rate.rate_sell, rate.rate_buy])

    # Transpose table
    data = list(map(list, zip(*data)))

    table_path = render_table_actual(
        cells=data,
        bank_name=selected_bank.registered_name,
        actual_date=date.today(),
    )

    update.message.reply_photo(photo=open(table_path, 'rb'))
    reply_cancel_info(update, context)

    return BANK
Exemplo n.º 6
0
def cb_bank(update: Update, context: CallbackContext):
    reply_typing(update, context)

    selected_bank = next((b for b in context.user_data['banks'] if b.registered_name == update.message.text), None)
    if not selected_bank:
        reply_unknown_bank(update, context)
        reply_cancel_info(update, context)
        return BANK

    context.user_data['selected_bank'] = selected_bank

    coins = api.request_coins_of_bank(selected_bank.id)
    if not coins:
        reply_internal_error(update, context, ReplyKeyboardRemove())
        return END

    markup = markup_columns([coin.abbr for coin in coins], columns=5)
    reply_select_coin(update, context, markup)
    reply_cancel_info(update, context)

    context.user_data['coins'] = coins

    return COIN