Interested in creating a KakaoTalk Chat Bot or explore their APIs but unfortunately you can’t read nor speak Korean? Well, this is the place for you.
I recently discovered KakaoTalk via a couple of South Korean friends that use it for their daily communication. Unfortunately, one of them recently moved to the US and still has a bit of difficulty with communicating in English. As a non-native english speaker, I remember how long it took me to learn it and I wished I just had a quick way to still chat with my friends without having to go on Google Translate every 5 seconds to understand what they are writing. My friends also often write a bunch of messages in group chat in Korean and as someone who understands absolutely nothing, it started to be a bit of a hassle to translate everything into English myself.
Here’s where that idea of creating a KakaoTalk Translation Bot came from.
Bad news, most of the KakaoTalk users are Korean and therefore, when I decided to create that chat bot, there was no resources available in english concerning any kind of KakaoTalk API.
You can see what it looks like in action here:
As you can see the bot can translate from English to Korean and from Korean to English. It also directly attaches the translation as a reply to the message.
So how do create that?
KakaoTalk Unofficial API
KakaoTalk possess a various list of APIs available here: https://developers.kakao.com/. The 2 closest APIs we could use to create a Chat Bot could be this one https://developers.kakao.com/product/kakaoTalkChannel or this one https://developers.kakao.com/product/message. Unfortunately, nothing, at the time this blog post was wrote, exists to directly reads a user conversation and reply to it.
KakaoTalk uses the proprietary LOCO message protocol for its messaging system. I was not successful in finding any good english resources about it but a few unofficial LOCO-compatible projects exist on Github.
The one I uses for my translate bot is https://github.com/storycraft/node-kakao and the thing that caught my attention is this example: https://github.com/storycraft/node-kakao/blob/b3665855481e631f29fad813cd954b29279568e3/examples/login-chat.ts#L30. Looks like that little piece of code is exactly what we want to do: append a text message to a conversation and send it!
Let’s code our Chat Bot
Now that we found our potential way to interact with KakaoTalk it’s time to develop our translation bot. I personally use Papago and its API as my translation engine. You can also use Google Translate or Amazon Translate. The translations won’t be good but I find Papago to be the one that sucks the least.
You will also need to create a new KakaoTalk account and use it as your bot account.
UUID Generator
You also need to get or generate a device UUID.
Create a new node project (npm init
), add node-kakao (npm i -S node-kakao
) and create a file with this script
|
|
Run npx ts-node uuid_generator.ts
in your terminal and get your UUID.
API Connection
Now let’s try to connect to the API. In a new file you can add this code:
|
|
Run the script with npx ts-node server.ts
. If everything went well you should see Login success
in your terminal.
Good! Now let’s see if we can collect the messages sent to our bot.
Catch messages
Here we will use the function CLIENT.on('chat', ...){}
which means ANY message sent to the bot will be processed.
For each message received, we will send back a direct reply with OK
.
|
|
Run the script with the same command as above. Add your bot as a friend to KakaoTalk and send him a message. He should reply to you with OK. Alright now the final part, let’s use the Papago API to translate the messages.
Add the Translator
Add the papago and google translate libraries: npm i -S papago @google-cloud/translate
and go create a Naver developer account here: https://developers.naver.com/main/. You will need to generate a Papago API key.
I’m using Google Translate here to detect the source language.
Now let’s add a translation function to our script and use it in our catch message function:
|
|
Now run the script, send a message and you should your message being translated to Korean! If you send a message in Korean you should also see it being translated to English!
Enjoy your new KakaoTalk translator with your friends.
Sources
You can find the whole project on my Github repo here: https://github.com/tgeselle/kakaotalk-translator