unexist.dev

Changeset 28:5621a40b24b5

Added !find
author unexist
date Fri, 23 Oct 2015 14:31:07 +0200
parents 3ba9fbcbf9ca
children 9db6db32a7ea
files contextbot/layer.py
diffstat 1 files changed, 46 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/contextbot/layer.py	Fri Oct 23 14:08:35 2015 +0200
+++ b/contextbot/layer.py	Fri Oct 23 14:31:07 2015 +0200
@@ -86,6 +86,8 @@
       os.makedirs(self.path_images)
     # }}}
 
+  # Event handler
+
   ## onMessage {{{
   # Handle client message
   # @param {Object}          self    This..
@@ -222,6 +224,8 @@
     print("[INFO] %s => %s, %d%%" % ( path, jid, progress))
     # }}}
 
+  # Command handler
+
   ## handleURLMessage {{{
   # Handle URL in message
   # @param {Object}  self      This..
@@ -433,6 +437,29 @@
     self.sendReply(msg, jid)
     # }}}
 
+  ## handleFindMessage {{{
+  # Handle find message
+  # @param {Object}  self      This..
+  # @param {List}    captures  Message captures
+  # @param {String}  jid       Message jid
+  ##
+
+  @ContextBotMatch(r"^!find (.+)")
+  def handleFindMessage(self, captures, jid):
+    print("handleFindMessage: captures=(" + ", ".join(captures) + "), jid=" + jid)
+
+    # Try to find similar phrases
+    alikes = self.findAlike(captures[0], 10)
+    length = len(alikes)
+
+    # Reply orderly
+    if 1 == length:
+      self.sendReply("Did you mean %s, Mistress/Master?" % tuple(alikes), jid)
+    elif 1 < length:
+      self.sendReply("Mistress/Master, I am just aware of following: %s" %
+        ", ".join(alikes), jid)
+    # }}}
+
   ## handleRemMessage {{{
   # Handle phrase message
   # @param {Object}  self      This..
@@ -541,18 +568,25 @@
             value[3] ), jid)
       else:
         # Try to find similar phrases
-        likes = self.findAlike(key, 5)
+        alikes = self.findAlike(key, 5)
+        length = len(alikes)
 
-        if "" != likes:
-          self.sendReply("Did you mean %s, Mistress/Master?" % ( likes ), jid)
+        # Reply orderly
+        if 1 == length:
+          self.sendReply("Did you mean %s, Mistress/Master?" % tuple(alikes), jid)
+        elif 1 < length:
+          self.sendReply("Mistress/Master, I am just aware of following: %s" %
+            ", ".join(alikes), jid)
 
     except IndexError, e:
-      self.sendReply("I need more parameters, Mistress/Master", jid)
+      self.sendReply("Please give me more parameters, Mistress/Master", jid)
 
     except sqlite3.Error, e:
       print("Error %s" % e.args[0])
       # }}} 
 
+  # Send helper
+
   ## sendReply {{{
   # Send reply to whatsapp server
   # @param {Object}  self    This..
@@ -624,6 +658,8 @@
     self._sendIq(entity, successFn, errorFn)
     # }}}
 
+  # Phrase helper
+
   ## getRandomPhrase {{{
   # Get random phrase for given key
   # @param {Object}  self    This..
@@ -719,7 +755,7 @@
   ##
 
   def findAlike(self, key, limit):
-    result = ""
+    alikes = []
 
     try:
       cur = self.conn.cursor()
@@ -732,17 +768,13 @@
       phrases = cur.fetchall()
 
       if None != phrases:
-        matches = []
-
         for i in range(0, len(phrases)):
-          matches.append("%s[%s]" % phrases[i])
-
-        result = ", ".join(matches)
+          alikes.append("%s[%s]" % phrases[i])
 
     except sqlite3.Error, e:
       print("Error %s" % e.args[0])
 
-    return result
+    return alikes
     # }}}
 
   ## replaceArgs {{{
@@ -790,6 +822,8 @@
     return value
     # }}}
 
+  # Toggle helper
+
   ## isFlagSet {{{
   # Check whether flag is set
   # @param {Integer}  flag  Flag to check
@@ -813,8 +847,8 @@
       # }}}
 
   #@ContextBotMatch(r"^([0-9]*)?\!give ([^ ]+) ([^ ]+)((?:\s(?:[^ ]*))*)")
-  #@ContextBotMatch(r"^!find (.+)")
 
+# Debug
 #layer = ContextLayer()
 
 #print(layer.handleProtoMessage(("", "!", "proto", ""), "unexist"))