From fe549add63eb0f9cb75cb3044faa091862d6f6c5 Mon Sep 17 00:00:00 2001 From: topkecleon Date: Wed, 13 Jan 2016 13:00:17 -0500 Subject: [PATCH] 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(). --- bindings.lua | 12 +++++------ bot.lua | 6 ++++-- config.lua | 1 + plugins/lastfm.lua | 2 +- plugins/preview.lua | 47 +++++++++++++++++++++++++++++++++++++++++++ plugins/wikipedia.lua | 9 ++++++++- plugins/youtube.lua | 12 ++++++++--- utilities.lua | 13 ++++++++++++ 8 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 plugins/preview.lua diff --git a/bindings.lua b/bindings.lua index aced791..be20c17 100755 --- a/bindings.lua +++ b/bindings.lua @@ -105,7 +105,7 @@ sendPhoto = function(chat_id, photo, caption, reply_to_message_id) local url = BASE_URL .. '/sendPhoto' - local curl_command = 'curl "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "photo=@' .. photo .. '"' + local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "photo=@' .. photo .. '"' if reply_to_message_id then curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"' @@ -123,7 +123,7 @@ sendDocument = function(chat_id, document, reply_to_message_id) local url = BASE_URL .. '/sendDocument' - local curl_command = 'curl "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "document=@' .. document .. '"' + local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "document=@' .. document .. '"' if reply_to_message_id then curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"' @@ -137,7 +137,7 @@ sendSticker = function(chat_id, sticker, reply_to_message_id) local url = BASE_URL .. '/sendSticker' - local curl_command = 'curl "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "sticker=@' .. sticker .. '"' + local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "sticker=@' .. sticker .. '"' if reply_to_message_id then curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"' @@ -151,7 +151,7 @@ sendAudio = function(chat_id, audio, reply_to_message_id, duration, performer, t local url = BASE_URL .. '/sendAudio' - local curl_command = 'curl "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "audio=@' .. audio .. '"' + local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "audio=@' .. audio .. '"' if reply_to_message_id then curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"' @@ -177,7 +177,7 @@ sendVideo = function(chat_id, video, reply_to_message_id, duration, performer, t local url = BASE_URL .. '/sendVideo' - local curl_command = 'curl "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "video=@' .. video .. '"' + local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "video=@' .. video .. '"' if reply_to_message_id then curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"' @@ -199,7 +199,7 @@ sendVoice = function(chat_id, voice, reply_to_message_id) local url = BASE_URL .. '/sendVoice' - local curl_command = 'curl "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "voice=@' .. voice .. '"' + local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "voice=@' .. voice .. '"' if reply_to_message_id then curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"' diff --git a/bot.lua b/bot.lua index d0672a9..0e40a94 100755 --- a/bot.lua +++ b/bot.lua @@ -61,7 +61,7 @@ on_msg_receive = function(msg) -- The fn run whenever a message is received. end) if not success then sendReply(msg, 'An unexpected error occurred.') - print(msg.text, result) + handle_exception(result, msg.text) return end -- If the action returns a table, make that table msg. @@ -95,7 +95,9 @@ while is_started do -- Start a loop while the bot should be running. for i,v in ipairs(plugins) do if v.cron then -- Call each plugin's cron function, if it has one. local res, err = pcall(function() v.cron() end) - if not res then print('ERROR: '..err) end + if not res then + handle_exception(err, 'CRON: ' .. i) + end end end last_cron = os.time() -- And finally, update the variable. diff --git a/config.lua b/config.lua index ee4271f..c6e7026 100755 --- a/config.lua +++ b/config.lua @@ -12,6 +12,7 @@ return { cli_port = 4567, admin = 00000000, admin_name = 'John Smith', + log_chat = nil, about_text = [[ I am otouto, the plugin-wielding, multi-purpose Telegram bot written by topkecleon. diff --git a/plugins/lastfm.lua b/plugins/lastfm.lua index e8bbe6e..9f33200 100755 --- a/plugins/lastfm.lua +++ b/plugins/lastfm.lua @@ -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 diff --git a/plugins/preview.lua b/plugins/preview.lua new file mode 100644 index 0000000..234a7bd --- /dev/null +++ b/plugins/preview.lua @@ -0,0 +1,47 @@ +local command = 'preview ' +local doc = [[``` +/preview +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 +} diff --git a/plugins/wikipedia.lua b/plugins/wikipedia.lua index 23079a6..a5e5954 100755 --- a/plugins/wikipedia.lua +++ b/plugins/wikipedia.lua @@ -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 diff --git a/plugins/youtube.lua b/plugins/youtube.lua index 321ed82..5e9628e 100755 --- a/plugins/youtube.lua +++ b/plugins/youtube.lua @@ -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 ' local doc = [[``` /youtube @@ -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 diff --git a/utilities.lua b/utilities.lua index 75ed5af..a9b8955 100755 --- a/utilities.lua +++ b/utilities.lua @@ -140,3 +140,16 @@ resolve_username = function(target) end end + +handle_exception = function(err, message) + + local output = '\n[' .. os.date('%F %T', os.time()) .. ']\n' .. bot.username .. ': ' .. err .. '\n' .. message .. '\n' + + if config.log_chat then + output = '```' .. output .. '```' + sendMessage(config.log_chat, output, true, nil, true) + else + print(output) + end + +end