From 51bf0cf869e169c6f900c685786507e5d592e989 Mon Sep 17 00:00:00 2001
From: derAnfaenger <robhoenig@gmail.com>
Date: Tue, 25 Jul 2017 16:08:18 +0200
Subject: [PATCH] bots: Use restricted open function in john.
---
 zulip_bots/zulip_bots/bots/john/john.py | 104 +++++++++++-------------
 1 file changed, 48 insertions(+), 56 deletions(-)
diff --git a/zulip_bots/zulip_bots/bots/john/john.py b/zulip_bots/zulip_bots/bots/john/john.py
index c0ad54cc..edb4e658 100644
--- a/zulip_bots/zulip_bots/bots/john/john.py
+++ b/zulip_bots/zulip_bots/bots/john/john.py
@@ -11,10 +11,10 @@ except ImportError:
                       Please: pip install chatterbot""")
 
 BOTS_DIR = os.path.dirname(os.path.abspath(__file__))
-JOKES_PATH = os.path.join(BOTS_DIR, 'assets/var/jokes.json')
 DATABASE_PATH = os.path.join(BOTS_DIR, 'assets/var/database.db')
 DIRECTORY_PATH = os.path.join(BOTS_DIR, 'assets')
 VAR_PATH = os.path.join(BOTS_DIR, 'assets/var')
+JOKES_PATH = 'assets/var/jokes.json'
 
 if not os.path.exists(DIRECTORY_PATH):
     os.makedirs(DIRECTORY_PATH)
@@ -40,60 +40,6 @@ def create_chat_bot(no_learn):
                    silence_performance_warning="True",
                    read_only=no_learn)
 
-bot = create_chat_bot(False)
-bot.set_trainer(ListTrainer)
-
-bot.train([
-    "I want to contribute",
-    """Contributors are more than welcomed! Please read
-    https://github.com/zulip/zulip#how-to-get-involved-with-contributing-to-zulip
-    to learn how to contribute.""",
-])
-
-bot.train([
-    "What is Zulip?",
-    """Zulip is a powerful, open source group chat application. Written in Python
-    and using the Django framework, Zulip supports both private messaging and group
-    chats via conversation streams. You can learn more about the product and its
-    features at https://www.zulip.org.""",
-])
-
-bot.train([
-    "I would like to request a remote dev instance",
-    """Greetings! You should receive a response from one of our mentors soon.
-    In the meantime, why don't you learn more about running Zulip on a development
-    environment? https://zulip.readthedocs.io/en/latest/using-dev-environment.html""",
-])
-
-bot.train([
-    "Joke!",
-    "Only if you ask nicely!",
-])
-
-bot.train([
-    "What is your name?",
-    "I am John, my job is to assist you with Zulip.",
-])
-
-bot.train([
-    "What can you do?",
-    "I can provide useful information and jokes if you follow etiquette.",
-])
-
-with open(JOKES_PATH) as data_file:
-    for joke in json.load(data_file):
-        bot.train([
-            "Please can you tell me a joke?",
-            joke['joke'],
-        ])
-
-bot.set_trainer(ChatterBotCorpusTrainer)
-
-bot.train(
-    "chatterbot.corpus.english"
-)
-
-bota = create_chat_bot(True)
 
 class JohnHandler(object):
     '''
@@ -111,9 +57,55 @@ class JohnHandler(object):
             mantain a conversation, joke and give useful information.
             '''
 
+    def initialize(self, bot_handler):
+        self.bot = create_chat_bot(False)
+        self.bot.set_trainer(ListTrainer)
+        self.bot.train([
+            "I want to contribute",
+            """Contributors are more than welcomed! Please read
+            https://github.com/zulip/zulip#how-to-get-involved-with-contributing-to-zulip
+            to learn how to contribute.""",
+        ])
+        self.bot.train([
+            "What is Zulip?",
+            """Zulip is a powerful, open source group chat application. Written in Python
+            and using the Django framework, Zulip supports both private messaging and group
+            chats via conversation streams. You can learn more about the product and its
+            features at https://www.zulip.org.""",
+        ])
+        self.bot.train([
+            "I would like to request a remote dev instance",
+            """Greetings! You should receive a response from one of our mentors soon.
+            In the meantime, why don't you learn more about running Zulip on a development
+            environment? https://zulip.readthedocs.io/en/latest/using-dev-environment.html""",
+        ])
+        self.bot.train([
+            "Joke!",
+            "Only if you ask nicely!",
+        ])
+        self.bot.train([
+            "What is your name?",
+            "I am John, my job is to assist you with Zulip.",
+        ])
+        self.bot.train([
+            "What can you do?",
+            "I can provide useful information and jokes if you follow etiquette.",
+        ])
+        with bot_handler.open(JOKES_PATH) as data_file:
+            for joke in json.load(data_file):
+                self.bot.train([
+                    "Please can you tell me a joke?",
+                    joke['joke'],
+                ])
+        self.bot.set_trainer(ChatterBotCorpusTrainer)
+        self.bot.train(
+            "chatterbot.corpus.english"
+        )
+        self.bota = create_chat_bot(True)
+
     def handle_message(self, message, bot_handler, state_handler):
         original_content = message['content']
-        bot_response = str(bota.get_response(original_content))
+        bot_response = str(self.bota.get_response(original_content))
         bot_handler.send_reply(message, bot_response)
 
 handler_class = JohnHandler