From 5cda6ad9e69c7951a28d9f22a157698224961af1 Mon Sep 17 00:00:00 2001 From: Marcel van der Boom Date: Fri, 28 Nov 2014 22:25:38 +0100 Subject: [PATCH] Initial implemenation of btc command --- plugins/btc.lua | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 plugins/btc.lua diff --git a/plugins/btc.lua b/plugins/btc.lua new file mode 100644 index 0000000..23d5091 --- /dev/null +++ b/plugins/btc.lua @@ -0,0 +1,36 @@ + +function getBTCEUR(eur) + -- Do request on bitcoinaverage, the final / is critical! + local res,code = https.request("https://api.bitcoinaverage.com/ticker/global/EUR/") + + if code~= 200 then return nil end + local data = json:decode(res) + + -- Easy, it's right there + text = "BTC/EUR"..'\n'..'Buy: '..data.ask..'\n'..'Sell: '..data.bid + + -- If we have a number as second parameter, calculate the bitcoin amount + if eur~=nil then + btc = tonumber(eur) / tonumber(data.ask) + text = text.."\n EUR "..eur.." = BTC "..btc + end + return text +end + +function run(msg, matches) + if matches[1] == "!btc" then + return getBTCEUR(nil) + end + return getBTCEUR(matches[1]) +end + +return { + description = "BTCEUR market value", + usage = "!btc [EUR]", + patterns = { + "^!btc$", + "^!btc (%d+[%d%.]*)$", + }, + run = run +} +