Home > Blog > Programming > My First Messenger Bot: BilCalendar

I’ve been willing to dive into the world of Facebook Messenger bots and I finally could find some time. Few days ago, I’ve completed coding my first Facebook Messenger bot, BilCalendar.

What is BilCalendar?

BilCalendar provides quick and up-to-date calendar information to Bilkent University students and employees, in both English and Turkish. Normally, you would need to open a new tab, find the relevant webpage and then find the time period that you are interested in to check some calendar events. This way, you just send a command (for example, “this”) to BilCalendar, without even switching the tab while you browse Facebook, and it sends you this week’s information. First World problems, I know. You can also look at the previous/next week, or request the link to look at the whole calendar from the official page. In order to see the full list of commands, just type “commands” (for English) or “komutlar” (for Turkish). You can find the page from here or you can directly send a message from here.

How does it work?

BilCalendar

A screenshot from Facebook Messenger.

It’s written in PHP and I didn’t use any bot framework, because they seemed a bit complicated and they had lots of features that I wasn’t going to use. I’m pretty newbie at PHP and I wanted to keep my code simple. Despite my lack of experience, I chose PHP because it’s very fast and easy to get it work. You don’t even need a VPS, a shared hosting is enough (at least for now). Using Java or Python would be a burden for me, since you can’t run them on everywhere. Also, it was a good opportunity for me to gain some experience with PHP, as I think PHP isn’t dead yet. Anyway, setting up a simple Messenger bot is pretty easy if you follow the tutorials and guides. The thing that took a few days was dealing with parsing, processing, and serving the data.

I periodically run a PHP script (using a cron job) to check if the website has changed, by comparing that page’s hash with the one that I keep on my server. If they are different, I update the hash I keep and start to parse the HTML. This part involves some clever tricks since I had to obtain only the things I needed and in a proper format. This script creates/overwrites four different encoded files (four different calendars) since calendar events of the English Language Preparatory Program are kept separately. Maybe this part could be done a bit more efficiently, but it doesn’t even need to run fast. It’s just a cron job that would update the files very very rarely (most probably one or two times a year). (Update: I realized it’s more frequent than I imagined.) Using less steps would require a more complicated approach and I wanted to keep the code logically simple.

Once a user sends a message to BilCalendar, it checks the current date and it checks the date period the user wants. According to the command, the language that the user is interested in is automatically selected. For example, sending “next” brings the next week in English while sending “gelecek” brings the next week in Turkish. After choosing the correct calendar file and creating a date interval, the bot decodes the information and checks the elements that have a date which matches with one of the dates that is included in that date interval. If there is any, the bot composes a mini calendar and sends it to the user. Also, if an event has some specific keywords like “deadline” or “holiday”, the bot adds a corresponding emoji to that line just for some fun. I obviously omitted some details about processing and formatting the data, but it’s pretty much it.

So…

Is it something revolutionary? No, but I would use it if I were still a student at Bilkent University. I was just looking for an interesting idea for my bot project and I’m very pleased with how it turned out. I learned a lot of things about Messenger bots, PHP, HTML parsing, JSON, and more. I didn’t implement it for my current school (Middle East Technical University) because they weren’t serving the data in a standardized and convenient way. I would also like to send my regards to the people who update the calendar. Although it could be improved, the calendar was so neat and standardized. Similar events were even phrased in a similar way, containing the same keywords. Those things made it possible to fully automatize the system. I may later add new features, but more importantly, I expect it to run smoothly even if I don’t touch anything, as long as the links and the format don’t change. By the way, I’m not proud with the logo. I tried different designs involving a calendar icon and the school’s crest, but… At least the font and the colors succesfully refer to Bilkent University.

Update: Few weeks ago, we had a problem with our in-progress school project. We decided to ditch Python and quickly recode it in PHP. Stuff that I learned while doing this project came in immensely handy.

Update (2018): I moved my stuff from the old server and this service will be offline for a while.

Add comment

ozgunozankilic.com