Build a chatbot with unofficial LINE API in Python
Chances are you have not heard of LINE chat app before. It is the dominant messaging app in Taiwan, Japan, and Thailand. Its counterparts are Whatsapps, WeChat, KakaoTalk. Stickers were actually popularized by LINE, and now we see them on Facebook, Path and other apps.
Currently, there is no official public API for LINE chat. The closest thing has been LINE Official Account, and even that doesn’t give you API access. That is until I came across an unofficial LINE API by Taehoon Kim on github. He reversed engineered the LINE client commination protocol, and put together an easy to use python library with extensive documentation.
Unfortunately, some code had to be removed from the library due to a request from LINE. With a bit of effort and google fu, I was able to fill in the missing code, and this thing works flawlessly! If you decide to play around with the API, you will not be able to login without programming the missing function. Included as an example in the echobot.py, is a simple bot that returns any messages to sender.
Very cool, and this makes for a great starting point to build my own bot.
I wanted to build a bot of my own, but I couldn’t up with any interesting ideas until I remembered the Wikibot on Reddit. So I set off to make a toy version of wikibot for LINE chat. The idea is to use the bot to log on to LINE, and listen in selected chat groups. When a member of the group says, “wiki:{topic}”, I will reply with quick text summary of that topic into that LINE group.
It’s simple, and working as expected. Great.
Wikibot is very cool, but instead of the summary, I also wanted the bot to also answer general knowledge questions. I turned to WolframAlpha, which can do a lot more than solving your tedious math homework, believe it or not. Try typing in a general knowledge, and see the results! WolframAlpha has free API with up to 2000 calls per month, which requires you to sign up. With the API hooked up to the bot, I made the bot look for chat message ending with a question mark, “{question}?” and return a text result from WolframAlpha. Again, a very simple pattern, and here it is in action.