From c94c8e3b4d4d9645fcb61b235880c4bbe3eb6b47 Mon Sep 17 00:00:00 2001 From: topkecleon Date: Thu, 14 Jan 2016 22:39:24 -0500 Subject: [PATCH] minor improvements --- .gitignore | 1 + bindings.lua | 7 +++++++ config.lua | 2 +- plugins/apod.lua | 7 +++---- utilities.lua | 42 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 275c49b..7db5eb6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ plugins/administration.lua +plugins/CUSTOM.lua diff --git a/bindings.lua b/bindings.lua index be20c17..ad68073 100755 --- a/bindings.lua +++ b/bindings.lua @@ -1,3 +1,10 @@ +-- bindings.lua +-- Bindings for the Telegram bot API. +-- https://core.telegram.org/bots/api + +assert(HTTPS) +assert(JSON) + local BASE_URL = 'https://api.telegram.org/bot' .. config.bot_api_key if not config.bot_api_key then diff --git a/config.lua b/config.lua index e706cba..de56b10 100755 --- a/config.lua +++ b/config.lua @@ -15,7 +15,7 @@ return { admin_name = 'John Smith', log_chat = nil, 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. Send /help to get started. diff --git a/plugins/apod.lua b/plugins/apod.lua index b558c91..ec412be 100755 --- a/plugins/apod.lua +++ b/plugins/apod.lua @@ -26,7 +26,7 @@ local action = function(msg) url = url .. '&date=' .. URL.escape(input) date = date .. input else - date = date .. os.date("%Y-%m-%d") + date = date .. os.date("%F") end date = date .. '*\n' @@ -44,9 +44,8 @@ local action = function(msg) return end - --local weburl = 'http://apod.nasa.gov/apod/ap' .. date_url .. '.html' - --output = date .. '[' .. jdat.title .. '](' .. weburl .. ')\n' - output = date .. '[' .. jdat.title .. '](' .. jdat.hdurl .. ')\n' + local img_url = jdat.hdurl or jdat.url + output = date .. '[' .. jdat.title .. '](' .. img_url .. ')\n' if jdat.copyright then output = output .. 'Copyright: ' .. jdat.copyright diff --git a/utilities.lua b/utilities.lua index a9b8955..834e511 100755 --- a/utilities.lua +++ b/utilities.lua @@ -1,6 +1,10 @@ -- utilities.lua -- Functions shared among plugins. +HTTP = require('socket.http') +HTTPS = require('ssl.https') +JSON = require('dkjson') + -- get the indexed word in a string get_word = function(s, i) @@ -152,4 +156,42 @@ handle_exception = function(err, message) print(output) end +end + + -- Okay, this one I actually did copy from yagop. + -- https://github.com/yagop/telegram-bot/blob/master/bot/utils.lua +download_file = function(url, filename) + + local respbody = {} + local options = { + url = url, + sink = ltn12.sink.table(respbody), + redirect = true + } + + local response = nil + + if url:match('^https') then + options.redirect = false + response = { HTTPS.request(options) } + else + response = { HTTP.request(options) } + end + + local code = response[2] + local headers = response[3] + local status = response[4] + + if code ~= 200 then return false end + + filename = filename or os.time() + + local file_path = '/tmp/'..filename + + file = io.open(file_path, 'w+') + file:write(table.concat(respbody)) + file:close() + + return file_path + end