fe549add63
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().
56 lines
1.2 KiB
Lua
Executable File
56 lines
1.2 KiB
Lua
Executable File
-- 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>
|
||
Returns the top result from YouTube.
|
||
Alias: /yt
|
||
```]]
|
||
|
||
local triggers = {
|
||
'^/youtube[@'..bot.username..']*',
|
||
'^/yt[@'..bot.username..']*$',
|
||
'^/yt[@'..bot.username..']* '
|
||
}
|
||
|
||
local action = function(msg)
|
||
|
||
local input = msg.text:input()
|
||
if not input then
|
||
if msg.reply_to_message and msg.reply_to_message.text then
|
||
input = msg.reply_to_message.text
|
||
else
|
||
sendMessage(msg.chat.id, doc, true, msg.message_id, true)
|
||
return
|
||
end
|
||
end
|
||
|
||
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
|
||
sendReply(msg, config.errors.connection)
|
||
return
|
||
end
|
||
|
||
local jdat = JSON.decode(jstr)
|
||
|
||
local output = '[](https://www.youtube.com/watch?v=' .. jdat.items[1].id.videoId .. ')'
|
||
|
||
sendMessage(msg.chat.id, output, false, nil, true)
|
||
|
||
end
|
||
|
||
return {
|
||
action = action,
|
||
triggers = triggers,
|
||
doc = doc,
|
||
command = command
|
||
}
|