This question has been asked before, with a few good in depth answers by u/sterio
https://www.reddit.com/r/AskHistorians/comments/1dc20m/how_did_siam_thailand_avoid_colonization_by/