From 52426b14b753863a62e2b7908d5a531ddab5949d Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Thu, 8 Sep 2016 00:38:00 +0200 Subject: [PATCH] - Lagere Funktion zum Printen von Nachrichten in eigene Funktion aus & verbessere den Code etwas - Fix Readme --- README.md | 2 +- miku/bot.lua | 159 ++++++++++++++++++++++----------------------------- 2 files changed, 68 insertions(+), 93 deletions(-) diff --git a/README.md b/README.md index b47e02b..c39f812 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Mikubot V2 ist freie Software; du darfst ihn modifizieren und weiterverbreiten, ### Ubuntu und Debian Falls du Ubuntu oder Debian verwendest, kannst du einfach `./install-dependencies.sh` ausführen, damit alles installiert wird. Ergänze dann noch den `bot_api_key` und die `admin`-ID (Bekommst du in Telegram mit `@Mikubot id`) und kopiere die config.lua.example nach config.lua. -###Optionale Pakete +####Optionale Pakete Diese Pakete werden für `flip-text.lua`, `shell.lua`, `speedtest_cli.lua` und `youtube_dl.lua` benötigt. * `sudo apt-get install -y cowsay scrot fswebcam vnstat vnstati youtube-dl npm` * `sudo npm install -g flip-text` diff --git a/miku/bot.lua b/miku/bot.lua index 8a41c0f..92a7c8c 100644 --- a/miku/bot.lua +++ b/miku/bot.lua @@ -20,7 +20,7 @@ local bot = {} -bot.version = '160906' +bot.version = '160908' function bot:init(config) -- The function run when the bot is started or reloaded. assert(config.bot_api_key, 'Dein Bot-Token ist nicht in der Config gesetzt!') @@ -63,97 +63,6 @@ function bot:on_msg_receive(msg, config) -- The fn run whenever a message is rec -- remove comment to enable debugging -- vardump(msg) - -- print messages - msg_time = '\27[36m'..convert_timestamp(msg.date, '[%H:%M:%S]')..'\27[0m' - - if msg.chat.title then - from_chat = ' \27[36m'..msg.chat.title..':\27[0m ' - else - from_chat = '' - end - - if msg.from.first_name then - name1 = msg.from.first_name - else - name1 = '' - end - if msg.from.last_name then - name2 = ' '..msg.from.last_name - else - name2 = '' - end - if msg.from.username then - name3 = ' \27[31m(@'..msg.from.username..')\27[0m' - else - name3 = '' - end - full_name = '\27[1m\27[31m'..name1..name2..'\27[0m'..name3 - - if msg.forward_from then - if msg.forward_from.first_name then - fwd_name1 = msg.forward_from.first_name - else - fwd_name1 = '' - end - if msg.forward_from.last_name then - fwd_name2 = ' '..msg.forward_from.last_name - else - fwd_name2 = '' - end - full_fwd_name = '\27[3mWeitergeleitet von \27[31m'..fwd_name1..fwd_name2..'\27[0m: ' - else - full_fwd_name = '' - end - - if msg.reply_to_message then - if msg.reply_to_message.from.first_name then - from_name1 = msg.reply_to_message.from.first_name - else - from_name1 = '' - end - if msg.reply_to_message.from.last_name then - from_name2 = ' '..msg.reply_to_message.from.last_name - else - from_name2 = '' - end - full_from_name = '\27[3mAntwort auf \27[31m'..from_name1..from_name2..'\27[0m: ' - else - full_from_name = '' - end - - if msg.text then - user_msg = msg.text..'\27[0m' - elseif msg.caption then - user_msg = msg.caption..'\27[0m' - else - user_msg = '' - end - - if msg.photo then - filetext = '[Foto] ' - elseif msg.video then - filetext = '[Video] ' - elseif msg.sticker then - filetext = '[Sticker] ' - elseif msg.voice then - filetext = '[Sprachnachricht] ' - elseif msg.audio then - filetext = '[Audio] ' - elseif msg.document then - filetext = '[Datei] ' - elseif msg.location then - filetext = '[Standort] ' - elseif msg.contact then - filetext = '[Kontakt] ' - elseif msg.venue then - filetext = '[Venue] ' - else - filetext = '' - end - - full_message = msg_time..from_chat..full_name..' \27[36m>>>\27[0m '..full_fwd_name..full_from_name..filetext..user_msg - print(full_message) - if msg.date < os.time() - 5 then return end -- Do not process old messages. msg = utilities.enrich_message(msg) @@ -161,6 +70,9 @@ function bot:on_msg_receive(msg, config) -- The fn run whenever a message is rec if msg.reply_to_message then msg.reply_to_message.text = msg.reply_to_message.text or msg.reply_to_message.caption or '' end + + -- print out received messages + print(bot:print_msg(msg)) -- Support deep linking. if msg.text:match('^'..config.cmd_pat..'start .+') then @@ -337,6 +249,69 @@ function bot:run(config) print('Halted.') end +function bot:print_msg(msg) + local msg_time = '\27[36m'..convert_timestamp(msg.date, '[%H:%M:%S]')..'\27[0m' + + if msg.chat.title then + from_chat = ' \27[36m'..msg.chat.title..':\27[0m' + else + from_chat = '' + end + + if msg.from.username then + username = ' \27[31m(@'..msg.from.username..')\27[0m' + else + username = '' + end + local full_name = ' \27[1m\27[31m'..msg.from.name..'\27[0m'..username + + if msg.forward_from then + if msg.forward_from.username then + fwd_username = ' \27[31m(@'..msg.forward_from.username..')\27[0m' + else + fwd_username = '' + end + fwd_text = '\27[3mWeitergeleitet von \27[31m'..msg.forward_from.name..'\27[0m'..fwd_username..': ' + else + fwd_text = '' + end + + if msg.reply_to_message then + if msg.reply_to_message.from.username then + from_username = ' \27[31m(@'..msg.reply_to_message.from.username..')\27[0m' + else + from_username = '' + end + reply_text = '\27[3mAntwort auf \27[31m'..msg.reply_to_message.from.name..'\27[0m'..from_username..': ' + else + reply_text = '' + end + + if msg.photo then + filetext = '[Foto] ' + elseif msg.video then + filetext = '[Video] ' + elseif msg.sticker then + filetext = '[Sticker] ' + elseif msg.voice then + filetext = '[Sprachnachricht] ' + elseif msg.audio then + filetext = '[Audio] ' + elseif msg.document then + filetext = '[Datei] ' + elseif msg.location then + filetext = '[Standort] ' + elseif msg.contact then + filetext = '[Kontakt] ' + elseif msg.venue then + filetext = '[Ort] ' + else + filetext = '' + end + + return msg_time..from_chat..full_name..' \27[36m>>>\27[0m '..fwd_text..reply_text..filetext..msg.text..'\27[0m' +end + -- Apply plugin.pre_process function function pre_process_msg(self, msg, config) for n=1, #self.plugins do