unexist.dev

Changeset 74:dae8d59b8480

Added upload and post and removed !msg und !give commands
author unexist
date Fri, 22 Nov 2019 12:07:58 +0100
parents b5df6571dc20
children ba5e7a0c97c7
files slack/slack.rb
diffstat 1 files changed, 48 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/slack/slack.rb	Fri Nov 15 13:07:59 2019 +0100
+++ b/slack/slack.rb	Fri Nov 22 12:07:58 2019 +0100
@@ -29,6 +29,7 @@
 enable :inline_templates
 
 # Globals
+COLOR = "#ff8978"
 
 REGEXP = [ # {{{
   # Slack
@@ -36,6 +37,7 @@
   { match: /^!star$/, method: :star },
   { match: /^!like$/, method: :like },
 
+  # Fun
   { match: /^!isitfriday$/, method: :isitfriday },
   { match: /^!commit$/, method: :commit },
   { match: /^!proto$/, method: :protolol },
@@ -53,14 +55,19 @@
   # Phrases
   { match: /^!rem (.+) ([+-]?)= (.+)/, method: :store_phrase },
   { match: /^!alias (.+) => (.+)/, method: :store_alias },
-  { match: /^([0-9]*)?\!give ([^ ]+) ([^ ]+)((?:\s(?:[^ ]*))*)/, method: :give_phrase },
-  { match: /^([0-9]*)?\!msg ([^ ]+) ([^ ]+)((?:\s(?:[^ ]*))*)/, method: :msg_phrase },
   { match: /^([!~])find (.+)/, method: :find_phrase },
   { match: /^!vs (.+) (.+)/, method: :versus_phrase },
   { match: /^(\d+)?!forget (.+)/, method: :forget_phrase },
   { match: /^([0-9]*)?(\!|\?|%)([^ ]+)((?:\s(?:[^ ]*))*)/, method: :get_phrase }
 ] # }}}
 
+CHANNELS = [ # {{{
+  "GBZV7DNKA",
+  "GDZ15FGFJ",
+  "CCC93VA1Y",
+  "CQ57VC8FK"
+] # }}}
+
 # Database
 
 # Database # {{{
@@ -154,15 +161,9 @@
 end # }}}
 
 def getToken(chan) # {{{
-  if "GBZV7DNKA" == chan
-    return ENV["TOKEN1"]
-  elsif "GDZ15FGFJ" == chan
-    return ENV["TOKEN2"]
-  elsif "CCC93VA1Y" == chan
-    return ENV["TOKEN3"]
-  elsif "CQ57VC8FK" == chan
-    return ENV["TOKEN4"]
-  end
+  idx = CHANNELS.index(chan) || 0
+
+  ENV["TOKEN%d" % (idx + 1)]
 end # }}}
 
 def handleInteractiveMessage(event) # {{{
@@ -312,7 +313,7 @@
   sendMessageToChan("Good Sir, was this helpful?", event["channel"], {
     "fallback" => "Was this helpful?",
     "callback_id" => "helpfulness",
-    "color" => "#ff8978",
+    "color" => COLOR,
     "attachment_type" =>  "default",
     "actions" => [
       {
@@ -406,8 +407,10 @@
       message = "This good Sir, is quite oooold! :face_with_monocle:"
 
       sendMessageToChan(message, event["channel"], {
-        "text" => "First seen by this humble servant from %s on %s!" % [ u.nick, u.created_at.strftime("%Y-%m-%d %H:%M") ],
-        "color" => "#ff8978",
+        "text" => "First seen by this humble servant from %s on %s!" % [ 
+            u.nick, u.created_at.strftime("%Y-%m-%d %H:%M")
+        ],
+        "color" => COLOR,
         "fallback" => "Faithfully updated"
       })
     end
@@ -432,7 +435,7 @@
   else
     sendMessageToChan(message, event["channel"], {
       "text" => "Your faithful servant expanded the link for you, good Sir.",
-      "color" => "#ff8978",
+      "color" => COLOR,
       "fallback" => "Faithfully updated"
     })
   end
@@ -713,7 +716,7 @@
   unless event["channel"].nil? then
     sendMessageToChan("Thank you, Sir. :face_with_monocle:", event["channel"], {
       "text" => "I stored this eternally as version %d." % phrase.version,
-      "color" => "#ff8978",
+      "color" => COLOR,
       "fallback" => "Stored eternally"
     })
   else
@@ -723,45 +726,6 @@
   "Oops something went wrong.."
 end # }}}
 
-def give_phrase(event, version, target, key, args) # {{{
-  phrase, *args = phrase_args(key, args)
-
-  unless phrase.nil?
-    # Get phrase
-    if not version.empty? and phrase.has_version?(version.to_i)
-      v = phrase.specific_version(version.to_i)
-    else
-      v = phrase.latest_version
-    end
-
-    "%s: %s" % [ target, replace_args(v.value, args) ]
-  else
-    "Did you mean: %s" % [ find_alike(key, 5) ]
-  end
-rescue
-  "Oops something went wrong.."
-end # }}}
-
-def msg_phrase(event, version, target, key, args) # {{{
-  phrase, *args = phrase_args(key, args)
-
-  unless phrase.nil?
-    # Get phrase
-    if !version.empty? and phrase.has_version?(version.to_i)
-      v = phrase.specific_version(version.to_i)
-    else
-      v = phrase.latest_version
-    end
-
-    user = User(target)
-    user.msg "%s" % replace_args(v.value, args)
-  else
-    "Did you mean: %s" % [ find_alike(key, 5) ]
-  end
-rescue
-  "Oops something went wrong.."
-end # }}}
-
 def find_phrase(event, op, key) # {{{
   case op
     when "!"
@@ -943,18 +907,20 @@
   begin
     file = params[:file]
 
+    p params
+
     # Assemble name and path
     idx = 0
-    sanitizedname = params[:phrase].gsub(/[^0-9A-Z]/i, '_')
+    sanitizedName = params[:phrase].gsub(/[^0-9A-Z]/i, '_')
 
     fileext  = File.extname(file[:filename])
-    filename = "%s@%d%s" % [ sanitizedname, idx, fileext]
+    filename = "%s@%d%s" % [ sanitizedName, idx, fileext]
     filepath = File.join(File.dirname(__FILE__), "..",
       "images", "upload", filename)
 
     # Check whether file already exists
     while File.exist?(filepath) do
-      filename = "%s@%d%s" % [ sanitizedname, idx, fileext]
+      filename = "%s@%d%s" % [ sanitizedName, idx, fileext]
       filepath = File.join(File.dirname(__FILE__), "..", 
         "images", "upload", filename)
 
@@ -966,8 +932,19 @@
       f.write(file[:tempfile].read)
     end
 
-    version = store_phrase({ "user" => "Web" }, params[:phrase], nil, 
-      "https://cb.subforge.org/images/upload/" + filename)
+    fileUrl = "https://cb.subforge.org/images/upload/" + filename
+    version = store_phrase({ "user" => "Web" }, params[:phrase], nil, fileUrl)
+
+    # Send to channels
+    unless params[:submit2].nil?
+      CHANNELS.each do |chan|
+        sendMessageToChan(fileUrl, chan, {
+          "text" => "Your faithful herald proclaimed !%s for an unbeknownst Sir." % params[:phrase],
+          "color" => COLOR,
+          "fallback" => "Faithfully proclaimed"
+        })
+      end
+    end
 
     @message = "Thank you, unbeknownst Sir! I stored this eternally as version %d" % version.version
 
@@ -1007,11 +984,11 @@
         when "message"
           case json["event"]["subtype"]
             when "bot_message"
-              #< Nothing to do here
+              # Nothing to do here
               return
 
             when "message_changed"
-              #< Nothing to do here
+              # Nothing to do here
               return
 
             when "app_mention"
@@ -1076,7 +1053,8 @@
         <input id="phrase_file" type="file" name="file" />
       </div>
 
-      <input type="submit" name="submit" value="Upload" />
+      <input type="submit" name="submit1" value="Upload" />
+      <input type="submit" name="submit2" value="Upload and Post" />
     </form>
 
     <script>
@@ -1145,6 +1123,12 @@
   <body>
     <h1>ChangeLog</h1>
 
+    <h4>r74</h4>
+    <ul>
+      <li>Added upload and post</li>
+      <li>Removed !msg and !give commands</li>
+    </ul>
+
     <h4>r73</h4>
     <ul>
       <li>Added !vs command</li>
@@ -1187,7 +1171,7 @@
   </head>
 
   <body>
-    <h1>Oops something went wrong.."</h2>
+    <h1>Oops something went wrong..</h2>
     <pre>
       <%= @error %>
     </pre>