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-2/plugins/btc.lua

58 lines
1.1 KiB
Lua
Raw Normal View History

2015-07-03 00:15:52 +02:00
local PLUGIN = {}
PLUGIN.doc = [[
2015-07-08 09:38:04 +02:00
/btc <currency> [amount]
2015-07-03 00:15:52 +02:00
Gives bitcoin prices for the given currency, and optionally conversion of an amount to and from that currency.
BitcoinAverage Price Index https://bitcoinaverage.com/
]]
PLUGIN.triggers = {
2015-07-08 09:38:04 +02:00
'^/btc'
2015-07-03 00:15:52 +02:00
}
function PLUGIN.action(msg)
local url = nil
local arg1 = 'USD'
local arg2 = 1
local jstr, res = HTTPS.request('https://api.bitcoinaverage.com/ticker/global/')
if res ~= 200 then
return send_msg(msg, 'Connection error.')
end
local jdat = JSON.decode(jstr)
2015-07-09 04:56:50 +02:00
local input = get_input(msg.text)
if input then
arg1 = string.upper(string.sub(input, 1, 3))
arg2 = string.sub(input, 5)
2015-07-03 14:31:54 +02:00
if not tonumber(arg2) then
return send_msg(msg, 'Invalid argument.')
end
2015-07-03 00:15:52 +02:00
end
for k,v in pairs(jdat) do
if k == arg1 then
url = v .. '/'
break
end
end
if url then
jstr, b = HTTPS.request(url)
else
return send_msg(msg, 'Error: Currency not found.')
end
jdat = JSON.decode(jstr)
local m = arg2 .. ' BTC = ' .. jdat['24h_avg']*arg2 ..' '.. arg1 .. '\n'
m = m .. arg2 ..' '.. arg1 .. ' = ' .. string.format("%.8f", arg2/jdat['24h_avg']) .. ' BTC'
send_msg(msg, m)
end
return PLUGIN