If you want to write such a script yourself, you'll mainly have to learn about binary variables and UDP sockets - and, of course, manage to understand the RFC you mentioned. If you just want this functionality as part of something else, then you could take a look at my
nslookup script which can already do what you want.
[13:58.58] * Resolved 154.193.245.88.country.netop.org (TXT) to TR