Skip to content

Instantly share code, notes, and snippets.

@dev4Fun
Last active May 17, 2018 00:47
Show Gist options
  • Save dev4Fun/296ae8b9335d12218b0c23d1ab274aa5 to your computer and use it in GitHub Desktop.
Save dev4Fun/296ae8b9335d12218b0c23d1ab274aa5 to your computer and use it in GitHub Desktop.
def process_trade_selection(bot, update, user_data):
query = update.callback_query
selection = query.data
if selection == OPEN_ORDERS:
orders = self.exchange.fetch_open_orders()
if len(orders) == 0:
bot.edit_message_text(text="You don't have open orders",
chat_id=query.message.chat_id,
message_id=query.message.message_id)
return END_CONVERSATION
# show the option to cancel active orders
keyboard = [
[InlineKeyboardButton("Ok", callback_data=CONFIRM),
InlineKeyboardButton("Cancel order", callback_data=CANCEL)]
]
bot.edit_message_text(text=formatter.format_open_orders(orders),
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=InlineKeyboardMarkup(keyboard))
# attach opened orders, so that we can cancel by index
user_data[OPEN_ORDERS] = orders
return CANCEL_ORD
elif selection == FREE_BALANCE:
balance = self.exchange.free_balance
msg = "You don't have any available balance" if len(balance) == 0 \
else f"Your available balance:\n{formatter.format_balance(balance)}"
bot.edit_message_text(text=msg,
chat_id=query.message.chat_id,
message_id=query.message.message_id)
return END_CONVERSATION
user_data[TRADE_SELECT] = selection
bot.edit_message_text(text=f'Enter coin name for {selection}',
chat_id=query.message.chat_id,
message_id=query.message.message_id)
return COIN_NAME
def cancel_order(bot, update):
query = update.callback_query
if query.data == CANCEL:
query.message.reply_text('Enter order index to cancel: ')
return PROCESS_ORD_CANCEL
show_help(bot, update)
return END_CONVERSATION
def process_order_cancel(bot, update, user_data):
idx = int(update.message.text)
order = user_data[OPEN_ORDERS][idx]
self.exchange.cancel_order(order['id'])
update.message.reply_text(f'Canceled order: {formatter.format_order(order)}')
return END_CONVERSATION
def process_coin_name(bot, update, user_data):
user_data[COIN_NAME] = update.message.text.upper()
update.message.reply_text(f'What amount of {user_data[COIN_NAME]}')
return AMOUNT
def process_amount(bot, update, user_data):
user_data[AMOUNT] = float(update.message.text)
update.message.reply_text(f'What % change for {user_data[AMOUNT]} {user_data[COIN_NAME]}')
return PERCENT_CHANGE
def process_percent(bot, update, user_data):
user_data[PERCENT_CHANGE] = float(update.message.text)
update.message.reply_text(f'What price for 1 unit of {user_data[COIN_NAME]}')
return PRICE
def process_price(bot, update, user_data):
user_data[PRICE] = float(update.message.text)
keyboard = [
[InlineKeyboardButton("Confirm", callback_data=CONFIRM),
InlineKeyboardButton("Cancel", callback_data=CANCEL)]
]
update.message.reply_text(f"Confirm the trade: '{TelegramBot.build_trade(user_data)}'",
reply_markup=InlineKeyboardMarkup(keyboard))
return PROCESS_TRADE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment