This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/plugins/youtube.lua
topkecleon 9070a44c8f Improved triggers for many plugins.
Redone README to match the website.
Bot now supports deep linking / start payloads.
lastfm.lua now notifies a user when his default username is saved.
antisquig is now part of moderation.lua.
2016-01-07 22:30:12 -05:00

47 lines
1009 B
Lua
Executable File

-- Thanks to @TiagoDanin for writing the original plugin.
local doc = [[
/youtube <query>
Returns the top result from YouTube.
]]
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
sendReply(msg, doc)
return
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 jstr, res = HTTPS.request(url)
if res ~= 200 then
sendReply(msg, config.errors.connection)
return
end
local jdat = JSON.decode(jstr)
local message = 'https://www.youtube.com/watch?v=' .. jdat.items[1].id.videoId
sendMessage(msg.chat.id, message, false, msg.message_id)
end
return {
action = action,
triggers = triggers,
doc = doc
}