Move telegram bot code to library
This commit is contained in:
parent
d8e5a6b40c
commit
a458c2ed47
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,4 @@
|
||||
own-modules
|
||||
config.own
|
||||
config_*.own
|
||||
redditimages.db
|
6
modules.json
Normal file
6
modules.json
Normal file
@ -0,0 +1,6 @@
|
||||
[
|
||||
{
|
||||
"id": "18f1894447dfa72000a83011511a817c",
|
||||
"name": "telegram-bot"
|
||||
}
|
||||
]
|
39
modules.own
Normal file
39
modules.own
Normal file
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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(
|
||||
"<a href=\"%s\">%s</a>\n" +
|
||||
"<a href=\"%s\">%s</a>\n" +
|
||||
"<a href=\"https://reddit.com%s\">🗨 Comments</a>%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()
|
||||
|
Loading…
Reference in New Issue
Block a user