From f94b936c173fefa3cdbbf817f56d0009bb73f73b Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Tue, 27 Sep 2016 23:31:16 +0200 Subject: [PATCH] Quotes: Listquotes per Callback --- otouto/plugins/quotes.lua | 46 +++++++++++++++++++++++++++++--------- otouto/plugins/twitter.lua | 1 - 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/otouto/plugins/quotes.lua b/otouto/plugins/quotes.lua index d56c75d..e02835e 100644 --- a/otouto/plugins/quotes.lua +++ b/otouto/plugins/quotes.lua @@ -54,27 +54,56 @@ function quotes:get_quote(msg) end end -function quotes:list_quotes(msg) +function quotes:callback(callback, msg, self, config) local hash = get_redis_hash(msg, 'quotes') if hash then print('Getting quotes from redis set '..hash) local quotes_table = redis:smembers(hash) local text = "" + for num,quote in pairs(quotes_table) do text = text..num..") "..quote..'\n' end + if not text or text == "" then - return '*Es wurden noch keine Zitate gespeichert.*\nSpeichere doch welche mit `/addquote [Zitat]`', true + utilities.answer_callback_query(callback, 'Es wurden noch keine Zitate gespeichert.', true) else - return upload(text) - end + -- In case the quote list is > 4096 chars + local text_len = string.len(text) + + while text_len > 4096 do + to_send_text = string.sub(text, 1, 4096) + text = string.sub(text, 4096, text_len) + local res = utilities.send_message(callback.from.id, to_send_text) + + if not res then + utilities.answer_callback_query(callback, 'Bitte starte den Bot zuerst privat!', true) + return + end + text_len = string.len(text) + end + + local res = utilities.send_message(callback.from.id, text) + if not res then + utilities.answer_callback_query(callback, 'Bitte starte den Bot zuerst privat!', true) + return + end + utilities.answer_callback_query(callback, 'Zitatliste per PN verschickt') + end + else + utilities.answer_callback_query(callback, 'Es wurden noch keine Zitate gespeichert.', true) end end function quotes:action(msg, config, matches) + if msg.chat.type == 'private' then + utilities.send_reply(msg, 'Dieses Plugin kann nur in Gruppen verwendet werden!') + return + end + if matches[1] == "quote" then - utilities.send_message(msg.chat.id, quotes:get_quote(msg), true) + utilities.send_message(msg.chat.id, quotes:get_quote(msg), true, nil, false) return elseif matches[1] == "addquote" and matches[2] then utilities.send_reply(msg, quotes:save_quote(msg), true) @@ -97,12 +126,7 @@ function quotes:action(msg, config, matches) return end elseif matches[1] == "listquotes" then - local link, iserror = quotes:list_quotes(msg) - if iserror then - utilities.send_reply(msg, link, true) - return - end - utilities.send_reply(msg, 'Ich habe eine Liste aller Zitate hochgeladen.', false, '{"inline_keyboard":[[{"text":"Alle Zitate abrufen","url":"'..link..'"}]]}') + utilities.send_reply(msg, 'Bitte klicke hier unten auf diese attraktive Schaltfläche.', false, '{"inline_keyboard":[[{"text":"Alle Zitate per PN","callback_data":"quotes:"}]]}') return end utilities.send_reply(msg, quotes.doc, true) diff --git a/otouto/plugins/twitter.lua b/otouto/plugins/twitter.lua index d388615..4f52d30 100644 --- a/otouto/plugins/twitter.lua +++ b/otouto/plugins/twitter.lua @@ -41,7 +41,6 @@ local client = OAuth.new(consumer_key, consumer_secret, { }) function twitter:action(msg, config, matches) - if not matches[2] then id = matches[1] else