From a1a4978a1bb66cd4c65bed65d8d7ae8e81acee0f Mon Sep 17 00:00:00 2001 From: topkecleon Date: Wed, 15 Jul 2015 02:15:23 -0400 Subject: [PATCH] config, locale in lua personality.lua -> interactions.lua innumerable improvements --- .gitignore | 2 + README.md | 164 +++++++++--------- bindings.lua | 4 +- bot.lua | 34 ++-- config.json | 48 ----- config.lua.default | 47 +++++ loc/en.lua | 21 +++ loc/en_US.json | 40 ----- loc/weeb.json | 46 ----- plugins/about.lua | 4 +- plugins/admin.lua | 7 +- plugins/bible.lua | 4 +- plugins/btc.lua | 6 +- plugins/calc.lua | 2 +- plugins/dice.lua | 8 +- plugins/gImages.lua | 11 +- plugins/gMaps.lua | 11 +- plugins/gSearch.lua | 12 +- plugins/giphy.lua | 8 +- plugins/hex.lua | 2 +- plugins/imdb.lua | 9 +- plugins/{personality.lua => interactions.lua} | 18 +- plugins/lmgtfy.lua | 18 ++ plugins/pokedex.lua | 6 +- plugins/reaction.lua | 12 +- plugins/reddit.lua | 10 +- plugins/time.lua | 24 ++- plugins/urbandictionary.lua | 4 +- plugins/weather.lua | 4 +- plugins/xkcd.lua | 6 +- 30 files changed, 289 insertions(+), 303 deletions(-) create mode 100644 .gitignore delete mode 100644 config.json create mode 100644 config.lua.default create mode 100644 loc/en.lua delete mode 100644 loc/en_US.json delete mode 100644 loc/weeb.json rename plugins/{personality.lua => interactions.lua} (58%) create mode 100644 plugins/lmgtfy.lua diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa4bd13 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +config.lua +loc/weeb.lua diff --git a/README.md b/README.md index 314c6d5..23dbee7 100644 --- a/README.md +++ b/README.md @@ -4,16 +4,93 @@ The plugin-wielding, multi-purpose Telegram bot. Public bot runs on [@mokubot](http://telegram.me/mokubot). +To start, send "/start" or say "Hello, otouto." + ##Plugins - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -34,21 +111,11 @@ Public bot runs on [@mokubot](http://telegram.me/mokubot). - - - - - - - - - - @@ -64,46 +131,11 @@ Public bot runs on [@mokubot](http://telegram.me/mokubot). - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -114,46 +146,16 @@ Public bot runs on [@mokubot](http://telegram.me/mokubot). - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
help.lua/help [command]List commands
about.lua /about Information about the bot
gSearch.lua/google <query>Google Search
gImages.lua/images <query>Google Images search
reddit.lua/reddit [r/subreddit | query]Posts from reddit
giphy.lua/giphy [query]Giphy search or random
xkcd.lua/xkcd [search]xkcd strips and alt text
gMaps.lua/loc <location>Google Maps search
imdb.lua/imdb <movie | TV series>IMDb movie/television info
urbandictionary.lua/ud <term>Urban Dictionary search
hackernews.lua/hackernewsTop stories from Hackernews
time.lua/time <location>Get the time for a place
weather.lua/weather <location>Get the weather for a place
calc.lua/calc <expression>Solve math expression and convert units
dice.lua/roll [arg]Roll a die. Accepts D&D notation
remind.lua/remind <delay> <message>Set a reminder for yourself or a group
8ball.lua /8ball/btc <currency> [amount] Bitcoin prices and conversion
calc.lua/calc <expression>Solve math expression and convert units
commit.lua /commit http://whatthecommit.com
dice.lua/roll [arg]Roll a die. Accepts D&D notation
dogify.lua /dogify <lines/separatedby/slashes>/fortune Random fortunes
gSearch.lua/google <query>Google Search
gMaps.lua/loc <location>Google Maps search
gImages.lua/images <query>Google Images search
giphy.lua/giphy [query]Giphy search or random
hackernews.lua/hackernewsTop stories from Hackernews
help.lua/help [command]List commands
hex.lua /hex <number> Convert to and from hexadecimal
imdb.lua/imdb <movie | TV series>IMDb movie/television info
pokedex.lua /dex <pokemon>/pun Puns
reddit.lua/reddit [r/subreddit | query]Posts from reddit
remind.lua/remind <delay> <message>Set a reminder for yourself or a group
slap.lua /slap [victim] Slap someone!
time.lua/time <location>Get the time for a place
urbandictionary.lua/ud <term>Urban Dictionary search
weather.lua/weather <location>Get the weather for a place
whoami.lua /who Get user and group IDs
xkcd.lua/xkcd [search]xkcd strips and alt text
@@ -166,7 +168,9 @@ You must have a Telegram bot and auth token from the [BotFather](http://telegram ###Configuration -Most config.json entries are self-explanatory. +To begin, copy config.lua.default to config.lua and add the relevant information. + +Most config.lua entries are self-explanatory. Add your bot API key, and other API keys if desirable. The plugins which require API keys that are not provided are disabled by default. @@ -174,12 +178,12 @@ The provided Giphy key is the public test key, and is subject to rate limitaton. The "fortune.lua" plugin requires the fortune program to be installed on the host computer. -TIME_OFFSET is the time difference, in seconds, between your system clock. It is sometimes necessary for accurate output of the time plugin. Default at 0 (duh). +"time_offset" is the time difference, in seconds, between your system clock. It is sometimes necessary for accurate output of the time plugin. "admins" table includes the ID numbers, as integers, of any privileged users. These will have access to the admin plugin and any addition privileged commands. "people" table is for the personality plugin: -`"55994550": "topkecleon"` +`["55994550"] = "topkecleon"` ID number must be a string. The second string is the nickname to be given to the identified user when a personality greeting is triggered. @@ -190,6 +194,6 @@ To run: ##Support -Please do not private message me for support. I will probably block you. +Do not private message me for support. For support for otouto as well as general Lua and bot assistance, please join the [CIS Bot Development](http://telegram.me/joinchat/05fe39f500f8f1b2d1548147a68acd2a) group. After you read the rules and the pastebin, I will assist you there. diff --git a/bindings.lua b/bindings.lua index 122a60a..07e0e97 100644 --- a/bindings.lua +++ b/bindings.lua @@ -1,9 +1,9 @@ -- bindings.lua -- Functions for the Telegram API. -- Requires ssl.https ('HTTPS'), socket.url ('URL'), and a json decoder ('JSON'). --- Also requires config.BOT_API_KEY. +-- Also requires config.bot_api_key. -local BASE_URL = 'https://api.telegram.org/bot' .. config.BOT_API_KEY .. '/' +local BASE_URL = 'https://api.telegram.org/bot' .. config.bot_api_key .. '/' local function send_request(url) diff --git a/bot.lua b/bot.lua index 12ed18c..7ed4591 100644 --- a/bot.lua +++ b/bot.lua @@ -6,7 +6,7 @@ HTTPS = require('ssl.https') URL = require('socket.url') JSON = require('dkjson') -VERSION = 2.3 +VERSION = 2.4 function on_msg_receive(msg) @@ -31,12 +31,7 @@ function bot_init() print('\nLoading configuration...') - local j = io.open('config.json') - local j = j:read('*all') - config = JSON.decode(j) - local j = io.open('loc/'..config.LOCALE..'.json') - local j = j:read('*all') - locale = JSON.decode(j) + config = dofile('config.lua') print(#config.plugins .. ' plugins enabled.') @@ -56,7 +51,7 @@ function bot_init() plugins = {} for i,v in ipairs(config.plugins) do print('',v) - local p = loadfile('plugins/'..v)() + local p = dofile('plugins/'..v) table.insert(plugins, p) end @@ -94,6 +89,10 @@ function process_msg(msg) msg.from = msg.left_chat_participant end + if msg.new_chat_participant and msg.new_chat_participant.id == bot.id then + msg.text = '/about' + end + return msg end @@ -103,17 +102,24 @@ reminders = {} last_update = 0 while is_started == true do - for i,v in ipairs(get_updates(last_update).result) do - if v.update_id > last_update then - last_update = v.update_id - on_msg_receive(v.message) + local result = get_updates(last_update) + if not result then + print('Error getting updates.') + else + for i,v in ipairs(get_updates(last_update).result) do + if v.update_id > last_update then + last_update = v.update_id + on_msg_receive(v.message) + end end end for i,v in pairs(reminders) do if os.time() > v.alarm then - send_message(v.chat_id, 'Reminder: '..v.text) - table.remove(reminders, i) + local a = send_message(v.chat_id, 'Reminder: '..v.text) + if a then + table.remove(reminders, i) + end end end diff --git a/config.json b/config.json deleted file mode 100644 index 5e602c7..0000000 --- a/config.json +++ /dev/null @@ -1,48 +0,0 @@ -{ - "BOT_API_KEY": "", - "BIBLIA_API_KEY": "", - "GIPHY_API_KEY": "dc6zaTOxFJmzC", - "TIME_OFFSET": 0, - "LOCALE": "en_US", - "admins": [ - 0 - ], - "plugins": [ - "about.lua", - "8ball.lua", - "admin.lua", - "bandersnatch.lua", - "btc.lua", - "calc.lua", - "chatter.lua", - "commit.lua", - "dice.lua", - "dogify.lua", - "echo.lua", - "fortune.lua", - "gImages.lua", - "giphy.lua", - "gMaps.lua", - "gSearch.lua", - "hackernews.lua", - "help.lua", - "hex.lua", - "imdb.lua", - "personality.lua", - "pokedex.lua", - "pun.lua", - "reaction.lua", - "reddit.lua", - "remind.lua", - "slap.lua", - "time.lua", - "urbandictionary.lua", - "weather.lua", - "whoami.lua", - "xkcd.lua" - ], - "people": { - "55994550": "topkecleon", - } -} - diff --git a/config.lua.default b/config.lua.default new file mode 100644 index 0000000..4f664d1 --- /dev/null +++ b/config.lua.default @@ -0,0 +1,47 @@ +return { + bot_api_key = '', -- Put your Telegram bot API key here + biblia_api_key = '', -- For bible.lua; get one at biblia.com + giphy_api_key = 'dc6zaTOxFJmzC', -- Public test key + time_offset = 0, -- Offset to be added/subtracted to match UTC + locale = require('loc.en'), + admins = { + 0 + }, + plugins = { + 'about.lua', + 'help.lua', + 'admin.lua', + 'gSearch.lua', + 'gImages.lua', + 'reddit.lua', + 'giphy.lua', + 'xkcd.lua', + 'gMaps.lua', + 'imdb.lua', + 'urbandictionary.lua', + 'hackernews.lua', + 'time.lua', + 'weather.lua', + 'calc.lua', + 'dice.lua', + 'remind.lua', + '8ball.lua', + 'bandersnatch.lua', + 'btc.lua', + 'chatter.lua', + 'commit.lua', + 'dogify.lua', + 'echo.lua', + 'hex.lua', + 'interactions.lua', + 'pokedex.lua', + 'pun.lua', + 'reaction.lua', + 'slap.lua', + 'whoami.lua', + 'lmgtfy.lua' + }, + people = { -- For interactions.lua; ID number and nickname + ['55994550'] = 'topkecleon' + } +} diff --git a/loc/en.lua b/loc/en.lua new file mode 100644 index 0000000..084d69d --- /dev/null +++ b/loc/en.lua @@ -0,0 +1,21 @@ +return { + interactions = { -- Add to this table as you'd like. + ['Hello, #NAME.'] = { + 'hello', + 'hey', + 'hi' + }, + ['Goodbye, #NAME.'] = { + 'bye', + 'later', + 'see ya' + } + }, + errors = { + connection = 'Connection error.', + results = 'No results found.', + argument = 'Invalid argument.', + syntax = 'Invalid syntax.' + } +} + diff --git a/loc/en_US.json b/loc/en_US.json deleted file mode 100644 index 769d850..0000000 --- a/loc/en_US.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "hello": [ - "hello", - "hey", - "hi", - "good morning", - "good day", - "good afternoon", - "good evening" - ], - "goodbye": [ - "bye", - "later", - "see ya", - "good night" - ], - "thankyou": [ - "thanks", - "thank you" - ], - "love": [ - "love you" - ], - "hate": [ - "hate you", - "screw you", - "fuck you", - "go away" - ], - "responses": { - "hello": "Hi", - "goodbye": "Bye-bye", - "thankyou": "No problem", - "love": "You, too", - "hate": "Screw you" - }, - "conn_err": "Connection error.", - "noresults": "No results found.", - "inv_arg": "Invalid argument." -} diff --git a/loc/weeb.json b/loc/weeb.json deleted file mode 100644 index 883aab9..0000000 --- a/loc/weeb.json +++ /dev/null @@ -1,46 +0,0 @@ -{ - "hello": [ - "hello", - "hey", - "hi", - "good morning", - "good day", - "good afternoon", - "good evening", - "ohayou?", - "kon?'?nichiwa", - "konbanwa" - ], - "goodbye": [ - "bye", - "later", - "see ya", - "good night", - "sayou?nara", - "oyasumi" - ], - "thankyou": [ - "thanks", - "thank you", - "arigatou?" - ], - "love": [ - "love you" - ], - "hate": [ - "hate you", - "screw you", - "fuck you", - "go away" - ], - "responses": { - "hello": "Hi", - "goodbye": "Bye-bye", - "thankyou": "No problem", - "love": "You, too", - "hate": "Screw you" - }, - "conn_err": "Connection error.", - "noresults": "No results found.", - "inv_arg": "Invalid argument." -} diff --git a/plugins/about.lua b/plugins/about.lua index 7b3e7a6..4af18c8 100644 --- a/plugins/about.lua +++ b/plugins/about.lua @@ -13,7 +13,7 @@ PLUGIN.triggers = { function PLUGIN.action(msg) local message = [[ - This is ]] .. bot.first_name .. [[: a plugin-wielding, multi-purpose Telegram bot. + I am ]] .. bot.first_name .. [[: a plugin-wielding, multi-purpose Telegram bot. Use /help for a list of commands. Based on otouto v]] .. VERSION .. [[ by @topkecleon. @@ -21,7 +21,7 @@ function PLUGIN.action(msg) topkecleon.github.io/otouto ]] -- Please do not remove this message. - send_msg(msg, message) + send_message(msg.chat.id, message, true) end diff --git a/plugins/admin.lua b/plugins/admin.lua index 6c88297..37a83e1 100644 --- a/plugins/admin.lua +++ b/plugins/admin.lua @@ -10,7 +10,7 @@ function PLUGIN.action(msg) local input = get_input(msg.text) - local message = locale.inv_arg + local message = config.locale.errors.argument local sudo = 0 for i,v in ipairs(config.admins) do @@ -24,7 +24,10 @@ function PLUGIN.action(msg) elseif string.lower(first_word(input)) == 'run' then - local output = string.sub(input, 5) + local output = get_input(input) + if not output then + return send_msg(msg, config.locale.errors.argument) + end local output = io.popen(output) message = output:read('*all') output:close() diff --git a/plugins/bible.lua b/plugins/bible.lua index 0f8cac5..79735bf 100644 --- a/plugins/bible.lua +++ b/plugins/bible.lua @@ -18,11 +18,11 @@ function PLUGIN.action(msg) return send_msg(msg, PLUGIN.doc) end - local url = 'http://api.biblia.com/v1/bible/content/KJV.txt?key=' .. config.BIBLIA_API_KEY .. '&passage=' .. URL.escape(input) + local url = 'http://api.biblia.com/v1/bible/content/KJV.txt?key=' .. config.biblia_api_key .. '&passage=' .. URL.escape(input) local message, res = HTTP.request(url) if res ~= 200 then - message = locale.conn_err + message = config.locale.errors.connection end send_msg(msg, message) diff --git a/plugins/btc.lua b/plugins/btc.lua index a4e94e3..0731a6b 100644 --- a/plugins/btc.lua +++ b/plugins/btc.lua @@ -19,7 +19,7 @@ function PLUGIN.action(msg) local jstr, res = HTTPS.request('https://api.bitcoinaverage.com/ticker/global/') if res ~= 200 then - return send_msg(msg, locale.conn_err) + return send_msg(msg, config.locale.errors.connection) end local jdat = JSON.decode(jstr) @@ -29,7 +29,7 @@ function PLUGIN.action(msg) arg1 = string.upper(string.sub(input, 1, 3)) arg2 = string.sub(input, 5) if not tonumber(arg2) then - return send_msg(msg, locale.inv_arg) + return send_msg(msg, config.locale.errors.argument) end end @@ -43,7 +43,7 @@ function PLUGIN.action(msg) if url then jstr, b = HTTPS.request(url) else - return send_msg(msg, locale.noresults) + return send_msg(msg, config.locale.errors.results) end jdat = JSON.decode(jstr) diff --git a/plugins/calc.lua b/plugins/calc.lua index 152e9dc..c94033d 100644 --- a/plugins/calc.lua +++ b/plugins/calc.lua @@ -20,7 +20,7 @@ function PLUGIN.action(msg) local message, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, locale.conn_err) + return send_msg(msg, config.locale.errors.connection) end send_msg(msg, message) diff --git a/plugins/dice.lua b/plugins/dice.lua index 3d700ba..a92b242 100644 --- a/plugins/dice.lua +++ b/plugins/dice.lua @@ -33,10 +33,10 @@ function PLUGIN.action(msg) end range = string.sub(input, dloc+1) if not tonumber(rolls) or not tonumber(range) then - return send_msg(msg, locale.inv_arg) + return send_msg(msg, config.locale.errors.argument) end else - return send_msg(msg, locale.inv_arg) + return send_msg(msg, config.locale.errors.argument) end if tonumber(rolls) == 1 then @@ -44,11 +44,11 @@ function PLUGIN.action(msg) elseif tonumber(rolls) > 1 then results = rolls .. 'D' .. range .. ':\n' else - return send_msg(msg, locale.inv_arg) + return send_msg(msg, config.locale.errors.syntax) end if tonumber(range) < 2 then - return send_msg(msg, locale.inv_arg) + return send_msg(msg, config.locale.errors.syntax) end if tonumber(rolls) > 100 or tonumber(range) > 100000 then diff --git a/plugins/gImages.lua b/plugins/gImages.lua index 5b9cdb1..efb8a19 100644 --- a/plugins/gImages.lua +++ b/plugins/gImages.lua @@ -30,7 +30,12 @@ function PLUGIN.action(msg) local input = get_input(msg.text) if not input then - return send_msg(msg, PLUGIN.doc) + if msg.reply_to_message then + msg = msg.reply_to_message + input = msg.text + else + return send_msg(msg, PLUGIN.doc) + end end url = url .. '&q=' .. URL.escape(input) @@ -38,14 +43,14 @@ function PLUGIN.action(msg) local jstr, res = HTTP.request(url) if res ~= 200 then - send_msg(msg, locale.conn_err) + send_msg(msg, config.locale.errors.connection) return end local jdat = JSON.decode(jstr) if #jdat.responseData.results < 1 then - send_msg(msg, locale.noresults) + send_msg(msg, config.locale.errors.results) return end diff --git a/plugins/gMaps.lua b/plugins/gMaps.lua index 731712f..45e4819 100644 --- a/plugins/gMaps.lua +++ b/plugins/gMaps.lua @@ -13,20 +13,25 @@ function PLUGIN.action(msg) local input = get_input(msg.text) if not input then - return send_msg(msg, PLUGIN.doc) + if msg.reply_to_message then + msg = msg.reply_to_message + input = msg.text + else + return send_msg(msg, PLUGIN.doc) + end end local url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' .. URL.escape(input) local jstr, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, locale.conn_err) + return send_msg(msg, config.locale.errors.connection) end local jdat = JSON.decode(jstr) if jdat.status ~= 'OK' then - local message = locale.noresults + local message = config.locale.errors.results return send_msg(msg, message) end diff --git a/plugins/gSearch.lua b/plugins/gSearch.lua index 68ff21f..f9dbe1e 100644 --- a/plugins/gSearch.lua +++ b/plugins/gSearch.lua @@ -7,6 +7,7 @@ PLUGIN.doc = [[ PLUGIN.triggers = { '^/g ', + '^/g$', '^/google', '^/gnsfw' } @@ -25,7 +26,12 @@ function PLUGIN.action(msg) local input = get_input(msg.text) if not input then - return send_msg(msg, PLUGIN.doc) + if msg.reply_to_message then + msg = msg.reply_to_message + input = msg.text + else + return send_msg(msg, PLUGIN.doc) + end end url = url .. '&q=' .. URL.escape(input) @@ -33,13 +39,13 @@ function PLUGIN.action(msg) local jstr, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, locale.conn_err) + return send_msg(msg, config.locale.errors.connection) end local jdat = JSON.decode(jstr) if #jdat.responseData.results < 1 then - return send_msg(msg, locale.noresults) + return send_msg(msg, config.locale.errors.results) end message = '' diff --git a/plugins/giphy.lua b/plugins/giphy.lua index 459812c..a1f0c14 100644 --- a/plugins/giphy.lua +++ b/plugins/giphy.lua @@ -12,8 +12,8 @@ PLUGIN.triggers = { function PLUGIN.action(msg) - local search_url = 'http://api.giphy.com/v1/gifs/search?limit=10&api_key=' .. config.GIPHY_API_KEY - local random_url = 'http://tv.giphy.com/v1/gifs/random?api_key=' .. config.GIPHY_API_KEY + local search_url = 'http://api.giphy.com/v1/gifs/search?limit=10&api_key=' .. config.giphy_api_key + local random_url = 'http://tv.giphy.com/v1/gifs/random?api_key=' .. config.giphy_api_key local result_url = '' if string.match(msg.text, '^/giphynsfw') then @@ -30,7 +30,7 @@ function PLUGIN.action(msg) local jstr, res = HTTP.request(random_url) if res ~= 200 then - return send_msg(msg, locale.conn_err) + return send_msg(msg, config.locale.errors.connection) end local jdat = JSON.decode(jstr) result_url = jdat.data.image_url @@ -39,7 +39,7 @@ function PLUGIN.action(msg) local jstr, res = HTTP.request(search_url .. input) if res ~= 200 then - return send_msg(msg, locale.conn_err) + return send_msg(msg, config.locale.errors.connection) end local jdat = JSON.decode(jstr) result_url = jdat.data[math.random(#jdat.data)].images.original.url diff --git a/plugins/hex.lua b/plugins/hex.lua index cc7a6eb..2a369d4 100644 --- a/plugins/hex.lua +++ b/plugins/hex.lua @@ -20,7 +20,7 @@ function PLUGIN.action(msg) send_msg(msg, string.format('%x', input)) else - send_msg(msg, locale.inv_arg) + send_msg(msg, config.locale.errors.argument) end diff --git a/plugins/imdb.lua b/plugins/imdb.lua index 1ab2d85..d08eea1 100644 --- a/plugins/imdb.lua +++ b/plugins/imdb.lua @@ -13,7 +13,12 @@ function PLUGIN.action(msg) local input = get_input(msg.text) if not input then - return send_msg(msg, PLUGIN.doc) + if msg.reply_to_message then + msg = msg.reply_to_message + input = msg.text + else + return send_msg(msg, PLUGIN.doc) + end end local url = 'http://www.imdbapi.com/?t=' .. URL.escape(input) @@ -21,7 +26,7 @@ function PLUGIN.action(msg) local jdat = JSON.decode(jstr) if res ~= 200 then - return send_msg(msg, locale.conn_err) + return send_msg(msg, config.locale.errors.connection) end if jdat.Response ~= 'True' then diff --git a/plugins/personality.lua b/plugins/interactions.lua similarity index 58% rename from plugins/personality.lua rename to plugins/interactions.lua index 60bba7a..16b9093 100644 --- a/plugins/personality.lua +++ b/plugins/interactions.lua @@ -7,6 +7,7 @@ local PLUGIN = {} PLUGIN.triggers = { bot.first_name .. '%p?$', + '@' .. bot.username .. '%p?$', '^tadaima%p?$', '^i\'m home%p?$', '^i\'m back%p?$' @@ -18,31 +19,20 @@ function PLUGIN.action(msg) if config.people[tostring(msg.from.id)] then msg.from.first_name = config.people[tostring(msg.from.id)] end - for i = 2, #PLUGIN.triggers do + for i = 3, #PLUGIN.triggers do if string.match(input, PLUGIN.triggers[i]) then return send_message(msg.chat.id, 'Welcome back, ' .. msg.from.first_name .. '!') end end - interactions = { - [locale.responses.hello] = locale.hello, - [locale.responses.goodbye] = locale.goodbye, - [locale.responses.thankyou] = locale.thankyou, - [locale.responses.love] = locale.love, - [locale.responses.hate] = locale.hate - } - - for k,v in pairs(interactions) do + for k,v in pairs(config.locale.interactions) do for key,val in pairs(v) do if input:match(val..',? '..bot.first_name) then - return send_message(msg.chat.id, k..', '..msg.from.first_name..'!') + return send_message(msg.chat.id, k:gsub('#NAME', msg.from.first_name)) end end end --- msg.text = '@' .. bot.username .. ', ' .. msg.text:gsub(bot.first_name, '') --- on_msg_receive(msg) - end return PLUGIN diff --git a/plugins/lmgtfy.lua b/plugins/lmgtfy.lua new file mode 100644 index 0000000..22bebff --- /dev/null +++ b/plugins/lmgtfy.lua @@ -0,0 +1,18 @@ +local PLUGIN = {} + +PLUGIN.triggers = { + '^/lmgtfy' +} + +function PLUGIN.action(msg) + + if not msg.reply_to_message then return end + msg = msg.reply_to_message + + local message = 'http://lmgtfy.com/?q=' .. URL.escape(msg.text) + + send_msg(msg, message) + +end + +return PLUGIN diff --git a/plugins/pokedex.lua b/plugins/pokedex.lua index ae81e39..70b64ea 100644 --- a/plugins/pokedex.lua +++ b/plugins/pokedex.lua @@ -12,7 +12,7 @@ PLUGIN.triggers = { function PLUGIN.action(msg) - local input = get_input(msg.text) + local input = get_input(msg.text:lower()) if not input then return send_msg(msg, PLUGIN.doc) end @@ -23,7 +23,7 @@ function PLUGIN.action(msg) local dex_url = base_url .. '/api/v1/pokemon/' .. input local dex_jstr, res = HTTP.request(dex_url) if res ~= 200 then - return send_msg(msg, locale.noresults) + return send_msg(msg, config.locale.errors.results) end local dex_jdat = JSON.decode(dex_jstr) @@ -31,7 +31,7 @@ function PLUGIN.action(msg) local desc_url = base_url .. dex_jdat.descriptions[math.random(#dex_jdat.descriptions)].resource_uri local desc_jstr, res = HTTP.request(desc_url) if res ~= 200 then - return send_msg(msg, locale.conn_err) + return send_msg(msg, config.locale.errors.connection) end local desc_jdat = JSON.decode(desc_jstr) diff --git a/plugins/reaction.lua b/plugins/reaction.lua index 033b3eb..938fb43 100644 --- a/plugins/reaction.lua +++ b/plugins/reaction.lua @@ -12,16 +12,18 @@ function PLUGIN.action(msg) local message = string.lower(msg.text) - if msg.reply_to_message then - msg = msg.reply_to_message - end - for k,v in pairs(PLUGIN.triggers) do if string.match(message, v) then - return send_msg(msg, k) + message = k end end + if msg.reply_to_message then + send_msg(msg.reply_to_message, message) + else + send_message(msg.chat.id, message) + end + end return PLUGIN diff --git a/plugins/reddit.lua b/plugins/reddit.lua index e60f3ac..672a78e 100644 --- a/plugins/reddit.lua +++ b/plugins/reddit.lua @@ -24,11 +24,11 @@ function PLUGIN.action(msg) local url = 'http://www.reddit.com/' .. first_word(input) .. '/.json' local jstr, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, locale.conn_err) + return send_msg(msg, config.locale.errors.connection) end jdat = JSON.decode(jstr) if #jdat.data.children == 0 then - return send_msg(msg, locale.noresults) + return send_msg(msg, config.locale.errors.results) end else @@ -36,11 +36,11 @@ function PLUGIN.action(msg) local url = 'http://www.reddit.com/search.json?q=' .. URL.escape(input) local jstr, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, locale.conn_err) + return send_msg(msg, config.locale.errors.connection) end jdat = JSON.decode(jstr) if #jdat.data.children == 0 then - return send_msg(msg, locale.noresults) + return send_msg(msg, config.locale.errors.results) end end @@ -50,7 +50,7 @@ function PLUGIN.action(msg) url = 'https://www.reddit.com/.json' local jstr, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, locale.conn_err) + return send_msg(msg, config.locale.errors.connection) end jdat = JSON.decode(jstr) diff --git a/plugins/time.lua b/plugins/time.lua index dc9502c..5a9f5f5 100644 --- a/plugins/time.lua +++ b/plugins/time.lua @@ -1,4 +1,4 @@ - -- TIME_OFFSET is the number of seconds necessary to correct your system clock to UTC. + -- time_offset is the number of seconds necessary to correct your system clock to UTC. local PLUGIN = {} @@ -18,22 +18,28 @@ function PLUGIN.action(msg) return send_msg(msg, PLUGIN.doc) end - coords = get_coords(input) + local coords = get_coords(input) if not coords then - return send_msg(msg, locale.noresults) + return send_msg(msg, config.locale.errors.results) end - local url = 'http://maps.googleapis.com/maps/api/timezone/json?location=' .. coords.lat ..','.. coords.lon .. '×tamp='..os.time() + local url = 'https://maps.googleapis.com/maps/api/timezone/json?location=' .. coords.lat ..','.. coords.lon .. '×tamp='..os.time() + local jstr, res = HTTPS.request(url) if res ~= 200 then - return send_msg(msg, locale.conn_err) + return send_msg(msg, config.locale.errors.connection) end + local jdat = JSON.decode(jstr) - local timestamp = os.time() + jdat.rawOffset + jdat.dstOffset + config.TIME_OFFSET - timestamp = os.date("%H:%M on %A, %B %d.", timestamp) - local timeloc = (string.gsub((string.sub(jdat.timeZoneId, string.find(jdat.timeZoneId, '/')+1)), '_', ' ')) - local message = "The time in " .. timeloc .. " is " .. timestamp + local timestamp = os.time() + jdat.rawOffset + jdat.dstOffset + config.time_offset + + local utcoff = (jdat.rawOffset + jdat.dstOffset) / 3600 + if utcoff == math.abs(utcoff) then + utcoff = '+' .. utcoff + end + + local message = os.date('%I:%M %p\n', timestamp) .. os.date('%A, %B %d, %Y\n', timestamp) .. jdat.timeZoneName .. ' (UTC' .. utcoff .. ')' send_msg(msg, message) diff --git a/plugins/urbandictionary.lua b/plugins/urbandictionary.lua index 962d7e3..710b159 100644 --- a/plugins/urbandictionary.lua +++ b/plugins/urbandictionary.lua @@ -21,13 +21,13 @@ function PLUGIN.action(msg) local jstr, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, locale.conn_err) + return send_msg(msg, config.locale.errors.connection) end local jdat = JSON.decode(jstr) if jdat.result_type == "no_results" then - return send_msg(msg, locale.noresults) + return send_msg(msg, config.locale.errors.results) end message = '"' .. jdat.list[1].word .. '"\n' .. trim_string(jdat.list[1].definition) diff --git a/plugins/weather.lua b/plugins/weather.lua index 2894ba2..6db27e3 100644 --- a/plugins/weather.lua +++ b/plugins/weather.lua @@ -19,13 +19,13 @@ function PLUGIN.action(msg) coords = get_coords(input) if not coords then - return send_msg(msg, locale.noresults) + return send_msg(msg, config.locale.errors.results) end local url = 'http://api.openweathermap.org/data/2.5/weather?lat=' .. coords.lat .. '&lon=' .. coords.lon local jstr, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, locale.conn_err) + return send_msg(msg, config.locale.errors.connection) end local jdat = JSON.decode(jstr) diff --git a/plugins/xkcd.lua b/plugins/xkcd.lua index d8a1182..9b509fd 100644 --- a/plugins/xkcd.lua +++ b/plugins/xkcd.lua @@ -15,7 +15,7 @@ function PLUGIN.action(msg) local url = 'http://xkcd.com/info.0.json' local jstr, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, locale.conn_err) + return send_msg(msg, config.locale.errors.connection) end local latest = JSON.decode(jstr).num @@ -24,7 +24,7 @@ function PLUGIN.action(msg) local jstr, res = HTTP.request(url) if res ~= 200 then print('here') - return send_msg(msg, locale.conn_err) + return send_msg(msg, config.locale.errors.connection) end url = JSON.decode(jstr).responseData.results[1].url .. 'info.0.json' else @@ -34,7 +34,7 @@ function PLUGIN.action(msg) local jstr, res = HTTP.request(url) if res ~= 200 then - return send_msg(msg, locale.conn_err) + return send_msg(msg, config.locale.errors.connection) end local jdat = JSON.decode(jstr)