Better logging, new preview.lua plugin.

When an exception is caught, info will be printed to the config.log_chat or the console.
/preview will give an "unlinked" preview for the link.
youtube.lua now uses config.google_api_key.
youtube.lua now uses unlinked previews.
lastfm.lua gives more informative error messages.
New utility: handle_exception().
This commit is contained in:
topkecleon
2016-01-13 13:00:17 -05:00
parent c50b1ca3fa
commit fe549add63
8 changed files with 89 additions and 13 deletions

View File

@ -69,7 +69,7 @@ local action = function(msg)
local jdat = JSON.decode(jstr)
if jdat.error then
sendReply(msg, config.errors.results)
sendReply(msg, 'Please specify your last.fm username or set it with /fmset.')
return
end

47
plugins/preview.lua Normal file
View File

@ -0,0 +1,47 @@
local command = 'preview <link>'
local doc = [[```
/preview <link>
Returns a full-message, "unlinked" preview.
```]]
local triggers = {
'^/preview'
}
local action = function(msg)
local input = msg.text:input()
if not input then
sendMessage(msg.chat.id, doc, true, nil, true)
return
end
input = get_word(input, 1)
if not input:match('^https?://.+') then
input = 'http://' .. input
end
local res = HTTP.request(input)
if not res then
sendReply(msg, 'Please provide a valid link.')
return
end
if res:len() == 0 then
sendReply(msg, 'Sorry, the link you provided is not letting us make a preview.')
return
end
-- Invisible zero-width, non-joiner.
local output = '[](' .. input .. ')'
sendMessage(msg.chat.id, output, false, nil, true)
end
return {
action = action,
triggers = triggers,
doc = doc,
command = command
}

View File

@ -42,7 +42,7 @@ local action = function(msg)
sendReply(msg, config.errors.results)
return
end
--
local url = jdat.responseData.results[1].url
local title = jdat.responseData.results[1].titleNoFormatting:gsub(' %- Wikipedia, the free encyclopedia', '')
@ -79,6 +79,13 @@ local action = function(msg)
end
sendMessage(msg.chat.id, output, true, nil, true)
--
--[[ Comment the previous block and uncomment this one for full-message,
-- "unlinked" link previews.
-- Invisible zero-width, non-joiner.
local output = '[](' .. jdat.responseData.results[1].url .. ')'
sendMessage(msg.chat.id, output, false, nil, true)
]]--
end

View File

@ -1,5 +1,11 @@
-- Thanks to @TiagoDanin for writing the original plugin.
if not config.google_api_key then
print('Missing config value: google_api_key.')
print('youtube.lua will not be enabled.')
return
end
local command = 'youtube <query>'
local doc = [[```
/youtube <query>
@ -25,7 +31,7 @@ local action = function(msg)
end
end
local url = 'https://www.googleapis.com/youtube/v3/search?key=AIzaSyAfe7SI8kwQqaoouvAmevBfKumaLf-3HzI&type=video&part=snippet&maxResults=1&q=' .. URL.escape(input)
local url = 'https://www.googleapis.com/youtube/v3/search?key=' .. config.google_api_key .. '&type=video&part=snippet&maxResults=1&q=' .. URL.escape(input)
local jstr, res = HTTPS.request(url)
if res ~= 200 then
@ -35,9 +41,9 @@ local action = function(msg)
local jdat = JSON.decode(jstr)
local message = 'https://www.youtube.com/watch?v=' .. jdat.items[1].id.videoId
local output = '[](https://www.youtube.com/watch?v=' .. jdat.items[1].id.videoId .. ')'
sendMessage(msg.chat.id, message, false, msg.message_id)
sendMessage(msg.chat.id, output, false, nil, true)
end