Kimchi Premium Telegram bot by a python beginner

As a non-programmer who has work experiences as a project manager and is interested in IT development, I have learnt basic python from a Udemy course since pandemic is all over the world. and as many people aware, the kimchi premium was more than 10–15% in 2021 April-May, and I decided to create my own telegram bot to have a notification of kimchi premium rate as well as to practice my python with a real use case.

t.me/kimp_tracker_bot

What is kimchi premium by the way?

Kimchi premium is basically a price gap between South Korea crypto exchanges and other countries. The price difference may be caused by many different reasons such as a strong capital control by Korea government or/and by a lack of high-return investment options for investors in South Korea. Personally as a Korean expat out of South Korea, i more believe that the reason is a strong capital and financial control by governments. it is difficult to take out my Korean won to out of Korea… too many regulation, too high transfer fees.

What does this kimchi premium bot do?

this kimchi premium telegram bot is returning the real time price differences between Upbit (Korean exchange) and Bitkub(Thailand exchange) — KRW vs THB. you can set up an alert to receive a notification when the kimchi premium rate is higher/lower than a specific % per crypto currency. in the future I am planning to add bithumb exchange for EUR.

I have uploaded my code in my github repository. as an open source and I would like to receive feedback/advice on my code by posting this blog :)

Let’s look into the code!

everything is based on ‘functions’ in python. (limited to my knowledge lol)

‘start’ function to greet with a message by simply replying with update.message.reply_text( ). this start function will be called when execution at the end of the code.

‘kimpnow’ function to calculate the diff % from Bitkub and Upbit price per crypto currency. here is the big chunk of the code. now here we need to call Bitkub API and Upbit API to extract the real time price of each cryto currency and calculate the %, and display all per crypto currency.

in order to do so, first, I have created separate functions (exchange_upbit, exchange_bitkub) to be embedded in ‘kimpnow’ function. with each exchange function, I can get real time price of crypto currency i wanted.

as each price is in different fiat currency (THB, KRW), I need foreign exchange rate to compare. based on my research, people usually use USDT for USD forex(it is hard to find the free forex API). but my bot is for THB and KRW, which we don’t have a solid stablecoin for yet. so I needed to have a real forex API, luckily I got one from dunamu. here is the get_forex function to be embedded in kimpnow function.

now I have functions to get real price data from each exchange (exchange_upbit, exchange_bitkub), function to extract the foreign exchange rate (get_forex). with these, let’s build the kimpnow function to get kimchi premium rate!

as you can see, I have a list(crypto_list) in global to indicate 4 crypto currencies that I want to check kimchi premium rate for.

with the list, kimpnow function get forex from get_forex, and with the forex, I used for and in method to go through crypto_list to calculate kimp (kimchi premium) and display messages. with these functions above, you will be able to receive premium rate with a command ‘/kimpnow’ as below screenshot! *the command to display message is something that we will indicate at the end of the code.

now, I would like to add ‘alert’ function to set up a notification to get alert when the premium reach a specific %. ( a key functionality of the bot!)

first of all, I will get input from a command /alert.“/alert ETH > 1” meaning, I would like to get alert when ETH of kimchi premium is more than 1%. I followed one blog that I found for creating an arbitrage telegram bot (Thank you so much!)

as you can see, I get the input with context.arg[] for type of crypto currency, higher/lower and rate. and I have created ‘alert_id’ per user (chat_id) to set up multiple alerts (maximum 3), and once the alert is set, using AlertCallback function(to come) to keep checking the price per 60 seconds.

so here, AlertCallBack function should do : check the kimp, if kimp reach the specific rate that we input earlier, return the message with rate Or keep checking as following.

Now so far you have alert notification set! (command to display message, input the message are at the end of code)

have you followed well so far? with all above codes, finally it’s time to build the main function to keep running the bot.

CommandHandler is for the command in telegram ‘/’ and especially as /alert command has an input after, you need to pass through with pass_args.

I have hosted my bot in Heroku with webhook, so that’s where updater.start_webhook sits. there are many blogs to guide you how to host your telegram bot in Heroku, so please feel free to research!

what do you think? it is quite simple and do-able for even non-programmer! for me it took literally one week to establish the bot with above functions after testing and testing. I am sure for professional programmer, it may take only 1–2 hours to complete (haha). bur for me it was a great learning and I am proud of myself to accomplish my first real use case bot for me to use. I am happy to share the code and I am going to enhance the bots continuously (as there are some known issues) by posting in kimptracker twitter. please stay tuned and feel free to add comments/feedback/advice please! last word that I would like to share :

Everyone can do the coding!

I will post another blog to talk about my lessons learned from product management point of view.

A non-programmer who is interested in automation, globalization and financial independent