2016-04-11 06:04:47 +02:00
|
|
|
local currency = {}
|
|
|
|
|
2016-06-21 18:34:33 +02:00
|
|
|
currency.command = 'cash [Menge] <von> <zu>'
|
2016-05-27 05:28:44 +02:00
|
|
|
|
|
|
|
function currency:init(config)
|
2016-06-17 17:57:40 +02:00
|
|
|
currency.triggers = {
|
|
|
|
"^/cash ([A-Za-z]+)$",
|
|
|
|
"^/cash ([A-Za-z]+) ([A-Za-z]+)$",
|
|
|
|
"^/cash (%d+[%d%.,]*) ([A-Za-z]+) ([A-Za-z]+)$",
|
2016-08-10 19:39:04 +02:00
|
|
|
"^(/cash)$"
|
|
|
|
}
|
|
|
|
currency.inline_triggers = {
|
|
|
|
"^c ([A-Za-z]+)$",
|
|
|
|
"^c ([A-Za-z]+) ([A-Za-z]+)$",
|
|
|
|
"^c (%d+[%d%.,]*) ([A-Za-z]+) ([A-Za-z]+)$"
|
2016-06-17 17:57:40 +02:00
|
|
|
}
|
|
|
|
currency.doc = [[*
|
|
|
|
]]..config.cmd_pat..[[cash* _[Menge]_ _<von>_ _<zu>_
|
2016-08-10 19:39:04 +02:00
|
|
|
*]]..config.cmd_pat..[[cash* _<von>_: Rechnet in Euro um
|
|
|
|
*]]..config.cmd_pat..[[cash* _<von>_ _<zu>_: Rechnet mit der Einheit 1
|
2016-06-17 17:57:40 +02:00
|
|
|
Beispiel: _]]..config.cmd_pat..[[cash 5 USD EUR_]]
|
2016-04-11 06:04:47 +02:00
|
|
|
end
|
2015-08-04 22:11:55 +02:00
|
|
|
|
2016-08-10 19:39:04 +02:00
|
|
|
local BASE_URL = 'https://api.fixer.io'
|
|
|
|
|
|
|
|
function currency:inline_callback(inline_query, config, matches)
|
|
|
|
if not matches[2] then -- first pattern
|
|
|
|
base = 'EUR'
|
|
|
|
to = string.upper(matches[1])
|
2016-06-17 17:57:40 +02:00
|
|
|
amount = 1
|
2016-08-10 19:39:04 +02:00
|
|
|
elseif matches[3] then -- third pattern
|
|
|
|
base = string.upper(matches[2])
|
2016-06-17 17:57:40 +02:00
|
|
|
to = string.upper(matches[3])
|
|
|
|
amount = matches[1]
|
2016-08-10 19:39:04 +02:00
|
|
|
else -- second pattern
|
|
|
|
base = string.upper(matches[1])
|
2016-06-17 17:57:40 +02:00
|
|
|
to = string.upper(matches[2])
|
|
|
|
amount = 1
|
|
|
|
end
|
|
|
|
|
2016-08-10 19:39:04 +02:00
|
|
|
local value, iserr = currency:convert_money(base, to, amount)
|
2016-08-24 15:38:29 +02:00
|
|
|
if iserr then abort_inline_query(inline_query) return end
|
2016-08-10 19:39:04 +02:00
|
|
|
|
|
|
|
local output = amount..' '..base..' = *'..value..' '..to..'*'
|
|
|
|
if tonumber(amount) == 1 then
|
|
|
|
title = amount..' '..base..' entspricht'
|
|
|
|
else
|
|
|
|
title = amount..' '..base..' entsprechen'
|
|
|
|
end
|
|
|
|
local results = '[{"type":"article","id":"20","title":"'..title..'","description":"'..value..' '..to..'","thumb_url":"https://anditest.perseus.uberspace.de/inlineQuerys/currency/cash.jpg","thumb_width":157,"thumb_height":140,"input_message_content":{"message_text":"'..output..'","parse_mode":"Markdown"}}]'
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.answer_inline_query(inline_query, results, 3600)
|
2016-08-10 19:39:04 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function currency:convert_money(base, to, amount)
|
|
|
|
local url = BASE_URL..'/latest?base='..base..'&symbols='..to
|
2016-06-17 17:57:40 +02:00
|
|
|
local amount = string.gsub(amount, ",", ".")
|
2016-08-10 19:39:04 +02:00
|
|
|
local amount = tonumber(amount)
|
|
|
|
local res, code = https.request(url)
|
|
|
|
if code ~= 200 and code ~= 422 then
|
|
|
|
return 'NOCONNECT', true
|
|
|
|
end
|
|
|
|
|
|
|
|
local res, code = https.request(url)
|
|
|
|
local data = json.decode(res)
|
|
|
|
if data.error then
|
|
|
|
return 'WRONGBASE', true
|
|
|
|
end
|
|
|
|
|
|
|
|
local rate = data.rates[to]
|
|
|
|
if not rate then
|
|
|
|
return 'WRONGCONVERTRATE', true
|
|
|
|
end
|
|
|
|
|
|
|
|
if amount == 1 then
|
|
|
|
value = round(rate, 2)
|
|
|
|
else
|
|
|
|
value = round(rate * amount, 2)
|
|
|
|
end
|
|
|
|
local value = tostring(string.gsub(value, "%.", ","))
|
|
|
|
|
|
|
|
return value
|
|
|
|
end
|
|
|
|
|
|
|
|
function currency:action(msg, config, matches)
|
|
|
|
if matches[1] == '/cash' then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, currency.doc, true)
|
2016-08-10 19:39:04 +02:00
|
|
|
return
|
|
|
|
elseif not matches[2] then -- first pattern
|
|
|
|
base = 'EUR'
|
|
|
|
to = string.upper(matches[1])
|
|
|
|
amount = 1
|
|
|
|
elseif matches[3] then -- third pattern
|
|
|
|
base = string.upper(matches[2])
|
|
|
|
to = string.upper(matches[3])
|
|
|
|
amount = matches[1]
|
|
|
|
else -- second pattern
|
|
|
|
base = string.upper(matches[1])
|
|
|
|
to = string.upper(matches[2])
|
|
|
|
amount = 1
|
|
|
|
end
|
|
|
|
|
2016-06-17 17:57:40 +02:00
|
|
|
if from == to then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, 'Jaja, sehr witzig...')
|
2016-06-17 17:57:40 +02:00
|
|
|
return
|
|
|
|
end
|
2016-08-10 19:39:04 +02:00
|
|
|
|
|
|
|
local value = currency:convert_money(base, to, amount)
|
|
|
|
if value == 'NOCONNECT' then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, config.errors.connection)
|
2016-06-17 17:57:40 +02:00
|
|
|
return
|
2016-08-10 19:39:04 +02:00
|
|
|
elseif value == 'WRONGBASE' then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, 'Keine gültige Basiswährung.')
|
2016-08-10 19:39:04 +02:00
|
|
|
return
|
|
|
|
elseif value == 'WRONGCONVERTRATE' then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, 'Keine gültige Umwandlungswährung.')
|
2016-06-17 17:57:40 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-08-10 19:39:04 +02:00
|
|
|
local output = amount..' '..base..' = *'..value..' '..to..'*'
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, output, true)
|
2015-08-04 22:11:55 +02:00
|
|
|
end
|
|
|
|
|
2016-06-17 17:57:40 +02:00
|
|
|
return currency
|