Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F633535
currency.mattata
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
1 KB
Referenced Files
None
Subscribers
None
currency.mattata
View Options
--[[
Based on a plugin by topkecleon.
Copyright 2020 Matthew Hesketh <wrxck0@gmail.com>
This code is licensed under the MIT. See LICENSE for details.
]]
local currency = {}
local mattata = require('mattata')
local https = require('ssl.https')
function currency:init()
currency.commands = mattata.commands(self.info.username):command('currency'):command('convert'):command('cash').table
currency.help = '/currency <amount> <from> to <to> - Converts exchange rates for various currencies via Google Finance. Aliases: /convert, /cash.'
currency.url = 'https://www.google.com/finance/converter?from=%s&to=%s&a=%s'
end
function currency.on_message(_, message, _, language)
local input = mattata.input(message.text:upper())
if input then
input = input:gsub('%$', 'USD'):gsub('€', 'EUR'):gsub('£', 'GBP')
end
if not input or not input:match('^.- %a%a%a TO %a%a%a$') then
return mattata.send_reply(message, currency.help)
end
local amount, from, to = input:match('^(.-) (%a%a%a) TO (%a%a%a)$')
amount = tonumber(amount) or 1
local result = 1
if from ~= to then
local str, res = https.request(string.format(currency.url, from, to, amount))
if res ~= 200 then
return mattata.send_reply(message, language.errors.connection)
end
str = str:match('<span class=bld>(.-) %u+</span>')
if not str then
return mattata.send_reply(message, language.errors.results)
end
result = string.format('%.2f', str)
end
result = string.format('%s %s = %s %s', amount, from, result, to)
return mattata.send_message(message.chat.id, result)
end
return currency
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Nov 15, 5:07 PM (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
134137
Default Alt Text
currency.mattata (1 KB)
Attached To
Mode
R69 mattata
Attached
Detach File
Event Timeline