Changeset 67:e58b737b6a7f
Added yomama handler, multi slack support and file upload versioning
author | unexist |
---|---|
date | Thu, 29 Nov 2018 10:39:27 +0100 |
parents | 938b6674d977 |
children | 7a834464579e |
files | slack/slack.rb |
diffstat | 1 files changed, 64 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- a/slack/slack.rb Thu Nov 29 09:30:11 2018 +0100 +++ b/slack/slack.rb Thu Nov 29 10:39:27 2018 +0100 @@ -120,11 +120,19 @@ end end # }}} +def getToken(chan) # {{{ + if "GBZV7DNKA" == chan + return ENV["TOKEN1"] + else + return ENV["TOKEN2"] + end +end # }}} + def handleInteractiveMessage(event) # {{{ p event # Verify token - return unless event["token"] != ENV["TOKEN"] + return unless event["token"] != ENV["TOKEN1"] and event["token"] != ENV["TOKEN2"] # Handle callback id case event["callback_id"] @@ -139,7 +147,7 @@ # Send reply payload = { - "token" => ENV["TOKEN"], + "token" => getToken(event["channel"][0]["id"]), "callback_id" => event["callback_id"], "message_ts" => event["message_ts"], "channel_id" => event["channel"][0]["id"], @@ -167,7 +175,7 @@ begin HTTParty.post("https://slack.com/api/chat.postMessage", query: { - "token" => ENV["TOKEN"], + "token" => getToken(chan), "channel" => chan, "text" => mesg, "attachments" => [ attachment ].to_json.to_s, @@ -186,7 +194,7 @@ begin HTTParty.post("https://slack.com/api/chat.update", query: { - "token" => ENV["TOKEN"], + "token" => getToken(chan), "channel" => chan, "text" => mesg, "ts" => ts, @@ -206,7 +214,7 @@ begin HTTParty.post("https://slack.com/api/reactions.add", query: { - "token" => ENV["TOKEN"], + "token" => getToken(event["channel"]), "channel" => event["channel"], "timestamp" => event["ts"], "name" => name @@ -225,7 +233,7 @@ begin HTTParty.post("https://slack.com/api/stars.add", query: { - "token" => ENV["TOKEN"], + "token" => getToken["channel"], "channel" => event["channel"], "timestamp" => event["ts"] }, @@ -243,7 +251,7 @@ begin HTTParty.post("https://slack.com/api/channels.history", query: { - "token" => ENV["TOKEN"], + "token" => getToken(chan), "channel" => chan, "count" => count }, @@ -328,6 +336,14 @@ @protos[rand(@protos.size)] end #}}} +# Yomama handler + +def yomama(event) #{{{ + @yomama = IO.readlines("../yomama.txt") if @yomama.nil? + + @yomama[rand(@yomama.size)] +end #}}} + # Url handler def old(event) #{{{ @@ -336,6 +352,7 @@ return if event["text"].match(/stfu$/) URI.extract(event["text"], ["http", "https"]) do |link| + now = Time.now # Check if we know it u = Uri.first(:uri => link, :channel => "Slack") @@ -345,11 +362,11 @@ :nick => event["user"], :channel => "Slack", :uri => link, - :created_at => Time.now + :created_at => now ) u.save - else + elsif u.created_at != now message = "This good Sir, is quite oooold! :face_with_monocle:" sendMessageToChan(message, event["channel"], { @@ -360,8 +377,6 @@ end end rescue => error - p error - "Oops something went wrong.." end # }}} @@ -663,6 +678,8 @@ "color" => "#ff8978", "fallback" => "Stored eternally" }) + + return version rescue "Oops something went wrong.." end # }}} @@ -805,6 +822,7 @@ { match: /^!isitfriday$/, method: :isitfriday }, { match: /^!commit$/, method: :commit }, { match: /^!proto$/, method: :protolol }, + { match: /^!yomama$/, method: :yomama }, { match: /^<http/, method: :old }, # Jira @@ -854,19 +872,29 @@ begin # Assemble name and path - filename = "%s%s" % [ - params[:phrase].gsub(/[^0-9A-Z]/i, '_'), - File.extname(file[:filename]) - ] - filepath = File.join(File.dirname(__FILE__), "..", "images", "upload", filename) + idx = 0 + filename = params[:phrase].gsub(/[^0-9A-Z]/i, '_') + fileext = File.extname(file[:filename]) + filepath = File.join(File.dirname(__FILE__), "..", + "images", "upload", "%s@%d%s" % [ filename, idx, fileext]) + # Check whether file already exists + while File.exist?(filepath) do + filepath = File.join(File.dirname(__FILE__), "..", + "images", "upload", "%s@%d%s" % [ filename, idx, fileext]) + + idx += 1 + end + + # Finally write file File.open(filepath, "wb") do |f| f.write(file[:tempfile].read) end - store_phrase("Web", params[:phrase], nil, "https://cb.subforge.org/images/upload/" + filename) + version = store_phrase("Web", params[:phrase], nil, + "https://cb.subforge.org/images/upload/" + filename) - @message = "Thank you, unbeknownst Sir!" + @message = "Thank you, unbeknownst Sir! I stored this eternally as version %d" % version.version rescue @message = "Oops something went wrong.." end @@ -914,7 +942,7 @@ return else - if "GBZV7DNKA" == json["event"]["channel"] + if "GBZV7DNKA" == json["event"]["channel"] or "GDZ15FGFJ" == json["event"]["channel"] mesg = handleMessage(json["event"]) # Send reply @@ -943,16 +971,31 @@ <form action="/upload" method="POST" enctype="multipart/form-data"> <div> <label for="phrase">Phrase: </label> - <input type="text" name="phrase" /> + <input id="phrase_name" type="text" name="phrase" /> </div> <div> <label for="file">Pick a file: </label> - <input type="file" name="file" /> + <input id="phrase_file" type="file" name="file" /> </div> <input type="submit" name="submit" value="Upload" /> </form> + + <script> + document.getElementById("phrase_file").addEventListener("change", function () { + var filename = this.value; + + var idx1 = filename.lastIndexOf("\\"); + var idx2 = filename.lastIndexOf("."); + + if (-1 !== idx1) { + filename = filename.slice(idx1 + 1, (-1 !== idx2 ? idx2: null)); + + document.getElementById("phrase_name").value = filename; + } + }); + </script> </body> </html>