From 4eb46422b562b59fa378dcce89708c3db0fbd5a2 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Tue, 5 Jul 2016 22:26:46 +0200 Subject: [PATCH] =?UTF-8?q?-=20RSS=20f=C3=BCr=20Kan=C3=A4le!=20Fix=20#6=20?= =?UTF-8?q?-=20ID=20kennzeichnet=20Chat-Admins=20und=20-Gr=C3=BCnder=20-?= =?UTF-8?q?=20table.contains()=20in=20utilites.lua=20verschoben=20-=20Mehr?= =?UTF-8?q?=20Bindings:=20getChatInfo,=20getChatAdministrators?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- otouto/plugins/id.lua | 20 +++++++- otouto/plugins/rss.lua | 100 ++++++++++++++++++++++++++----------- otouto/plugins/youtube.lua | 9 ---- otouto/utilities.lua | 23 +++++++++ 4 files changed, 112 insertions(+), 40 deletions(-) diff --git a/otouto/plugins/id.lua b/otouto/plugins/id.lua index df429fd..92bdbcf 100644 --- a/otouto/plugins/id.lua +++ b/otouto/plugins/id.lua @@ -95,6 +95,17 @@ function id:action(msg) local user_info = id:get_user(user_id, chat_id) table.insert(users_info, user_info) end + + -- get all administrators and the creator + local administrators = utilities.get_chat_administrators(self, chat_id) + local admins = {} + for num in pairs(administrators.result) do + if administrators.result[num].status ~= 'creator' then + table.insert(admins, tostring(administrators.result[num].user.id)) + else + creator_id = administrators.result[num].user.id + end + end local result = id:get_member_count(self, msg, chat_id) local member_count = result.result - 1 -- minus the bot if member_count == 1 then @@ -104,8 +115,13 @@ function id:action(msg) end local text = 'IDs für *'..chat_name..'* `['..chat_id..']`\nHier '..member_count..':*\n---------\n' for k,user in pairs(users_info) do - text = text..'*'..user.name..'* `['..user.id..']`\n' - text = string.gsub(text, "%_", " ") + if table.contains(admins, tostring(user.id)) then + text = text..'*'..user.name..'* `['..user.id..']` _Administrator_\n' + elseif tostring(creator_id) == user.id then + text = text..'*'..user.name..'* `['..user.id..']` _Gruppenersteller_\n' + else + text = text..'*'..user.name..'* `['..user.id..']`\n' + end end utilities.send_reply(self, msg, text, true) end diff --git a/otouto/plugins/rss.lua b/otouto/plugins/rss.lua index e4fd22f..442ba49 100644 --- a/otouto/plugins/rss.lua +++ b/otouto/plugins/rss.lua @@ -3,6 +3,7 @@ local rss = {} local http = require('socket.http') local https = require('ssl.https') local url = require('socket.url') +local bindings = require('otouto.bindings') local utilities = require('otouto.utilities') local redis = (loadfile "./otouto/redis.lua")() local feedparser = require("feedparser") @@ -10,12 +11,22 @@ local feedparser = require("feedparser") rss.command = 'rss ' function rss:init(config) - rss.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('rss', true).table + rss.triggers = { + "^/(rss) @(.*)$", + "^/rss$", + "^/rss (sub) (https?://[%w-_%.%?%.:/%+=&%~]+) @(.*)$", + "^/rss (sub) (https?://[%w-_%.%?%.:/%+=&%~]+)$", + "^/rss (del) (%d+) @(.*)$", + "^/rss (del) (%d+)$", + "^/rss (del)", + "^/rss (sync)$" + } rss.doc = [[* -]]..config.cmd_pat..[[rss*: Feed-Abonnements anzeigen -*]]..config.cmd_pat..[[rss* _sub_ __: Diesen Feed abonnieren -*]]..config.cmd_pat..[[rss* _del_ _<#>_: Diesen Feed deabonnieren -*]]..config.cmd_pat..[[rss* _sync_: Feeds syncen (nur Superuser)]] +]]..config.cmd_pat..[[rss* _@[Kanalname]_: Feed-Abonnements anzeigen +*]]..config.cmd_pat..[[rss* _sub_ __ _@[Kanalname]_: Diesen Feed abonnieren +*]]..config.cmd_pat..[[rss* _del_ _<#>_ _@[Kanalname]_: Diesen Feed deabonnieren +*]]..config.cmd_pat..[[rss* _sync_: Feeds syncen (nur Superuser) +Der Kanalname ist optional]] end function tail(n, k) @@ -208,60 +219,91 @@ function rss:print_subs(id, chat_name) return text, keyboard end -function rss:action(msg, config) - local input = utilities.input(msg.text) +function rss:action(msg, config, matches) local id = "user#id" .. msg.from.id - if msg.chat.type == 'channel' then - print('Kanäle werden momentan nicht unterstützt') - end if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then id = 'chat#id'..msg.chat.id end - if not input then - if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then - chat_name = msg.chat.title - else - chat_name = msg.chat.first_name + -- For channels + if matches[1] == 'sub' and matches[2] and matches[3] then + if msg.from.id ~= config.admin then + utilities.send_reply(self, msg, config.errors.sudo) + return + end + local id = '@'..matches[3] + local result = utilities.get_chat_info(self, id) + if not result then + utilities.send_reply(self, msg, 'Diesen Kanal gibt es nicht!') + return end + local output = rss:subscribe(id, matches[2]) + utilities.send_reply(self, msg, output, true) + return + elseif matches[1] == 'del' and matches[2] and matches[3] then + if msg.from.id ~= config.admin then + utilities.send_reply(self, msg, config.errors.sudo) + return + end + local id = '@'..matches[3] + local result = utilities.get_chat_info(self, id) + if not result then + utilities.send_reply(self, msg, 'Diesen Kanal gibt es nicht!') + return + end + local output = rss:unsubscribe(id, matches[2]) + utilities.send_reply(self, msg, output, true) + return + elseif matches[1] == 'rss' and matches[2] then + local id = '@'..matches[2] + local result = utilities.get_chat_info(self, id) + if not result then + utilities.send_reply(self, msg, 'Diesen Kanal gibt es nicht!') + return + end + local chat_name = result.result.title local output = rss:print_subs(id, chat_name) utilities.send_reply(self, msg, output, true) return end - - if input:match('(sub) (https?://[%w-_%.%?%.:/%+=&%~]+)$') then + + if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then + chat_name = msg.chat.title + else + chat_name = msg.chat.first_name + end + + if matches[1] == 'sub' and matches[2] then if msg.from.id ~= config.admin then utilities.send_reply(self, msg, config.errors.sudo) return end - local rss_url = input:match('(https?://[%w-_%.%?%.:/%+=&%~]+)$') - local output = rss:subscribe(id, rss_url) + local output = rss:subscribe(id, matches[2]) utilities.send_reply(self, msg, output, true) - elseif input:match('(del) (%d+)$') then + return + elseif matches[1] == 'del' and matches[2] then if msg.from.id ~= config.admin then utilities.send_reply(self, msg, config.errors.sudo) return end - local rss_url = input:match('(%d+)$') - local output = rss:unsubscribe(id, rss_url) + local output = rss:unsubscribe(id, matches[2]) utilities.send_reply(self, msg, output, true, '{"hide_keyboard":true}') - elseif input:match('(del)$') then - if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then - chat_name = msg.chat.title - else - chat_name = msg.chat.first_name - end + return + elseif matches[1] == 'del' and not matches[2] then local list_subs, keyboard = rss:print_subs(id, chat_name) utilities.send_reply(self, msg, list_subs, true, keyboard) return - elseif input:match('(sync)$') then + elseif matches[1] == 'sync' then if msg.from.id ~= config.admin then utilities.send_reply(self, msg, config.errors.sudo) return end rss:cron(self) + return end + local output = rss:print_subs(id, chat_name) + utilities.send_reply(self, msg, output, true) return end diff --git a/otouto/plugins/youtube.lua b/otouto/plugins/youtube.lua index d992b7e..7877b09 100644 --- a/otouto/plugins/youtube.lua +++ b/otouto/plugins/youtube.lua @@ -24,15 +24,6 @@ local apikey = cred_data.google_apikey local BASE_URL = 'https://www.googleapis.com/youtube/v3' -function table.contains(table, element) - for _, value in pairs(table) do - if value == element then - return true - end - end - return false -end - local makeOurDate = function(dateString) local pattern = "(%d+)%-(%d+)%-(%d+)T" local year, month, day = dateString:match(pattern) diff --git a/otouto/utilities.lua b/otouto/utilities.lua index 025b85e..27e7b6b 100644 --- a/otouto/utilities.lua +++ b/otouto/utilities.lua @@ -175,6 +175,20 @@ function utilities:answer_callback_query(callback, text, show_alert) } ) end +-- https://core.telegram.org/bots/api#getchat +function utilities:get_chat_info(chat_id) + return bindings.request(self, 'getChat', { + chat_id = chat_id + } ) +end + +-- https://core.telegram.org/bots/api#getchatadministrators +function utilities:get_chat_administrators(chat_id) + return bindings.request(self, 'getChatAdministrators', { + chat_id = chat_id + } ) +end + -- get the indexed word in a string function utilities.get_word(s, i) s = s or '' @@ -865,4 +879,13 @@ function url_encode(str) return str end +function table.contains(table, element) + for _, value in pairs(table) do + if value == element then + return true + end + end + return false +end + return utilities