diff --git a/config.lua.example b/config.lua.example index 5f7c9c9..f4eb86a 100644 --- a/config.lua.example +++ b/config.lua.example @@ -24,6 +24,10 @@ Sende /hilfe, um zu starten -- false = only whitelisted users can use inline querys -- NOTE that it doesn't matter, if the chat is whitelisted! The USER must be whitelisted! enable_inline_for_everyone = true, + + -- Botan.io statistics + enable_statistics = false, + botan_token = '', errors = { -- Generic error messages used in various plugins. generic = 'An unexpected error occurred.', diff --git a/otouto/bot.lua b/otouto/bot.lua index a22c720..d0dee0a 100644 --- a/otouto/bot.lua +++ b/otouto/bot.lua @@ -239,6 +239,7 @@ function match_plugins(self, msg, config, plugin) end end print(plugin.name..' triggered') + plugin_name = plugin.name return plugin.action(self, msg, config, matches) end) if not success then @@ -250,9 +251,20 @@ function match_plugins(self, msg, config, plugin) elseif plugin.error == nil then utilities.send_reply(self, msg, config.errors.generic, true) end - utilities.handle_exception(self, result, msg.from.id .. ': ' .. msg.text, config) - return + utilities.handle_exception(self, result, msg.from.id .. ': ' .. msg.text, config) + return end + + -- Analytics + if config.enable_analytics and config.botan_token ~= '' then + for _,plugin in ipairs(self.plugins) do + if plugin.name == 'botan' then + print('Analytics') + plugin.action(self, msg, config, nil, plugin_name) + end + end + end + -- If the action returns a table, make that table the new msg. if type(result) == 'table' then msg = result diff --git a/otouto/plugins/botan.lua b/otouto/plugins/botan.lua new file mode 100644 index 0000000..e7a44ac --- /dev/null +++ b/otouto/plugins/botan.lua @@ -0,0 +1,34 @@ +local botan = {} + +local https = require('ssl.https') +local URL = require('socket.url') +local redis = (loadfile "./otouto/redis.lua")() +local utilities = require('otouto.utilities') +local bindings = require('otouto.bindings') + +function botan:init(config) + if not config.botan_token then + print('Missing config value: botan_token.') + print('botan.lua will not be enabled.') + return + end + botan.triggers = { + "^/nil$" + } +end + +local BASE_URL = 'https://api.botan.io/track' + +function botan:appmetrica(text, token, plugin_name) + https.request(BASE_URL..'/?token='..token..'&uid=1&name='..plugin_name) +end + +function botan:action(msg, config, matches, plugin_name) + if not plugin_name then + return + end + + botan:appmetrica(msg.text, config.botan_token, plugin_name) +end + +return botan \ No newline at end of file diff --git a/otouto/utilities.lua b/otouto/utilities.lua index 726fb32..a7602cb 100644 --- a/otouto/utilities.lua +++ b/otouto/utilities.lua @@ -13,6 +13,8 @@ local serpent = require("serpent") local bindings = require('otouto.bindings') local redis = (loadfile "./otouto/redis.lua")() local mimetype = (loadfile "./otouto/mimetype.lua")() +local OAuth = require "OAuth" +local helpers = require "OAuth.helpers" HTTP.timeout = 10 @@ -628,6 +630,36 @@ function is_sudo(msg, config) return var end +-- http://stackoverflow.com/a/6081639/3146627 +function serializeTable(val, name, skipnewlines, depth) + skipnewlines = skipnewlines or false + depth = depth or 0 + + local tmp = string.rep(" ", depth) + + if name then tmp = tmp .. name .. " = " end + + if type(val) == "table" then + tmp = tmp .. "{" .. (not skipnewlines and "\n" or "") + + for k, v in pairs(val) do + tmp = tmp .. serializeTable(v, k, skipnewlines, depth + 1) .. "," .. (not skipnewlines and "\n" or "") + end + + tmp = tmp .. string.rep(" ", depth) .. "}" + elseif type(val) == "number" then + tmp = tmp .. tostring(val) + elseif type(val) == "string" then + tmp = tmp .. string.format("%q", val) + elseif type(val) == "boolean" then + tmp = tmp .. (val and "true" or "false") + else + tmp = tmp .. "\"[inserializeable datatype:" .. type(val) .. "]\"" + end + + return tmp +end + function post_petition(url, arguments, headers) local url, h = string.gsub(url, "http://", "") local url, hs = string.gsub(url, "https://", "") @@ -646,9 +678,9 @@ function post_petition(url, arguments, headers) local source = arguments if type(arguments) == "table" then - local source = helpers.url_encode_arguments(arguments) + source = helpers.url_encode_arguments(arguments) end - + if not headers then request_constructor.headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF8" request_constructor.headers["X-Accept"] = "application/json"