From fcfda08f4d871f8b67441ec731a67fd4cff63a24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20H=C3=B6nig?= Date: Sun, 18 Jun 2017 17:35:16 +0200 Subject: [PATCH] bots: Update yoda bot to use get_config_info(). --- bots/yoda/readme.md | 5 ++--- bots/yoda/yoda.conf | 2 ++ bots/yoda/yoda.config | 0 bots/yoda/yoda.py | 19 ++++++------------- 4 files changed, 10 insertions(+), 16 deletions(-) create mode 100644 bots/yoda/yoda.conf delete mode 100644 bots/yoda/yoda.config diff --git a/bots/yoda/readme.md b/bots/yoda/readme.md index 742b066..5ac6f17 100644 --- a/bots/yoda/readme.md +++ b/bots/yoda/readme.md @@ -29,9 +29,8 @@ the Yoda Speak API to. Click on the blue **GET THE KEYS** button. On the pop-up that comes up, click on the **COPY** button. This is your Mashape API key. It is used -to authenticate. Store it in the `yoda.config` file. - -The `yoda.config` file should be located at `~/yoda.config`. +to authenticate. Store it in the `yoda.conf` file in the bot's +directory. Example input: diff --git a/bots/yoda/yoda.conf b/bots/yoda/yoda.conf new file mode 100644 index 0000000..a6a8cc9 --- /dev/null +++ b/bots/yoda/yoda.conf @@ -0,0 +1,2 @@ +[yoda] +api_key=12345678 diff --git a/bots/yoda/yoda.config b/bots/yoda/yoda.config deleted file mode 100644 index e69de29..0000000 diff --git a/bots/yoda/yoda.py b/bots/yoda/yoda.py index 6805326..6caadab 100644 --- a/bots/yoda/yoda.py +++ b/bots/yoda/yoda.py @@ -1,7 +1,6 @@ # See readme.md for instructions on running this code. from __future__ import print_function -import os import logging import ssl import sys @@ -35,6 +34,9 @@ class YodaSpeakHandler(object): It looks for messages starting with '@mention-bot'. ''' + def initialize(self, bot_handler): + self.api_key = bot_handler.get_config_info('yoda')['api_key'] + def usage(self): return ''' This bot will allow users to translate a sentence into @@ -43,8 +45,8 @@ class YodaSpeakHandler(object): Before running this, make sure to get a Mashape Api token. Instructions are in the 'readme.md' file. - Store it in the 'yoda.config' file. - The 'yoda.config' file should be located at '~/yoda.config'. + Store it in the 'yoda.conf' file. + The 'yoda.conf' file should be located in this bot's directory. Example input: @mention-bot You will learn how to speak like me someday. ''' @@ -95,7 +97,7 @@ def handle_input(message, bot_handler): else: sentence = format_input(original_content) try: - reply_message = send_to_yoda_api(sentence, get_api_key()) + reply_message = send_to_yoda_api(sentence, handler_class.api_key) except ssl.SSLError or TypeError: reply_message = 'The service is temporarily unavailable, please try again.' @@ -108,15 +110,6 @@ def handle_input(message, bot_handler): bot_handler.send_reply(message, reply_message) - -def get_api_key(): - # function for getting Mashape api key - home = os.path.expanduser('~') - with open(home + '/yoda.config') as api_key_file: - api_key = api_key_file.read().strip() - return api_key - - def send_message(bot_handler, message, stream, subject): # function for sending a message bot_handler.send_message(dict(