diff --git a/.gitignore b/.gitignore index bcb7247..10b8d57 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ +own-modules config.own +config_*.own redditimages.db \ No newline at end of file diff --git a/modules.json b/modules.json new file mode 100644 index 0000000..bbd0bc8 --- /dev/null +++ b/modules.json @@ -0,0 +1,6 @@ +[ + { + "id": "18f1894447dfa72000a83011511a817c", + "name": "telegram-bot" + } +] \ No newline at end of file diff --git a/modules.own b/modules.own new file mode 100644 index 0000000..f6d2aa4 --- /dev/null +++ b/modules.own @@ -0,0 +1,39 @@ +use ["std"] + +if (ARGS.length >= 2 && ARGS[0] == "owm") { + use ["files", "json", "java"] + + File = newClass("java.io.File") + runtime = newClass("java.lang.Runtime").getRuntime() + + def loadModulesJson(path = "modules.json") { + f = fopen(path, "r") + modules = jsondecode(readText(f)) + fclose(f) + return modules + } + def exec(cmd, dir = ".") = runtime.exec(cmd, null, new File(dir)) + + match (ARGS[1]) { + case "install": { + modulesDir = "own-modules" + if (!exists(modulesDir)) { + mkdir(modulesDir) + } + for module : loadModulesJson() { + print module.name + moduleDir = modulesDir + "/" + module.name + if (!exists(moduleDir)) { + mkdir(moduleDir) + cmd = "git clone https://gist.github.com/" + module.id + ".git " + module.name + exec(cmd, modulesDir) + println " installed" + } else { + exec("git pull origin master", moduleDir) + println " updated" + } + } + } + } +} + diff --git a/redditimages2telegram.own b/redditimages2telegram.own index 68e2a1e..b447b98 100644 --- a/redditimages2telegram.own +++ b/redditimages2telegram.own @@ -2,25 +2,9 @@ // include "config.own" use ["std", "math", "http", "json", "functional", "files", "jdbc"] +include "own-modules/telegram-bot/TelegramBot.own" -// Telegram -def toParams(obj) = reduce(obj, "", def(acc, k, v) = acc + k + "=" + urlencode(v) + "&") -def createRawUrl(method, params, token = "") = "https://api.telegram.org/bot" + token + "/" + method + "?" + params + "access_token=" + token -def createUrl(method, params, token = "") = createRawUrl(method, toParams(params), token) -def invokeJson(method, params, callback) = http(createUrl(method, params, config.token), combine(::jsondecode, callback)) -def invoke(method, params, callback) = http(createUrl(method, params, config.token), callback) -def sendPhoto(chatId, url) { - invoke("sendPhoto", { - "chat_id": chatId, - "photo": url - }, ::debug) -} -def sendMediaGroup(chatId, media) { - invoke("sendMediaGroup", { - "chat_id": chatId, - "media": jsonencode(media) - }, ::debug) -} +bot = new TelegramBot(config.token) // Reddit def fetchSubreddit(subreddit) { @@ -90,13 +74,6 @@ def strToHashtag(str) = .replaceAll("[^a-z_0-9\s]", "") .replaceAll("\s+", "_") -def multireplace(str, replacements) { - for re : replacements { - str = str.replace(re[0], re[1]) - } - return str -} - def safe(str) = str.replace("&", "&") .replace("<", "<").replace(">", ">") @@ -107,7 +84,7 @@ def getCaption(post) { tag = " #" + strToHashtag(post.sub + "_" + post.flair_text) } return sprintf( - "%s\n" + + "%s\n" + "🗨 Comments%s", safe(post.url), safe(post.title), @@ -120,7 +97,7 @@ def getCaption(post) { media = stream(fetchAll(config.subreddits)) .filter(def(p) = reduce([".jpg", ".png"], false, def(acc, ext) = acc || indexOf(p.url, ext) > 0)) .filter(::isPostUnique) -// .peek(def(p) = sendPhoto(config.peer, p.url)) +// .peek(def(p) = bot.sendPhoto(config.peer, p.url)) .peek(::addPost) .map(def(p) = { "type": "photo", @@ -134,7 +111,7 @@ media = stream(fetchAll(config.subreddits)) debug(jsonencode(media)) if (length(media) > 0) { - sendMediaGroup(config.peer, media) + bot.sendMediaGroup(config.peer, media) } stIsPostExists.close()