config, locale in lua

personality.lua -> interactions.lua
innumerable improvements
This commit is contained in:
topkecleon 2015-07-15 02:15:23 -04:00
parent 42cda22ab6
commit a1a4978a1b
30 changed files with 289 additions and 303 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
config.lua
loc/weeb.lua

162
README.md
View File

@ -4,16 +4,93 @@ The plugin-wielding, multi-purpose Telegram bot.
Public bot runs on [@mokubot](http://telegram.me/mokubot). Public bot runs on [@mokubot](http://telegram.me/mokubot).
To start, send "/start" or say "Hello, otouto."
##Plugins ##Plugins
<table> <table>
<thead> <thead>
<tr>
<td>help.lua</td>
<td>/help [command]</td>
<td>List commands</td>
</tr>
<tr> <tr>
<td>about.lua</td> <td>about.lua</td>
<td>/about</td> <td>/about</td>
<td>Information about the bot</td> <td>Information about the bot</td>
</tr> </tr>
<tr>
<td>gSearch.lua</td>
<td>/google &lt;query&gt;</td>
<td>Google Search</td>
</tr>
<tr>
<td>gImages.lua</td>
<td>/images &lt;query&gt;</td>
<td>Google Images search</td>
</tr>
<tr>
<td>reddit.lua</td>
<td>/reddit [r/subreddit | query]</td>
<td>Posts from reddit</td>
</tr>
<tr>
<td>giphy.lua</td>
<td>/giphy [query]</td>
<td>Giphy search or random</td>
</tr>
<tr>
<td>xkcd.lua</td>
<td>/xkcd [search]</td>
<td>xkcd strips and alt text</td>
</tr>
<tr>
<td>gMaps.lua</td>
<td>/loc &lt;location&gt;</td>
<td>Google Maps search</td>
</tr>
<tr>
<td>imdb.lua</td>
<td>/imdb &lt;movie | TV series&gt;</td>
<td>IMDb movie/television info</td>
</tr>
<tr>
<td>urbandictionary.lua</td>
<td>/ud &lt;term&gt;</td>
<td>Urban Dictionary search</td>
</tr>
<tr>
<td>hackernews.lua</td>
<td>/hackernews</td>
<td>Top stories from Hackernews</td>
</tr>
<tr>
<td>time.lua</td>
<td>/time &lt;location&gt;</td>
<td>Get the time for a place</td>
</tr>
<tr>
<td>weather.lua</td>
<td>/weather &lt;location&gt;</td>
<td>Get the weather for a place</td>
</tr>
<tr>
<td>calc.lua</td>
<td>/calc &lt;expression&gt;</td>
<td>Solve math expression and convert units</td>
</tr>
<tr>
<td>dice.lua</td>
<td>/roll [arg]</td>
<td>Roll a die. Accepts D&amp;D notation</td>
</tr>
<tr>
<td>remind.lua</td>
<td>/remind &lt;delay&gt; &lt;message&gt;</td>
<td>Set a reminder for yourself or a group</td>
</tr>
<tr> <tr>
<td>8ball.lua</td> <td>8ball.lua</td>
<td>/8ball</td> <td>/8ball</td>
@ -34,21 +111,11 @@ Public bot runs on [@mokubot](http://telegram.me/mokubot).
<td>/btc &lt;currency&gt; [amount]</td> <td>/btc &lt;currency&gt; [amount]</td>
<td>Bitcoin prices and conversion</td> <td>Bitcoin prices and conversion</td>
</tr> </tr>
<tr>
<td>calc.lua</td>
<td>/calc &lt;expression&gt;</td>
<td>Solve math expression and convert units</td>
</tr>
<tr> <tr>
<td>commit.lua</td> <td>commit.lua</td>
<td>/commit</td> <td>/commit</td>
<td>http://whatthecommit.com</td> <td>http://whatthecommit.com</td>
</tr> </tr>
<tr>
<td>dice.lua</td>
<td>/roll [arg]</td>
<td>Roll a die. Accepts D&amp;D notation</td>
</tr>
<tr> <tr>
<td>dogify.lua</td> <td>dogify.lua</td>
<td>/dogify &lt;lines/separatedby/slashes&gt;</td> <td>/dogify &lt;lines/separatedby/slashes&gt;</td>
@ -64,46 +131,11 @@ Public bot runs on [@mokubot](http://telegram.me/mokubot).
<td>/fortune</td> <td>/fortune</td>
<td>Random fortunes</td> <td>Random fortunes</td>
</tr> </tr>
<tr>
<td>gSearch.lua</td>
<td>/google &lt;query&gt;</td>
<td>Google Search</td>
</tr>
<tr>
<td>gMaps.lua</td>
<td>/loc &lt;location&gt;</td>
<td>Google Maps search</td>
</tr>
<tr>
<td>gImages.lua</td>
<td>/images &lt;query&gt;</td>
<td>Google Images search</td>
</tr>
<tr>
<td>giphy.lua</td>
<td>/giphy [query]</td>
<td>Giphy search or random</td>
</tr>
<tr>
<td>hackernews.lua</td>
<td>/hackernews</td>
<td>Top stories from Hackernews</td>
</tr>
<tr>
<td>help.lua</td>
<td>/help [command]</td>
<td>List commands</td>
</tr>
<tr> <tr>
<td>hex.lua</td> <td>hex.lua</td>
<td>/hex &lt;number&gt;</td> <td>/hex &lt;number&gt;</td>
<td>Convert to and from hexadecimal</td> <td>Convert to and from hexadecimal</td>
</tr> </tr>
<tr>
<td>imdb.lua</td>
<td>/imdb &lt;movie | TV series&gt;</td>
<td>IMDb movie/television info</td>
</tr>
<tr> <tr>
<td>pokedex.lua</td> <td>pokedex.lua</td>
<td>/dex &lt;pokemon&gt;</td> <td>/dex &lt;pokemon&gt;</td>
@ -114,46 +146,16 @@ Public bot runs on [@mokubot](http://telegram.me/mokubot).
<td>/pun</td> <td>/pun</td>
<td>Puns</td> <td>Puns</td>
</tr> </tr>
<tr>
<td>reddit.lua</td>
<td>/reddit [r/subreddit | query]</td>
<td>Posts from reddit</td>
</tr>
<tr>
<td>remind.lua</td>
<td>/remind &lt;delay&gt; &lt;message&gt;</td>
<td>Set a reminder for yourself or a group</td>
</tr>
<tr> <tr>
<td>slap.lua</td> <td>slap.lua</td>
<td>/slap [victim]</td> <td>/slap [victim]</td>
<td>Slap someone!</td> <td>Slap someone!</td>
</tr> </tr>
<tr>
<td>time.lua</td>
<td>/time &lt;location&gt;</td>
<td>Get the time for a place</td>
</tr>
<tr>
<td>urbandictionary.lua</td>
<td>/ud &lt;term&gt;</td>
<td>Urban Dictionary search</td>
</tr>
<tr>
<td>weather.lua</td>
<td>/weather &lt;location&gt;</td>
<td>Get the weather for a place</td>
</tr>
<tr> <tr>
<td>whoami.lua</td> <td>whoami.lua</td>
<td>/who</td> <td>/who</td>
<td>Get user and group IDs</td> <td>Get user and group IDs</td>
</tr> </tr>
<tr>
<td>xkcd.lua</td>
<td>/xkcd [search]</td>
<td>xkcd strips and alt text</td>
</tr>
</tbody> </tbody>
</table> </table>
@ -166,7 +168,9 @@ You must have a Telegram bot and auth token from the [BotFather](http://telegram
###Configuration ###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. 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. 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. 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. "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: "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. 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 ##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. 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.

View File

@ -1,9 +1,9 @@
-- bindings.lua -- bindings.lua
-- Functions for the Telegram API. -- Functions for the Telegram API.
-- Requires ssl.https ('HTTPS'), socket.url ('URL'), and a json decoder ('JSON'). -- 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) local function send_request(url)

24
bot.lua
View File

@ -6,7 +6,7 @@ HTTPS = require('ssl.https')
URL = require('socket.url') URL = require('socket.url')
JSON = require('dkjson') JSON = require('dkjson')
VERSION = 2.3 VERSION = 2.4
function on_msg_receive(msg) function on_msg_receive(msg)
@ -31,12 +31,7 @@ function bot_init()
print('\nLoading configuration...') print('\nLoading configuration...')
local j = io.open('config.json') config = dofile('config.lua')
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)
print(#config.plugins .. ' plugins enabled.') print(#config.plugins .. ' plugins enabled.')
@ -56,7 +51,7 @@ function bot_init()
plugins = {} plugins = {}
for i,v in ipairs(config.plugins) do for i,v in ipairs(config.plugins) do
print('',v) print('',v)
local p = loadfile('plugins/'..v)() local p = dofile('plugins/'..v)
table.insert(plugins, p) table.insert(plugins, p)
end end
@ -94,6 +89,10 @@ function process_msg(msg)
msg.from = msg.left_chat_participant msg.from = msg.left_chat_participant
end end
if msg.new_chat_participant and msg.new_chat_participant.id == bot.id then
msg.text = '/about'
end
return msg return msg
end end
@ -103,18 +102,25 @@ reminders = {}
last_update = 0 last_update = 0
while is_started == true do while is_started == true do
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 for i,v in ipairs(get_updates(last_update).result) do
if v.update_id > last_update then if v.update_id > last_update then
last_update = v.update_id last_update = v.update_id
on_msg_receive(v.message) on_msg_receive(v.message)
end end
end end
end
for i,v in pairs(reminders) do for i,v in pairs(reminders) do
if os.time() > v.alarm then if os.time() > v.alarm then
send_message(v.chat_id, 'Reminder: '..v.text) local a = send_message(v.chat_id, 'Reminder: '..v.text)
if a then
table.remove(reminders, i) table.remove(reminders, i)
end end
end end
end
end end

View File

@ -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",
}
}

47
config.lua.default Normal file
View File

@ -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'
}
}

21
loc/en.lua Normal file
View File

@ -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.'
}
}

View File

@ -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."
}

View File

@ -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."
}

View File

@ -13,7 +13,7 @@ PLUGIN.triggers = {
function PLUGIN.action(msg) function PLUGIN.action(msg)
local message = [[ 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. Use /help for a list of commands.
Based on otouto v]] .. VERSION .. [[ by @topkecleon. Based on otouto v]] .. VERSION .. [[ by @topkecleon.
@ -21,7 +21,7 @@ function PLUGIN.action(msg)
topkecleon.github.io/otouto topkecleon.github.io/otouto
]] -- Please do not remove this message. ]] -- Please do not remove this message.
send_msg(msg, message) send_message(msg.chat.id, message, true)
end end

View File

@ -10,7 +10,7 @@ function PLUGIN.action(msg)
local input = get_input(msg.text) local input = get_input(msg.text)
local message = locale.inv_arg local message = config.locale.errors.argument
local sudo = 0 local sudo = 0
for i,v in ipairs(config.admins) do for i,v in ipairs(config.admins) do
@ -24,7 +24,10 @@ function PLUGIN.action(msg)
elseif string.lower(first_word(input)) == 'run' then 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) local output = io.popen(output)
message = output:read('*all') message = output:read('*all')
output:close() output:close()

View File

@ -18,11 +18,11 @@ function PLUGIN.action(msg)
return send_msg(msg, PLUGIN.doc) return send_msg(msg, PLUGIN.doc)
end 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) local message, res = HTTP.request(url)
if res ~= 200 then if res ~= 200 then
message = locale.conn_err message = config.locale.errors.connection
end end
send_msg(msg, message) send_msg(msg, message)

View File

@ -19,7 +19,7 @@ function PLUGIN.action(msg)
local jstr, res = HTTPS.request('https://api.bitcoinaverage.com/ticker/global/') local jstr, res = HTTPS.request('https://api.bitcoinaverage.com/ticker/global/')
if res ~= 200 then if res ~= 200 then
return send_msg(msg, locale.conn_err) return send_msg(msg, config.locale.errors.connection)
end end
local jdat = JSON.decode(jstr) local jdat = JSON.decode(jstr)
@ -29,7 +29,7 @@ function PLUGIN.action(msg)
arg1 = string.upper(string.sub(input, 1, 3)) arg1 = string.upper(string.sub(input, 1, 3))
arg2 = string.sub(input, 5) arg2 = string.sub(input, 5)
if not tonumber(arg2) then if not tonumber(arg2) then
return send_msg(msg, locale.inv_arg) return send_msg(msg, config.locale.errors.argument)
end end
end end
@ -43,7 +43,7 @@ function PLUGIN.action(msg)
if url then if url then
jstr, b = HTTPS.request(url) jstr, b = HTTPS.request(url)
else else
return send_msg(msg, locale.noresults) return send_msg(msg, config.locale.errors.results)
end end
jdat = JSON.decode(jstr) jdat = JSON.decode(jstr)

View File

@ -20,7 +20,7 @@ function PLUGIN.action(msg)
local message, res = HTTP.request(url) local message, res = HTTP.request(url)
if res ~= 200 then if res ~= 200 then
return send_msg(msg, locale.conn_err) return send_msg(msg, config.locale.errors.connection)
end end
send_msg(msg, message) send_msg(msg, message)

View File

@ -33,10 +33,10 @@ function PLUGIN.action(msg)
end end
range = string.sub(input, dloc+1) range = string.sub(input, dloc+1)
if not tonumber(rolls) or not tonumber(range) then 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 end
else else
return send_msg(msg, locale.inv_arg) return send_msg(msg, config.locale.errors.argument)
end end
if tonumber(rolls) == 1 then if tonumber(rolls) == 1 then
@ -44,11 +44,11 @@ function PLUGIN.action(msg)
elseif tonumber(rolls) > 1 then elseif tonumber(rolls) > 1 then
results = rolls .. 'D' .. range .. ':\n' results = rolls .. 'D' .. range .. ':\n'
else else
return send_msg(msg, locale.inv_arg) return send_msg(msg, config.locale.errors.syntax)
end end
if tonumber(range) < 2 then if tonumber(range) < 2 then
return send_msg(msg, locale.inv_arg) return send_msg(msg, config.locale.errors.syntax)
end end
if tonumber(rolls) > 100 or tonumber(range) > 100000 then if tonumber(rolls) > 100 or tonumber(range) > 100000 then

View File

@ -30,22 +30,27 @@ function PLUGIN.action(msg)
local input = get_input(msg.text) local input = get_input(msg.text)
if not input then if not input then
if msg.reply_to_message then
msg = msg.reply_to_message
input = msg.text
else
return send_msg(msg, PLUGIN.doc) return send_msg(msg, PLUGIN.doc)
end end
end
url = url .. '&q=' .. URL.escape(input) url = url .. '&q=' .. URL.escape(input)
local jstr, res = HTTP.request(url) local jstr, res = HTTP.request(url)
if res ~= 200 then if res ~= 200 then
send_msg(msg, locale.conn_err) send_msg(msg, config.locale.errors.connection)
return return
end end
local jdat = JSON.decode(jstr) local jdat = JSON.decode(jstr)
if #jdat.responseData.results < 1 then if #jdat.responseData.results < 1 then
send_msg(msg, locale.noresults) send_msg(msg, config.locale.errors.results)
return return
end end

View File

@ -13,20 +13,25 @@ function PLUGIN.action(msg)
local input = get_input(msg.text) local input = get_input(msg.text)
if not input then if not input then
if msg.reply_to_message then
msg = msg.reply_to_message
input = msg.text
else
return send_msg(msg, PLUGIN.doc) return send_msg(msg, PLUGIN.doc)
end end
end
local url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' .. URL.escape(input) local url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' .. URL.escape(input)
local jstr, res = HTTP.request(url) local jstr, res = HTTP.request(url)
if res ~= 200 then if res ~= 200 then
return send_msg(msg, locale.conn_err) return send_msg(msg, config.locale.errors.connection)
end end
local jdat = JSON.decode(jstr) local jdat = JSON.decode(jstr)
if jdat.status ~= 'OK' then if jdat.status ~= 'OK' then
local message = locale.noresults local message = config.locale.errors.results
return send_msg(msg, message) return send_msg(msg, message)
end end

View File

@ -7,6 +7,7 @@ PLUGIN.doc = [[
PLUGIN.triggers = { PLUGIN.triggers = {
'^/g ', '^/g ',
'^/g$',
'^/google', '^/google',
'^/gnsfw' '^/gnsfw'
} }
@ -25,21 +26,26 @@ function PLUGIN.action(msg)
local input = get_input(msg.text) local input = get_input(msg.text)
if not input then if not input then
if msg.reply_to_message then
msg = msg.reply_to_message
input = msg.text
else
return send_msg(msg, PLUGIN.doc) return send_msg(msg, PLUGIN.doc)
end end
end
url = url .. '&q=' .. URL.escape(input) url = url .. '&q=' .. URL.escape(input)
local jstr, res = HTTP.request(url) local jstr, res = HTTP.request(url)
if res ~= 200 then if res ~= 200 then
return send_msg(msg, locale.conn_err) return send_msg(msg, config.locale.errors.connection)
end end
local jdat = JSON.decode(jstr) local jdat = JSON.decode(jstr)
if #jdat.responseData.results < 1 then if #jdat.responseData.results < 1 then
return send_msg(msg, locale.noresults) return send_msg(msg, config.locale.errors.results)
end end
message = '' message = ''

View File

@ -12,8 +12,8 @@ PLUGIN.triggers = {
function PLUGIN.action(msg) function PLUGIN.action(msg)
local search_url = 'http://api.giphy.com/v1/gifs/search?limit=10&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 random_url = 'http://tv.giphy.com/v1/gifs/random?api_key=' .. config.giphy_api_key
local result_url = '' local result_url = ''
if string.match(msg.text, '^/giphynsfw') then if string.match(msg.text, '^/giphynsfw') then
@ -30,7 +30,7 @@ function PLUGIN.action(msg)
local jstr, res = HTTP.request(random_url) local jstr, res = HTTP.request(random_url)
if res ~= 200 then if res ~= 200 then
return send_msg(msg, locale.conn_err) return send_msg(msg, config.locale.errors.connection)
end end
local jdat = JSON.decode(jstr) local jdat = JSON.decode(jstr)
result_url = jdat.data.image_url result_url = jdat.data.image_url
@ -39,7 +39,7 @@ function PLUGIN.action(msg)
local jstr, res = HTTP.request(search_url .. input) local jstr, res = HTTP.request(search_url .. input)
if res ~= 200 then if res ~= 200 then
return send_msg(msg, locale.conn_err) return send_msg(msg, config.locale.errors.connection)
end end
local jdat = JSON.decode(jstr) local jdat = JSON.decode(jstr)
result_url = jdat.data[math.random(#jdat.data)].images.original.url result_url = jdat.data[math.random(#jdat.data)].images.original.url

View File

@ -20,7 +20,7 @@ function PLUGIN.action(msg)
send_msg(msg, string.format('%x', input)) send_msg(msg, string.format('%x', input))
else else
send_msg(msg, locale.inv_arg) send_msg(msg, config.locale.errors.argument)
end end

View File

@ -13,15 +13,20 @@ function PLUGIN.action(msg)
local input = get_input(msg.text) local input = get_input(msg.text)
if not input then if not input then
if msg.reply_to_message then
msg = msg.reply_to_message
input = msg.text
else
return send_msg(msg, PLUGIN.doc) return send_msg(msg, PLUGIN.doc)
end end
end
local url = 'http://www.imdbapi.com/?t=' .. URL.escape(input) local url = 'http://www.imdbapi.com/?t=' .. URL.escape(input)
local jstr, res = HTTP.request(url) local jstr, res = HTTP.request(url)
local jdat = JSON.decode(jstr) local jdat = JSON.decode(jstr)
if res ~= 200 then if res ~= 200 then
return send_msg(msg, locale.conn_err) return send_msg(msg, config.locale.errors.connection)
end end
if jdat.Response ~= 'True' then if jdat.Response ~= 'True' then

View File

@ -7,6 +7,7 @@ local PLUGIN = {}
PLUGIN.triggers = { PLUGIN.triggers = {
bot.first_name .. '%p?$', bot.first_name .. '%p?$',
'@' .. bot.username .. '%p?$',
'^tadaima%p?$', '^tadaima%p?$',
'^i\'m home%p?$', '^i\'m home%p?$',
'^i\'m back%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 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 if string.match(input, PLUGIN.triggers[i]) then
return send_message(msg.chat.id, 'Welcome back, ' .. msg.from.first_name .. '!') return send_message(msg.chat.id, 'Welcome back, ' .. msg.from.first_name .. '!')
end end
end end
interactions = { for k,v in pairs(config.locale.interactions) do
[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 key,val in pairs(v) do for key,val in pairs(v) do
if input:match(val..',? '..bot.first_name) then 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 end
end end
-- msg.text = '@' .. bot.username .. ', ' .. msg.text:gsub(bot.first_name, '')
-- on_msg_receive(msg)
end end
return PLUGIN return PLUGIN

18
plugins/lmgtfy.lua Normal file
View File

@ -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

View File

@ -12,7 +12,7 @@ PLUGIN.triggers = {
function PLUGIN.action(msg) function PLUGIN.action(msg)
local input = get_input(msg.text) local input = get_input(msg.text:lower())
if not input then if not input then
return send_msg(msg, PLUGIN.doc) return send_msg(msg, PLUGIN.doc)
end end
@ -23,7 +23,7 @@ function PLUGIN.action(msg)
local dex_url = base_url .. '/api/v1/pokemon/' .. input local dex_url = base_url .. '/api/v1/pokemon/' .. input
local dex_jstr, res = HTTP.request(dex_url) local dex_jstr, res = HTTP.request(dex_url)
if res ~= 200 then if res ~= 200 then
return send_msg(msg, locale.noresults) return send_msg(msg, config.locale.errors.results)
end end
local dex_jdat = JSON.decode(dex_jstr) 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_url = base_url .. dex_jdat.descriptions[math.random(#dex_jdat.descriptions)].resource_uri
local desc_jstr, res = HTTP.request(desc_url) local desc_jstr, res = HTTP.request(desc_url)
if res ~= 200 then if res ~= 200 then
return send_msg(msg, locale.conn_err) return send_msg(msg, config.locale.errors.connection)
end end
local desc_jdat = JSON.decode(desc_jstr) local desc_jdat = JSON.decode(desc_jstr)

View File

@ -12,16 +12,18 @@ function PLUGIN.action(msg)
local message = string.lower(msg.text) 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 for k,v in pairs(PLUGIN.triggers) do
if string.match(message, v) then if string.match(message, v) then
return send_msg(msg, k) message = k
end end
end end
if msg.reply_to_message then
send_msg(msg.reply_to_message, message)
else
send_message(msg.chat.id, message)
end
end end
return PLUGIN return PLUGIN

View File

@ -24,11 +24,11 @@ function PLUGIN.action(msg)
local url = 'http://www.reddit.com/' .. first_word(input) .. '/.json' local url = 'http://www.reddit.com/' .. first_word(input) .. '/.json'
local jstr, res = HTTP.request(url) local jstr, res = HTTP.request(url)
if res ~= 200 then if res ~= 200 then
return send_msg(msg, locale.conn_err) return send_msg(msg, config.locale.errors.connection)
end end
jdat = JSON.decode(jstr) jdat = JSON.decode(jstr)
if #jdat.data.children == 0 then if #jdat.data.children == 0 then
return send_msg(msg, locale.noresults) return send_msg(msg, config.locale.errors.results)
end end
else else
@ -36,11 +36,11 @@ function PLUGIN.action(msg)
local url = 'http://www.reddit.com/search.json?q=' .. URL.escape(input) local url = 'http://www.reddit.com/search.json?q=' .. URL.escape(input)
local jstr, res = HTTP.request(url) local jstr, res = HTTP.request(url)
if res ~= 200 then if res ~= 200 then
return send_msg(msg, locale.conn_err) return send_msg(msg, config.locale.errors.connection)
end end
jdat = JSON.decode(jstr) jdat = JSON.decode(jstr)
if #jdat.data.children == 0 then if #jdat.data.children == 0 then
return send_msg(msg, locale.noresults) return send_msg(msg, config.locale.errors.results)
end end
end end
@ -50,7 +50,7 @@ function PLUGIN.action(msg)
url = 'https://www.reddit.com/.json' url = 'https://www.reddit.com/.json'
local jstr, res = HTTP.request(url) local jstr, res = HTTP.request(url)
if res ~= 200 then if res ~= 200 then
return send_msg(msg, locale.conn_err) return send_msg(msg, config.locale.errors.connection)
end end
jdat = JSON.decode(jstr) jdat = JSON.decode(jstr)

View File

@ -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 = {} local PLUGIN = {}
@ -18,22 +18,28 @@ function PLUGIN.action(msg)
return send_msg(msg, PLUGIN.doc) return send_msg(msg, PLUGIN.doc)
end end
coords = get_coords(input) local coords = get_coords(input)
if not coords then if not coords then
return send_msg(msg, locale.noresults) return send_msg(msg, config.locale.errors.results)
end end
local url = 'http://maps.googleapis.com/maps/api/timezone/json?location=' .. coords.lat ..','.. coords.lon .. '&timestamp='..os.time() local url = 'https://maps.googleapis.com/maps/api/timezone/json?location=' .. coords.lat ..','.. coords.lon .. '&timestamp='..os.time()
local jstr, res = HTTPS.request(url) local jstr, res = HTTPS.request(url)
if res ~= 200 then if res ~= 200 then
return send_msg(msg, locale.conn_err) return send_msg(msg, config.locale.errors.connection)
end end
local jdat = JSON.decode(jstr) local jdat = JSON.decode(jstr)
local timestamp = os.time() + jdat.rawOffset + jdat.dstOffset + config.TIME_OFFSET 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 utcoff = (jdat.rawOffset + jdat.dstOffset) / 3600
local message = "The time in " .. timeloc .. " is " .. timestamp 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) send_msg(msg, message)

View File

@ -21,13 +21,13 @@ function PLUGIN.action(msg)
local jstr, res = HTTP.request(url) local jstr, res = HTTP.request(url)
if res ~= 200 then if res ~= 200 then
return send_msg(msg, locale.conn_err) return send_msg(msg, config.locale.errors.connection)
end end
local jdat = JSON.decode(jstr) local jdat = JSON.decode(jstr)
if jdat.result_type == "no_results" then if jdat.result_type == "no_results" then
return send_msg(msg, locale.noresults) return send_msg(msg, config.locale.errors.results)
end end
message = '"' .. jdat.list[1].word .. '"\n' .. trim_string(jdat.list[1].definition) message = '"' .. jdat.list[1].word .. '"\n' .. trim_string(jdat.list[1].definition)

View File

@ -19,13 +19,13 @@ function PLUGIN.action(msg)
coords = get_coords(input) coords = get_coords(input)
if not coords then if not coords then
return send_msg(msg, locale.noresults) return send_msg(msg, config.locale.errors.results)
end end
local url = 'http://api.openweathermap.org/data/2.5/weather?lat=' .. coords.lat .. '&lon=' .. coords.lon local url = 'http://api.openweathermap.org/data/2.5/weather?lat=' .. coords.lat .. '&lon=' .. coords.lon
local jstr, res = HTTP.request(url) local jstr, res = HTTP.request(url)
if res ~= 200 then if res ~= 200 then
return send_msg(msg, locale.conn_err) return send_msg(msg, config.locale.errors.connection)
end end
local jdat = JSON.decode(jstr) local jdat = JSON.decode(jstr)

View File

@ -15,7 +15,7 @@ function PLUGIN.action(msg)
local url = 'http://xkcd.com/info.0.json' local url = 'http://xkcd.com/info.0.json'
local jstr, res = HTTP.request(url) local jstr, res = HTTP.request(url)
if res ~= 200 then if res ~= 200 then
return send_msg(msg, locale.conn_err) return send_msg(msg, config.locale.errors.connection)
end end
local latest = JSON.decode(jstr).num local latest = JSON.decode(jstr).num
@ -24,7 +24,7 @@ function PLUGIN.action(msg)
local jstr, res = HTTP.request(url) local jstr, res = HTTP.request(url)
if res ~= 200 then if res ~= 200 then
print('here') print('here')
return send_msg(msg, locale.conn_err) return send_msg(msg, config.locale.errors.connection)
end end
url = JSON.decode(jstr).responseData.results[1].url .. 'info.0.json' url = JSON.decode(jstr).responseData.results[1].url .. 'info.0.json'
else else
@ -34,7 +34,7 @@ function PLUGIN.action(msg)
local jstr, res = HTTP.request(url) local jstr, res = HTTP.request(url)
if res ~= 200 then if res ~= 200 then
return send_msg(msg, locale.conn_err) return send_msg(msg, config.locale.errors.connection)
end end
local jdat = JSON.decode(jstr) local jdat = JSON.decode(jstr)