Sunday, May 15, 2016

Facebook Messenger chat bot on AWS Lambda

Facebook and Line both announced their own messenger bot platform. Tried to develop a sample bot on AWS Lambda.
Took some time to get the HTTP request passed from AWS API Gateway to AWS Lambda function. Choose to do it in Node.js as I found an example online in medium blog.

Network diagram

Developed a simple bot that spill out the suggested recipe if you send him the ingredients.

For example, you send "Tomato egg" to the bot, it will response with recipe link and photo.

Will publish the source code soon when I have time.

Run Facebook Messenger Chat Bot on AWS Lambda (

Sunday, July 19, 2015

run your own high-end cloud gaming service on EC2 (Windows edition)

Stumbled upon this blog post about using AWS EC2 for gaming.

- The openvpn settings file provided are supposed to be used with TunnelBlick on Mac. Faced some problems with openvpn client in windows. Zerotier or hamachi should be easier to setup. Steam client detected the client after I connect with Zerotier.

- Streamed Final Fantasy XIII-2 and Borderlands 2. Borderlands was playable albeit a little laggy. FFXIII has some issues with the audio, constantly beeping in the background.

- Tried this on 30Mbit broadband. Will test this again when I get higher speed internet connection.

Tuesday, June 18, 2013

Android ADB devices offline

Most of the solutions found on the internet ask you to kill and restart adb server.

adb kill-server
adb start-server

unplug and plug in the mobile phone
uncheck and check the usb debugging option

This does not fix my problem.

Android 4.2.2 has a new security feature where user has to confirm the RSA signature of the computer before it is allowed to connect to the phone. Old version of ADB does not support this feature and will always show your device as offline.

1. Check your adb version and where your adb command points to, on my case, there is an older version of adb at /usr/bin.
adb version
Android Debug Bridge version 1.0.29

type -a adb

2. Delete the old adb and fastboot. Ubuntu's apt-get installed the outdated version.

cd /usr/bin
sudo rm fastboot
sudo rm adb

3. Download ADT/SDK from
4. Update your system PATH and points to the updated adb

gedit .bashrc (ubuntu, replace with any other text editor you like)
add this to the last line of the file

export PATH=$PATH:...../adt-bundle-linux-x86_64-20130522/sdk/platform-tools (please replace it with the path to the sdk you downloaded)

5. Check your version again

type -a adb

adb version
Android Debug Bridge version 1.0.31

6. Plug in your phone and enjoy!

Sunday, April 21, 2013

Ambient Lighting Monitor with Android and Java

Fascinated by Arduino and Raspberry Pi recently. Found a cool application called "Ambient Lighting" which calculates the light on your monitor and changes the color of the LED accordingly. The effect is real time cinema-like movie watching experience.

Note that this video is stolen from

Most of them are using LED with Arduino controller. None of the people think of using the screen of the mobile phone or tablet as a light source (they are probably too dim for the desired effect which I found out later).

This is a quick hack during the weekend, firstly take a screenshot of the current screen. It was easier than I thought using the Java built-in function of java.awt.Robot, createScreenCapture(). After that get all the Red, Green and Blue of each pixels and calculate the average color for the current screen. Ratios of the color according to the different positions such as main color on the top right or bottom left portion of the screen are ignored as my light source is limited (1 or 2 android phone) and could not display different color on each spot.

After that, I need a simple way to send the color data to the android phone. I got lazy and just use Simple Framework to serve HTTP response on configured port. For example, every time the phone send request to http://my_ip:7788, it would return the calculated average color for current screen. To make it easier, I return a simple HTML code to set the background color of the whole page with the calculated RGB value. The page will reload every 1 second using HTTP "META refresh" tag. Reload the page more frequently with javascript should result in a more realistic ambient lighting.

The result is less satisfactory than had been anticipated. The screen of the mobile phone is not as bright as expected even after manually tuning it to the maximum brightness. Probably can improve the effect after tuning the calculated color to make it look brighter. Position information should make multiple phones and tablets act as the "LED", albeit much more expensive. Just for the sake of fun as I have a couple of Android devices hanging around.

I have published the code on bitbucket and feel free to have a look if you are looking to waste some time to enhance your movie watching experience. =)

My code on bitbucket
Ambient light


Sunday, April 22, 2012



用javascript寫的原因是以後有時間可以將之變成chrome extension。Chrome extension主要是由javascript與網頁包裝而成的。那樣就可以隨時翻譯網頁上的注音文了。=)




例:string.replace(new RegExp('ㄦ', 'g'),'er') - 將所有'ㄦ'換成拼音的'er'。

'g' 為global的意思。不那麼寫的話只有第一個matching的字體會被轉換。之後就呼叫alert()將結果顯示出來。

注音符號列表 (維基百科)

Monday, April 9, 2012


雖然這領域日新月異,不過舊式的語言與機器都還是存在的。目前的電腦跟1940-1950時Von Neumann所提出的規格沒什麼兩樣。Python、Ruby等新語言當然可以減輕寫程式的負擔,不過主要的邏輯是沒變的。舉個例子,表達能力沒有問題的話,用英文、中文、日文都沒差,只是需要去瞭解它們的語法與用詞。

C++目前還是很普遍的語言。主要是它的效能比較好,firefox、chrome都是C或C++的啊。就算是Java的VM也是吧。我所知道的,馬來西亞金融機構後端server主要還是使用舊式的電腦(Mainframe)和語言(RPG),主要是因為牽涉太廣,沒必要去更換。可能你看到硬體、手機不斷推陳出新,所謂的雲端,背後還是一堆平凡的電腦。Web 2.0、Web 3.0雖然也會有些不同,不過感覺都是行銷的人想出來的用語。


從Apple的個人電腦、Microsoft的作業系統、Netscape遊覽器、人工分類網頁的Yahoo、利用演算法分類網頁的Google、讓人們互動的Facebook、智能手機、平板電腦、App Store…… 每一波新的潮流出現勢必讓一群人暴富,而且頻率與速度也越來越快。


App Store建立了手機使用者與軟體開發者之間的橋樑,開發者不再被手機廠商、不同國家的電訊業者與關稅條款束縛。只要是有品質的軟體就會被全世界的使用者認同。Angry Birds熱潮后誰會意料到Draw Something的崛起。Draw Something早前已被Zynga收購,謠穿約180 million USD。

這還是一個充滿機會的領域。有能力的人就能突圍而出,創造 "A dent in the universe"。讀著Steve Jobs的自傳,心中燃起了一團火,希望可以維持下去。


Friday, November 26, 2010

Bit and Byte in Java and C++

There are a lot of bitwise operations involved in the C++ code.

signed and unsigned
C++ has "unsigned" type for each of the primitive types, for example char, int, etc.
What it means is to assume all values stored are positive. Bytes that were used to represent positive or negative can be used to stored additional values. Thus, unsigned data type can store up to double the range of the signed data type.

For example,
char in C:
signed: -128 to 127
unsigned: 0 to 255

Java has no "unsigned" keyword. Programmer cannot define signed or unsigned data type. Most of the data types are signed. int, short, long, etc.

The C++ program uses unsigned char, which can stored values between 0-255.

char in C++ and Java
char in Java is Unicode(2 bytes) - support chinese, japanese, etc.
char in C(1 byte) - only supports ASCII character(A-Z, 0-9, etc).

As the char in C++ is 1 byte, and it provides more freedom to play with memory. The C++ code directly manipulate byte and char with XOR operations.

Mapping char to the byte value
The alphabets and numbers are mapped to the same value in ASCII and Unicode. As long as only ASCII characters are used we can be sure that the values returned will fall in the range of 0-127, thus can be stored in byte of Java (-128 to 127). Value of 'A' in Unicode is same as the value in ASCII.

In Java, we just need to call .getBytes() method on the String to get a byte[] array for each of the char in the String. By default it would use the system default charset. We should defined the character encoding to make the result consistent across different platform. For example,getBytes("US-ASCII");

Endian is the way computer store the data in memory. It is fairly low level and normally we don't need to know/care about how the computer arrange the bytes.

Big Endian - 12-34-56-78
Little Endian - 78-56-34-12

Everything in Java is Big Endian while in C++ it depends on the implementation and CPU architecture. On x86 machine, it is Little Endian. I suspect it might affect us as we deal with bitwise operation in C++ and Java.

Luckily we didn't deal with more than 1 byte at a time. Each of the character in the String is mapped to 1 byte value and stored to array. Thus, endianness does not affect the output. '1' is 1, '2' is 2 no matter it is read backward or forward. =)

Zero and 0
Character '0' is not the same as the value 0. Character '0' is represented by value 48 in ASCII.

In the C++ program, 0x00 (0 in hex format) is appended to the char[] array. In this case, value 0 is being appended to the array, not the character '0', which would result in appending value 48 into the array.

In conclusion
Documentation matters. Standard matters.

The XOR part of the code(assume it is some kind of hashing) gives us a hard time compare to the AES encryption. AES encryption produces the same output if confirms to the standard. In Java, it is just a matter of calling the method in the library.

That's what I understand so far. Feel free to correct me if there's any error.

the C++ code is compiled and run on 32-bit machine.