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:
parent
c50b1ca3fa
commit
fe549add63
12
bindings.lua
12
bindings.lua
@ -105,7 +105,7 @@ sendPhoto = function(chat_id, photo, caption, reply_to_message_id)
|
|||||||
|
|
||||||
local url = BASE_URL .. '/sendPhoto'
|
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
|
if reply_to_message_id then
|
||||||
curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"'
|
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 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
|
if reply_to_message_id then
|
||||||
curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"'
|
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 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
|
if reply_to_message_id then
|
||||||
curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"'
|
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 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
|
if reply_to_message_id then
|
||||||
curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"'
|
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 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
|
if reply_to_message_id then
|
||||||
curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"'
|
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 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
|
if reply_to_message_id then
|
||||||
curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"'
|
curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"'
|
||||||
|
6
bot.lua
6
bot.lua
@ -61,7 +61,7 @@ on_msg_receive = function(msg) -- The fn run whenever a message is received.
|
|||||||
end)
|
end)
|
||||||
if not success then
|
if not success then
|
||||||
sendReply(msg, 'An unexpected error occurred.')
|
sendReply(msg, 'An unexpected error occurred.')
|
||||||
print(msg.text, result)
|
handle_exception(result, msg.text)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
-- If the action returns a table, make that table msg.
|
-- 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
|
for i,v in ipairs(plugins) do
|
||||||
if v.cron then -- Call each plugin's cron function, if it has one.
|
if v.cron then -- Call each plugin's cron function, if it has one.
|
||||||
local res, err = pcall(function() v.cron() end)
|
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
|
||||||
end
|
end
|
||||||
last_cron = os.time() -- And finally, update the variable.
|
last_cron = os.time() -- And finally, update the variable.
|
||||||
|
@ -12,6 +12,7 @@ return {
|
|||||||
cli_port = 4567,
|
cli_port = 4567,
|
||||||
admin = 00000000,
|
admin = 00000000,
|
||||||
admin_name = 'John Smith',
|
admin_name = 'John Smith',
|
||||||
|
log_chat = nil,
|
||||||
about_text = [[
|
about_text = [[
|
||||||
I am otouto, the plugin-wielding, multi-purpose Telegram bot written by topkecleon.
|
I am otouto, the plugin-wielding, multi-purpose Telegram bot written by topkecleon.
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ local action = function(msg)
|
|||||||
|
|
||||||
local jdat = JSON.decode(jstr)
|
local jdat = JSON.decode(jstr)
|
||||||
if jdat.error then
|
if jdat.error then
|
||||||
sendReply(msg, config.errors.results)
|
sendReply(msg, 'Please specify your last.fm username or set it with /fmset.')
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
47
plugins/preview.lua
Normal file
47
plugins/preview.lua
Normal 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
|
||||||
|
}
|
@ -42,7 +42,7 @@ local action = function(msg)
|
|||||||
sendReply(msg, config.errors.results)
|
sendReply(msg, config.errors.results)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
--
|
||||||
local url = jdat.responseData.results[1].url
|
local url = jdat.responseData.results[1].url
|
||||||
local title = jdat.responseData.results[1].titleNoFormatting:gsub(' %- Wikipedia, the free encyclopedia', '')
|
local title = jdat.responseData.results[1].titleNoFormatting:gsub(' %- Wikipedia, the free encyclopedia', '')
|
||||||
|
|
||||||
@ -79,6 +79,13 @@ local action = function(msg)
|
|||||||
end
|
end
|
||||||
|
|
||||||
sendMessage(msg.chat.id, output, true, nil, true)
|
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
|
end
|
||||||
|
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
-- Thanks to @TiagoDanin for writing the original plugin.
|
-- 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 command = 'youtube <query>'
|
||||||
local doc = [[```
|
local doc = [[```
|
||||||
/youtube <query>
|
/youtube <query>
|
||||||
@ -25,7 +31,7 @@ local action = function(msg)
|
|||||||
end
|
end
|
||||||
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)
|
local jstr, res = HTTPS.request(url)
|
||||||
if res ~= 200 then
|
if res ~= 200 then
|
||||||
@ -35,9 +41,9 @@ local action = function(msg)
|
|||||||
|
|
||||||
local jdat = JSON.decode(jstr)
|
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
|
end
|
||||||
|
|
||||||
|
@ -140,3 +140,16 @@ resolve_username = function(target)
|
|||||||
end
|
end
|
||||||
|
|
||||||
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
|
||||||
|
Reference in New Issue
Block a user