This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot/plugins/btc.lua

51 lines
1.3 KiB
Lua
Raw Normal View History

2014-11-28 22:25:38 +01:00
2014-11-28 22:59:52 +01:00
function getBTCX(amount,currency)
2014-11-28 22:25:38 +01:00
-- Do request on bitcoinaverage, the final / is critical!
2014-11-28 22:59:52 +01:00
local res,code = https.request("https://api.bitcoinaverage.com/ticker/global/"..currency.."/")
2014-11-28 22:25:38 +01:00
if code~= 200 then return nil end
local data = json:decode(res)
2014-11-28 22:59:52 +01:00
2014-11-28 22:25:38 +01:00
-- Easy, it's right there
2014-11-28 22:59:52 +01:00
text = "BTC/"..currency..'\n'..'Buy: '..data.ask..'\n'..'Sell: '..data.bid
2014-11-28 22:25:38 +01:00
-- If we have a number as second parameter, calculate the bitcoin amount
2014-11-28 22:59:52 +01:00
if amount~=nil then
btc = tonumber(amount) / tonumber(data.ask)
text = text.."\n "..currency .." "..amount.." = BTC "..btc
2014-11-28 22:25:38 +01:00
end
return text
end
function run(msg, matches)
2014-11-28 22:59:52 +01:00
vardump(matches)
local cur = 'EUR'
local amt = nil
-- Get the global match out of the way
if matches[1] == "!btc" then return getBTCX(amt,cur) end
if matches[2]~=nil then
-- There is a second match
amt = matches[2]
cur = string.upper(matches[1])
else
-- Just a EUR or USD param
cur = string.upper(matches[1])
end
return getBTCX(amt,cur)
2014-11-28 22:25:38 +01:00
end
return {
2014-11-28 22:59:52 +01:00
description = "Bitcoin global average market value (in EUR or USD)",
usage = "!btc [EUR|USD] [amount]",
patterns = {
2014-11-28 22:25:38 +01:00
"^!btc$",
2014-11-28 22:59:52 +01:00
"^!btc ([Ee][Uu][Rr])$",
"^!btc ([Uu][Ss][Dd])$",
"^!btc (EUR) (%d+[%d%.]*)$",
"^!btc (USD) (%d+[%d%.]*)$"
},
run = run
2014-11-28 22:25:38 +01:00
}