diff --git a/plugins/img_bing.lua b/plugins/img_bing.lua new file mode 100644 index 0000000..9ef3b50 --- /dev/null +++ b/plugins/img_bing.lua @@ -0,0 +1,184 @@ +-- This is a proprietary plugin, property of Andreas Bielawski, (c) 2015 +-- DO NOT USE WITHOUT PERMISSION +do + +local mime = require("mime") + +local _blacklist + +local function getBingeImage(text) + local bing_key = cred_data.bing_key + local accountkey = mime.b64(bing_key..':'..bing_key) + local url = 'https://api.datamarket.azure.com/Bing/Search/Image?$format=json&Query=%27'..URL.escape(text)..'%27' + local response_body = {} + local request_constructor = { + url = url, + method = "GET", + sink = ltn12.sink.table(response_body), + headers = { + Authorization = "Basic "..accountkey + } + } + local ok, response_code, response_headers, response_status_line = https.request(request_constructor) + if not ok or response_code ~= 200 then return nil end + + local bing = json:decode(table.concat(response_body)).d.results + return bing +end + +local function is_blacklisted(msg) + local var = false + for v,word in pairs(_blacklist) do + if string.find(string.lower(msg), string.lower(word)) then + print("Wort steht auf der Blacklist!") + var = true + break + end + end + return var +end + +local function show_blacklist() + if not _blacklist[1] then + return "Keine Wörter geblacklisted!\nBlackliste welche mit /imgblacklist add [Wort]" + else + local sort_alph = function( a,b ) return a < b end + table.sort( _blacklist, sort_alph ) + local blacklist = "Folgende Wörter stehen auf der Blacklist:\n" + for v,word in pairs(_blacklist) do + blacklist = blacklist..'- '..word..'\n' + end + return blacklist + end +end + +local function add_blacklist() + print('Blacklisting '..word..' - saving to redis set telegram:img_blacklist') + if redis:sismember("telegram:img_blacklist", word) == true then + return '"'..word..'" steht schon auf der Blacklist.' + else + redis:sadd("telegram:img_blacklist", word) + return '"'..word..'" blacklisted!' + end +end + +local function remove_blacklist() + print('De-blacklisting '..word..' - removing from redis set telegram:img_blacklist') + if redis:sismember("telegram:img_blacklist", word) == true then + redis:srem("telegram:img_blacklist", word) + return '"'..word..'" erfolgreich von der Blacklist gelöscht!' + else + return '"'..word..'" steht nicht auf der Blacklist.' + end +end + +local function cache_bing_image(results, text) + local cache = {} + for v in pairs(results) do + table.insert(cache, results[v].MediaUrl) + end + cache_data('img_bing', string.lower(text), cache, 1209600, 'set') +end + +local function run(msg, matches) + local receiver = get_receiver(msg) + local text = matches[1] + if matches[2] then word = string.lower(matches[2]) end + + _blacklist = redis:smembers("telegram:img_blacklist") + + if text == "/imgblacklist show" then + if is_sudo(msg) then + return show_blacklist() + else + return "Du bist kein Superuser. Dieser Vorfall wird gemeldet!" + end + end + + if text == "/imgblacklist add" and word == nil then + return "Benutzung: /imgblacklist add [Wort]" + elseif text == "/imgblacklist add" and word then + if is_sudo(msg) then + return add_blacklist() + else + return "Du bist kein Superuser. Dieser Vorfall wird gemeldet!" + end + end + + if text == "/imgblacklist remove" and word == nil then + return "Benutzung: /imgblacklist remove [Wort]" + elseif text == "/imgblacklist remove" and word then + if is_sudo(msg) then + return remove_blacklist() + else + return "Du bist kein Superuser. Dieser Vorfall wird gemeldet!" + end + end + + print ('Checking if search contains blacklisted words: '..text) + if is_blacklisted(text) then + return "Vergiss es ._." + end + + local hash = 'telegram:cache:img_bing:'..string.lower(text) + local results = redis:smembers(hash) + if not results[1] then + print('doing web request') + results = getBingeImage(text) + if not results[1] then + return "Kein Bild gefunden!" + end + cache_bing_image(results, text) + end + + -- Random image from table + local i = math.random(#results) + local url = nil + + local failed = true + local nofTries = 0 + while failed and nofTries < #results do + if not results[i].MediaUrl then + url = results[i] + else + url = results[i].MediaUrl + end + print("Bilder-URL: ", url) + + if string.ends(url, ".gif") then + failed = not send_document_from_url(receiver, url, nil, nil, true) + return 'Source: '..url + elseif string.ends(url, ".jpg") or string.ends(url, ".jpeg") or string.ends(url, ".png") then + failed = not send_photo_from_url(receiver, url, nil, nil, true) + return 'Source: '..url + end + + nofTries = nofTries + 1 + i = i+1 + if i > #results then + i = 1 + end + end + + if failed then + return "Fehler beim Herunterladen eines Bildes." + end +end + +return { + description = "Sucht Bild mit Bing-API und versendet es (SafeSearch aktiv)", + usage = { + "/img [Suchbegriff]", + "/imgblacklist show: Zeigt Blacklist (nur Superuser)", + "/imgblacklist add [Wort]: Fügt Wort der Blacklist hinzu (nur Superuser)", + "/imgblacklist remove [Wort]: Entfernt Wort aus der Blacklist (nur Superuser)" + }, + patterns = { + "^/img (.*)$", + "^(/imgblacklist show)$", + "^(/imgblacklist add) (.*)$", + "^(/imgblacklist remove) (.*)$" + }, + run = run +} +end \ No newline at end of file diff --git a/plugins/myanimelist_user.lua b/plugins/myanimelist_user.lua new file mode 100644 index 0000000..b456590 --- /dev/null +++ b/plugins/myanimelist_user.lua @@ -0,0 +1,40 @@ +do + +local function get_user_info(name) + local url = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%22http%3A%2F%2Fmyanimelist.net%2Fmalappinfo.php%3Fu%3D'..name..'%22&format=json&diagnostics=true&callback=' + local res,code = http.request(url) + local data = json:decode(res).query.results.myanimelist.myinfo + if code ~= 200 then return "HTTP-Fehler" end + if not data then return "HTTP-Fehler" end + + local name = data.user_name + local watching = data.user_watching + local pause = data.user_onhold + local dropped = data.user_dropped + local complete = data.user_completed + local planed = data.user_plantowatch + --local totaldays = data.user_days_spent_watching --What is this? + local mal_url = 'http://myanimelist.net/profile/'..data.user_name + --local text = name..' schaut derzeit '..watching..' Animes, hat '..pause..' pausiert, '..dropped..' abgebrochen und '..complete..' beendet. '..planed..' stehen auf der Watchlist.\n'..name..' hat '..totaldays..' Tage damit verbracht Animes zu schauen.\n'..mal_url + local text = name..' schaut derzeit '..watching..' Animes, hat '..pause..' pausiert, '..dropped..' abgebrochen und '..complete..' beendet. '..planed..' stehen auf der Watchlist.\n'..mal_url + + return text +end + +local function run(msg, matches) + local name = matches[1] + local text = get_user_info(name) + local receiver = get_receiver(msg) + return text +end + +return { + description = "Sendet Infos zu einem MAL-User.", + usage = "/malu [Name]: Sendet Infos zum User", + patterns = {"^/malu (.+)$", + "myanimelist.net/profile/(.*)$" + }, + run = run +} + +end \ No newline at end of file diff --git a/plugins/nicovideo.lua b/plugins/nicovideo.lua new file mode 100644 index 0000000..dd4559c --- /dev/null +++ b/plugins/nicovideo.lua @@ -0,0 +1,54 @@ +do + +local makeOurDate = function(dateString) + local pattern = "(%d+)%-(%d+)%-(%d+)T" + local year, month, day = dateString:match(pattern) + if month == "00" then + return year + elseif day == "00" then + return month..'.'..year + else + return day..'.'..month..'.'..year + end +end + +local function get_vid_info(id) + local url = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%22http%3A%2F%2Fext.nicovideo.jp%2Fapi%2Fgetthumbinfo%2Fsm'..id..'%22&format=json&diagnostics=true&callback=' + local res,code = http.request(url) + local data = json:decode(res).query.results.nicovideo_thumb_response.thumb + if code ~= 200 then return "HTTP-Fehler" end + if not data then return "HTTP-Fehler" end + + local title = data.title + local date = makeOurDate(data.first_retrieve) + local user = data.user_nickname + local views = comma_value(data.view_counter) + local dura = data.length + local favs = comma_value(data.mylist_counter) + local comm = comma_value(data.comment_num) + local pic = data.thumbnail_url + + local text = title..'\nHochgeladen am '..date..' von '..user..', '..views..'x angesehen, Länge: '..dura..', '..favs..'x favoritisiert, '..comm..' Kommentare' + + return text, pic +end + +local function run(msg, matches) + local id = matches[1] + local text, pic = get_vid_info(id) + local receiver = get_receiver(msg) + local file = download_to_file(pic) + send_photo(receiver, file, ok_cb, false) + return text +end + +return { + description = "Sendet Infos zu einem nicovideo-Video", + usage = "Link zu nicovideo.jp Video", + patterns = { + "nicovideo.jp/watch/sm(.*)$" + }, + run = run +} + +end \ No newline at end of file diff --git a/plugins/notifyme.lua b/plugins/notifyme.lua new file mode 100644 index 0000000..ad86b12 --- /dev/null +++ b/plugins/notifyme.lua @@ -0,0 +1,86 @@ +-- This is a proprietary plugin, property of Andreas Bielawski, (c) 2015 +-- DO NOT USE WITHOUT PERMISSION + +-- INFO: Stats must be activated, so that it can collect all members of a group and save his/her id to redis. +-- You can deactivate it afterwards. + + +local function pre_process(msg) + local notify_users = redis:smembers('notify:ls') + + -- I call this beautiful lady the "if soup" + if msg.to.type == 'chat' then + if msg.text then + for _,user in pairs(notify_users) do + if string.match(string.lower(msg.text), '@'..user) then + local chat_id = msg.to.id + local id = redis:hget('notify:'..user, 'id') + -- check, if user has sent at least one message to the group, + -- so that we don't send the user some private text, when he/she is not + -- in the group. + if redis:sismember('chat:'..chat_id..':users', id) then + + -- ignore message, if it user is mentioning him/herself + if id == tostring(msg.from.id) then break; end + + local send_date = run_bash('date -d @'..msg.date..' +"%d.%m.%Y um %H:%M:%S Uhr"') + local send_date = string.gsub(send_date, "\n", "") + local from = string.gsub(msg.from.print_name, "%_", " ") + local chat_name = string.gsub(msg.to.print_name, "%_", " ") + local text = from..' am '..send_date..' in "'..chat_name..'":\n\n'..msg.text + send_msg('user#id'..id, text, ok_cb, false) + end + end + end + end + end + + return msg +end + +local function run(msg, matches) + if not msg.from.username then + return 'Du hast keinen Usernamen und kannst daher dieses Feature nicht nutzen. Tut mir leid!' + end + + local username = string.lower(msg.from.username) + + local hash = 'notify:'..username + + if matches[1] == "del" then + if not redis:sismember('notify:ls', username) then + return 'Du wirst noch gar nicht benachrichtigt!' + end + print('Setting notify in redis hash '..hash..' to false') + redis:hset(hash, 'notify', false) + print('Removing '..username..' from redis set notify:ls') + redis:srem('notify:ls', username) + return 'Du erhälst jetzt keine Benachrichtigungen mehr, wenn du angesprochen wirst.' + else + if redis:sismember('notify:ls', username) then + return 'Du wirst schon benachrichtigt!' + end + print('Setting notify in redis hash '..hash..' to true') + redis:hset(hash, 'notify', true) + print('Setting id in redis hash '..hash..' to '..msg.from.id) + redis:hset(hash, 'id', msg.from.id) + print('Adding '..username..' to redis set notify:ls') + redis:sadd('notify:ls', username) + return 'Du erhälst jetzt Benachrichtigungen, wenn du angesprochen wirst!' + end +end + +return { + description = "Benachrichtigt User, wenn er/sie erwähnt wird.", + usage = { + "/notify: Benachrichtigt dich privat, wenn du erwähnt wirst", + "/notify del: Benachrichtigt dich nicht mehr" + }, + patterns = { + "^/notify (del)$", + "^/notify$" + }, + run = run, + pre_process = pre_process, + notyping = true +} \ No newline at end of file diff --git a/plugins/twitter_user.lua b/plugins/twitter_user.lua new file mode 100644 index 0000000..e88958d --- /dev/null +++ b/plugins/twitter_user.lua @@ -0,0 +1,94 @@ +local OAuth = require "OAuth" + +local consumer_key = cred_data.tw_consumer_key +local consumer_secret = cred_data.tw_consumer_secret +local access_token = cred_data.tw_access_token +local access_token_secret = cred_data.tw_access_token_secret + +local client = OAuth.new(consumer_key, consumer_secret, { + RequestToken = "https://api.twitter.com/oauth/request_token", + AuthorizeUser = {"https://api.twitter.com/oauth/authorize", method = "GET"}, + AccessToken = "https://api.twitter.com/oauth/access_token" +}, { + OAuthToken = access_token, + OAuthTokenSecret = access_token_secret +}) + +local function resolve_url(url) + local response_body = {} + local request_constructor = { + url = url, + method = "HEAD", + sink = ltn12.sink.table(response_body), + headers = {}, + redirect = false + } + + local ok, response_code, response_headers, response_status_line = http.request(request_constructor) + if ok and response_headers.location then + return response_headers.location + else + return url + end +end + +function run(msg, matches) + if consumer_key:isempty() then + return "Twitter Consumer Key ist leer, führe !creds add tw_consumer_key KEY aus" + end + if consumer_secret:isempty() then + return "Twitter Consumer Secret ist leer, führe !creds add tw_consumer_secret KEY aus" + end + if access_token:isempty() then + return "Twitter Access Token ist leer, führe !creds add tw_access_token KEY aus" + end + if access_token_secret:isempty() then + return "Twitter Access Token Secret ist leer, führe !creds add tw_access_token_secret KEY aus" + end + + local twitter_url = "https://api.twitter.com/1.1/users/show/"..matches[1]..".json" + local response_code, response_headers, response_status_line, response_body = client:PerformRequest("GET", twitter_url) + local response = json:decode(response_body) + + local full_name = response.name + local user_name = response.screen_name + if response.verified then + user_name = user_name..' ✅' + end + if response.protected then + user_name = user_name..' 🔒' + end + local header = full_name.. " (@" ..user_name.. ")\n" + + local description = unescape(response.description) + if response.location then + location = response.location + else + location = '' + end + if response.url and response.location ~= '' then + url = ' | '..resolve_url(response.url)..'\n' + elseif response.url and response.location == '' then + url = resolve_url(response.url)..'\n' + else + url = '\n' + end + + local body = description..'\n'..location..url + + local favorites = comma_value(response.favourites_count) + local follower = comma_value(response.followers_count) + local following = comma_value(response.friends_count) + local statuses = comma_value(response.statuses_count) + local footer = statuses..' Tweets, '..follower..' Follower, '..following..' folge ich, '..favorites..' Tweets favorisiert' + + + return header..body..footer +end + +return { + description = "Sendet Informationen über Twitter-User", + usage = "URL zu Twitter-User", + patterns = {"twitter.com/([A-Za-z0-9-_-.-_-]+)$"}, + run = run +} \ No newline at end of file