Through the Testing-Glass, & What Drew Found There

This commit is contained in:
Brayden Banks 2016-04-15 19:07:23 +00:00
parent 9b4604daa2
commit 56abfab0b4
27 changed files with 65 additions and 51 deletions

View File

@ -17,7 +17,7 @@ otouto is an independently-developed Telegram API bot written in Lua. Originally
| [Contributors](#contributors) |
## Setup
You _must_ have Lua (5.2+), lua-socket, lua-sec, and lua-cjson installed. To upload files, you must have curl installed. To use fortune.lua, you must have fortune installed.
You _must_ have Lua (5.2+), lua-socket, lua-sec, and dkjson installed. It is recommended you install these with LuaRocks. To upload files, you must have curl installed. To use fortune.lua, you must have fortune installed.
Clone the repository and set the following values in `config.lua`:
@ -26,7 +26,6 @@ Clone the repository and set the following values in `config.lua`:
Optionally:
- `time_offset` as the difference, in seconds, of your system clock to UTC.
- `lang` as the two-letter code representing your language.
Some plugins are not enabled by default. If you wish to enable them, add them to the `plugins` array.

View File

@ -5,7 +5,7 @@
local bindings = {}
local HTTPS = require('ssl.https')
local JSON = require('cjson')
local JSON = require('dkjson')
local URL = require('socket.url')
function bindings.sendRequest(url)

View File

@ -25,7 +25,7 @@
]]--
local JSON = require('cjson')
local JSON = require('dkjson')
local drua = dofile('drua-tg/drua-tg.lua')
local bindings = require('bindings')
local utilities = require('utilities')
@ -196,7 +196,7 @@ function administration:get_desc(chat_id)
for i,v in ipairs(group.rules) do
rulelist = rulelist .. '*' .. i .. '.* ' .. v .. '\n'
end
table.insert(t, rulelist:trim())
table.insert(t, utilities.trim(rulelist))
end
local flaglist = ''
for i = 1, #administration.flags do
@ -205,7 +205,7 @@ function administration:get_desc(chat_id)
end
end
if flaglist ~= '' then
table.insert(t, '*Flags:*\n' .. flaglist:trim())
table.insert(t, '*Flags:*\n' .. utilities.trim(flaglist))
end
if group.governor then
local gov = self.database.users[tostring(group.governor)]
@ -217,7 +217,7 @@ function administration:get_desc(chat_id)
modstring = modstring .. administration.mod_format(self, k)
end
if modstring ~= '' then
table.insert(t, '*Moderators:*\n' .. modstring:trim())
table.insert(t, '*Moderators:*\n' .. utilities.trim(modstring))
end
return table.concat(t, '\n\n')
@ -491,7 +491,7 @@ function administration.init_command(self_)
local gov = self.database.users[tostring(group.governor)]
govstring = '*Governor:* ' .. utilities.md_escape(utilities.build_name(gov.first_name, gov.last_name)) .. ' `[' .. gov.id .. ']`'
end
local output = modstring:trim() ..'\n\n' .. govstring:trim()
local output = utilities.trim(modstring) ..'\n\n' .. utilities.trim(govstring)
if output == '\n\n' then
output = 'There are currently no moderators for this group.'
end
@ -695,13 +695,13 @@ function administration.init_command(self_)
return
end
group.rules = {}
input = input:trim() .. '\n'
input = utilities.trim(input) .. '\n'
local output = '*Rules for* _' .. msg.chat.title .. '_ *:*\n'
local i = 1
for l in input:gmatch('(.-)\n') do
output = output .. '*' .. i .. '.* ' .. l .. '\n'
i = i + 1
table.insert(group.rules, l:trim())
table.insert(group.rules, utilities.trim(l))
end
bindings.sendMessage(self, msg.chat.id, output, true, nil, true)
end
@ -728,7 +728,7 @@ function administration.init_command(self_)
group.motd = nil
bindings.sendReply(self, msg, 'The MOTD has been cleared.')
else
input = input:trim()
input = utilities.trim(input)
group.motd = input
local output = '*MOTD for* _' .. msg.chat.title .. '_ *:*\n' .. input
bindings.sendMessage(self, msg.chat.id, output, true, nil, true)

View File

@ -1,7 +1,7 @@
local apod = {}
local HTTPS = require('ssl.https')
local JSON = require('cjson')
local JSON = require('dkjson')
local URL = require('socket.url')
local bindings = require('bindings')
local utilities = require('utilities')

View File

@ -4,7 +4,7 @@ local chatter = {}
local HTTP = require('socket.http')
local URL = require('socket.url')
local JSON = require('cjson')
local JSON = require('dkjson')
local bindings = require('bindings')
function chatter:init()

View File

@ -32,8 +32,12 @@ function dilbert:action(msg)
return
end
local strip_file = io.open('/tmp/' .. input .. '.gif')
if not strip_file then
local strip_filename = '/tmp/' .. input .. '.gif'
local strip_file = io.open(strip_filename)
if strip_file then
strip_file:close()
strip_file = strip_filename
else
local strip_url = str:match('<meta property="og:image" content="(.-)"/>')
strip_file = utilities.download_file(strip_url, '/tmp/' .. input .. '.gif')
end
@ -42,8 +46,6 @@ function dilbert:action(msg)
bindings.sendPhoto(self, msg.chat.id, strip_file, strip_title)
strip_file:close()
end
return dilbert

View File

@ -3,7 +3,7 @@
local floodcontrol = {}
local JSON = require('cjson')
local JSON = require('dkjson')
local utilities = require('utilities')
function floodcontrol:init()

View File

@ -5,7 +5,7 @@ local gImages = {}
local HTTPS = require('ssl.https')
local URL = require('socket.url')
local JSON = require('cjson')
local JSON = require('dkjson')
local bindings = require('bindings')
local utilities = require('utilities')

View File

@ -2,7 +2,7 @@ local gSearch = {}
local HTTPS = require('ssl.https')
local URL = require('socket.url')
local JSON = require('cjson')
local JSON = require('dkjson')
local bindings = require('bindings')
local utilities = require('utilities')

View File

@ -1,7 +1,7 @@
local hackernews = {}
local HTTPS = require('ssl.https')
local JSON = require('cjson')
local JSON = require('dkjson')
local bindings = require('bindings')
local utilities = require('utilities')

View File

@ -3,7 +3,7 @@
local hearthstone = {}
local HTTPS = require('ssl.https')
local JSON = require('cjson')
local JSON = require('dkjson')
local bindings = require('bindings')
local utilities = require('utilities')
@ -118,7 +118,7 @@ function hearthstone:action(msg)
end
end
output = output:trim()
output = utilities.trim(output)
if output:len() == 0 then
bindings.sendReply(self, msg, self.config.errors.results)
return

View File

@ -2,7 +2,7 @@ local imdb = {}
local HTTP = require('socket.http')
local URL = require('socket.url')
local JSON = require('cjson')
local JSON = require('dkjson')
local bindings = require('bindings')
local utilities = require('utilities')

View File

@ -2,7 +2,7 @@ local lastfm = {}
local HTTP = require('socket.http')
local URL = require('socket.url')
local JSON = require('cjson')
local JSON = require('dkjson')
local bindings = require('bindings')
local utilities = require('utilities')

View File

@ -2,7 +2,7 @@ local librefm = {}
local HTTPS = require('ssl.https')
local URL = require('socket.url')
local JSON = require('cjson')
local JSON = require('dkjson')
local bindings = require('bindings')
local utilities = require('utilities')

View File

@ -19,7 +19,7 @@ function luarun:action(msg)
return
end
local output = loadstring('local utilities = require(\'utilities\'); return function (instance) '..input..' end')()(self)
local output = loadstring('local bindings = require(\'bindings\'); local utilities = require(\'utilities\'); return function (instance, msg) '..input..' end')()(self, msg)
if output == nil then
output = 'Done!'
elseif type(output) == 'table' then

View File

@ -1,7 +1,7 @@
local pokedex = {}
local HTTP = require('socket.http')
local JSON = require('cjson')
local JSON = require('dkjson')
local bindings = require('bindings')
local utilities = require('utilities')

View File

@ -29,7 +29,7 @@ function reactions:init()
for trigger,reaction in pairs(mapping) do
help = help .. '' .. trigger:gsub('.%?', '') .. ': ' .. reaction .. '\n'
table.insert(reactions.triggers, utilities.INVOCATION_PATTERN..trigger)
table.insert(reactions.triggers, utilities.INVOCATION_PATTERN..trigger..'@'..self.username)
table.insert(reactions.triggers, utilities.INVOCATION_PATTERN..trigger..'@'..self.username:lower())
end
end

View File

@ -2,7 +2,7 @@ local reddit = {}
local HTTP = require('socket.http')
local URL = require('socket.url')
local JSON = require('cjson')
local JSON = require('dkjson')
local bindings = require('bindings')
local utilities = require('utilities')

View File

@ -21,7 +21,7 @@ function shout:action(msg)
bindings.sendMessage(self, msg.chat.id, shout.doc, true, msg.message_id, true)
return
end
input = input:trim()
input = utilities.trim(input)
if input:len() > 20 then
input = input:sub(1,20)
@ -42,7 +42,7 @@ function shout:action(msg)
inc = inc + 1
output = output .. match .. ' ' .. spacing .. match .. '\n'
end
output = '```\n' .. output:trim() .. '\n```'
output = '```\n' .. utilities.trim(output) .. '\n```'
bindings.sendMessage(self, msg.chat.id, output, true, false, true)
end

View File

@ -1,7 +1,7 @@
local time = {}
local HTTPS = require('ssl.https')
local JSON = require('cjson')
local JSON = require('dkjson')
local bindings = require('bindings')
local utilities = require('utilities')
@ -33,7 +33,10 @@ function time:action(msg)
return
end
local url = 'https://maps.googleapis.com/maps/api/timezone/json?location=' .. coords.lat ..','.. coords.lon .. '&timestamp='..os.time()
local now = os.time()
local utc = os.time(os.date("!*t", now))
local url = 'https://maps.googleapis.com/maps/api/timezone/json?location=' .. coords.lat ..','.. coords.lon .. '&timestamp='..utc
local jstr, res = HTTPS.request(url)
if res ~= 200 then
@ -43,16 +46,16 @@ function time:action(msg)
local jdat = JSON.decode(jstr)
local now = os.time()
local time_offset = os.difftime(now, os.time(os.date("!*t", now)))
local timestamp = now + jdat.rawOffset + jdat.dstOffset + time_offset
local timestamp = now + jdat.rawOffset + jdat.dstOffset
local utcoff = (jdat.rawOffset + jdat.dstOffset) / 3600
if utcoff == math.abs(utcoff) then
utcoff = '+' .. utcoff
utcoff = '+'.. utilities.pretty_float(utcoff)
else
utcoff = utilities.pretty_float(utcoff)
end
local message = os.date('%I:%M %p\n', timestamp) .. os.date('%A, %B %d, %Y\n', timestamp) .. jdat.timeZoneName .. ' (UTC' .. utcoff .. ')'
local message = os.date('!%I:%M %p\n', timestamp) .. os.date('!%A, %B %d, %Y\n', timestamp) .. jdat.timeZoneName .. ' (UTC' .. utcoff .. ')'
bindings.sendReply(self.msg, message)
bindings.sendReply(self, msg, message)
end

View File

@ -2,7 +2,7 @@ local translate = {}
local HTTPS = require('ssl.https')
local URL = require('socket.url')
local JSON = require('cjson')
local JSON = require('dkjson')
local bindings = require('bindings')
local utilities = require('utilities')

View File

@ -2,7 +2,7 @@ local urbandictionary = {}
local HTTP = require('socket.http')
local URL = require('socket.url')
local JSON = require('cjson')
local JSON = require('dkjson')
local bindings = require('bindings')
local utilities = require('utilities')
@ -43,9 +43,9 @@ function urbandictionary:action(msg)
return
end
local output = '*' .. jdat.list[1].word .. '*\n\n' .. jdat.list[1].definition:trim()
local output = '*' .. jdat.list[1].word .. '*\n\n' .. utilities.trim(jdat.list[1].definition)
if string.len(jdat.list[1].example) > 0 then
output = output .. '_\n\n' .. jdat.list[1].example:trim() .. '_'
output = output .. '_\n\n' .. utilities.trim(jdat.list[1].example) .. '_'
end
output = output:gsub('%[', ''):gsub('%]', '')

View File

@ -1,7 +1,7 @@
local weather = {}
local HTTP = require('socket.http')
local JSON = require('cjson')
local JSON = require('dkjson')
local bindings = require('bindings')
local utilities = require('utilities')

View File

@ -2,7 +2,7 @@ local wikipedia = {}
local HTTPS = require('ssl.https')
local URL = require('socket.url')
local JSON = require('cjson')
local JSON = require('dkjson')
local bindings = require('bindings')
local utilities = require('utilities')

View File

@ -3,7 +3,7 @@ local xkcd = {}
local HTTP = require('socket.http')
local HTTPS = require('ssl.https')
local URL = require('socket.url')
local JSON = require('cjson')
local JSON = require('dkjson')
local bindings = require('bindings')
local utilities = require('utilities')

View File

@ -4,7 +4,7 @@ local youtube = {}
local HTTPS = require('ssl.https')
local URL = require('socket.url')
local JSON = require('cjson')
local JSON = require('dkjson')
local bindings = require('bindings')
local utilities = require('utilities')

View File

@ -7,7 +7,7 @@ local HTTP = require('socket.http')
local ltn12 = require('ltn12')
local HTTPS = require('ssl.https')
local URL = require('socket.url')
local JSON = require('cjson')
local JSON = require('dkjson')
local bindings = require('bindings')
-- get the indexed word in a string
@ -169,6 +169,7 @@ function utilities:user_from_message(msg)
local input = utilities.input(msg.text_lower)
local target = {}
if msg.reply_to_message then
print('reply')
target = msg.reply_to_message.from
elseif input and tonumber(input) then
target.id = tonumber(input)
@ -279,11 +280,12 @@ utilities.INVOCATION_PATTERN = '/'
utilities.triggers_meta = {}
utilities.triggers_meta.__index = utilities.triggers_meta
function utilities.triggers_meta:t(pattern, has_args)
local username = self.username:lower()
table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'$')
table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'@'..self.username..'$')
table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'@'..username..'$')
if has_args then
table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'%s+[^%s]*')
table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'@'..self.username..'%s+[^%s]*')
table.insert(self.table, '^'..utilities.INVOCATION_PATTERN..pattern..'@'..username..'%s+[^%s]*')
end
return self
end
@ -333,4 +335,12 @@ function utilities.enrich_message(msg)
return msg
end
function utilities.pretty_float(x)
if x % 1 == 0 then
return tostring(math.floor(x))
else
return tostring(x)
end
end
return utilities