Merge pull request #21 from heitorPB/master

Added APOD plugin.
This commit is contained in:
Drew 2016-01-14 14:47:16 -05:00
commit 16b3d10762
3 changed files with 79 additions and 3 deletions

View File

@ -19,6 +19,7 @@ Here is a list of most otouto plugins.
|--------|---------|----------|-------| |--------|---------|----------|-------|
| help.lua | /help | Returns a list of commands. | /h | | help.lua | /help | Returns a list of commands. | /h |
| about.lua | /about | Returns the about text as configured in config.lua. | | about.lua | /about | Returns the about text as configured in config.lua. |
| apod.lua | /apod [query] | Gets Astronomy Picture of the Day for current day, or for a specific date (YYYY-MM-DD). | |
| ping.lua | /ping | The simplest plugin ever! | | ping.lua | /ping | The simplest plugin ever! |
| echo.lua | /echo <text> | Repeats a string of text. | | echo.lua | /echo <text> | Repeats a string of text. |
| gSearch.lua | /google <query> | Returns Google web results. | /g, /gnsfw | | gSearch.lua | /google <query> | Returns Google web results. | /g, /gnsfw |
@ -93,7 +94,7 @@ Where the key is the preconfigured response (where #NAME will be replaced with t
##Setup ##Setup
You **must** have Lua (5.2+), LuaSocket, and LuaSec installed. For uploading photos and other files, you must have curl installed. The fortune.lua plugin requires that fortune is installed. You **must** have Lua (5.2+), LuaSocket, and LuaSec installed. For uploading photos and other files, you must have curl installed. The fortune.lua plugin requires that fortune is installed.
For weather.lua, lastfm.lua, and bible.lua to work, you must have API keys for [OpenWeatherMap](http://openweathermap.org), [last.fm](http://last.fm), and [Biblia.com](http://biblia.com), respectively. cats.lua uses an API key (via [The Cat API](http://thecatapi.com)) to get more results, though it is not required. For weather.lua, lastfm.lua, and bible.lua to work, you must have API keys for [OpenWeatherMap](http://openweathermap.org), [last.fm](http://last.fm), and [Biblia.com](http://biblia.com), respectively. cats.lua uses an API key (via [The Cat API](http://thecatapi.com)) to get more results, though it is not required. apod.lua uses an API key (via [NASA API](https://api.nasa.gov/)) to have more queries per minute, but it can be used with a demo API key (DEMO_KEY).
**Before you do anything, open config.lua in a text editor and make the following changes:** **Before you do anything, open config.lua in a text editor and make the following changes:**
@ -145,5 +146,5 @@ otouto uses dkjson, a pure-Lua JSON parser. This is provided with the code and d
##Contributors ##Contributors
The creator and maintainer of otouto is [topkecleon](http://github.com/topkecleon). He can be contacted via [Telegram](http://telegram.me/topkecleon), [Twitter](http://twitter.com/topkecleon), or [email](mailto:topkecleon@outlook.com). The creator and maintainer of otouto is [topkecleon](http://github.com/topkecleon). He can be contacted via [Telegram](http://telegram.me/topkecleon), [Twitter](http://twitter.com/topkecleon), or [email](mailto:topkecleon@outlook.com).
Other developers who have contributed to otouto are [Juan Potato](http://github.com/JuanPotato), [Tiago Danin](http://github.com/TiagoDanin), [Ender](http://github.com/luksireiku), and [Iman Daneshi](http://github.com/Imandaneshi). Other developers who have contributed to otouto are [Juan Potato](http://github.com/JuanPotato), [Tiago Danin](http://github.com/TiagoDanin), [Ender](http://github.com/luksireiku), [Iman Daneshi](http://github.com/Imandaneshi) and [HeitorPB](https://github.com/heitorPB).

View File

@ -6,6 +6,7 @@ return {
owm_api_key = '', owm_api_key = '',
biblia_api_key = '', biblia_api_key = '',
thecatapi_key = '', thecatapi_key = '',
nasaapi_key = '',
time_offset = 0, time_offset = 0,
lang = 'en', lang = 'en',
antisquig = false, antisquig = false,
@ -99,7 +100,8 @@ telegram.me/otouto
'cats.lua', 'cats.lua',
'hearthstone.lua', 'hearthstone.lua',
'shout.lua', 'shout.lua',
-- Put new plugins here. 'apod.lua',
-- Put new plugins above this line.
'help.lua', 'help.lua',
'greetings.lua' 'greetings.lua'
} }

73
plugins/apod.lua Executable file
View File

@ -0,0 +1,73 @@
-- TODO:
-- inline bot stuff
if not config.nasaapi_key then
print('Missing config value: nasaapi_key.')
print('You can use the simple key DEMO_KEY, but it is very limited.')
print('apod.lua will not be enabled.')
return
end
local command = 'apod [query]'
local doc = [[```
/apod [query]
Returns the Astronomy Picture of the Day.
If the query is a date, in the format YYYY-MM-DD,
the APOD of that day is returned.
```]]
local triggers = {
'^/apod[@'..bot.username..']*'
}
local action = function(msg)
local input = msg.text:input()
local caption = ''
local date = '*'
--local date_url = ''
local url = 'https://api.nasa.gov/planetary/apod?api_key=' .. config.nasaapi_key
if input then
url = url .. '&date=' .. URL.escape(input)
-- date_url = string.sub(date,3,4) .. string.sub(date,6,7) .. string.sub(date,9,10)
date = date .. input
else
-- date_url = os.date("%y%m%d")
date = date .. os.date("%Y-%m-%d")
end
date = date .. '*\n'
local jstr, res = HTTPS.request(url)
if res ~= 200 then
sendReply(msg, config.errors.connection)
return
end
local jdat = JSON.decode(jstr)
if jdat.error then
sendReply(msg, config.errors.results)
return
end
--local weburl = 'http://apod.nasa.gov/apod/ap' .. date_url .. '.html'
--caption = date .. '[' .. jdat.title .. '](' .. weburl .. ')\n'
caption = date .. '[' .. jdat.title .. '](' .. jdat.url .. ')\n'
if jdat.copyright then
caption = caption .. 'Copyright: ' .. jdat.copyright
end
sendMessage(msg.chat.id, caption, false, nil, true)
end
return {
action = action,
triggers = triggers,
doc = doc,
command = command
}