From c4a85b2f743417988f977074922460df388c28ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20H=C3=B6nig?= Date: Sun, 18 Jun 2017 16:56:13 +0200 Subject: [PATCH] bots: Update commute bot to use get_config_info(). --- bots/commute/{commute.config => commute.conf} | 0 bots/commute/commute.py | 26 ++----------------- bots/commute/readme.md | 9 +++---- 3 files changed, 6 insertions(+), 29 deletions(-) rename bots/commute/{commute.config => commute.conf} (100%) diff --git a/bots/commute/commute.config b/bots/commute/commute.conf similarity index 100% rename from bots/commute/commute.config rename to bots/commute/commute.conf diff --git a/bots/commute/commute.py b/bots/commute/commute.py index 5146bcd..23cef4e 100644 --- a/bots/commute/commute.py +++ b/bots/commute/commute.py @@ -2,11 +2,6 @@ from __future__ import print_function import datetime as dt import requests -from os.path import expanduser -from six.moves import configparser as cp - -home = expanduser('~') -CONFIG_PATH = home + '/commute.config' class CommuteHandler(object): ''' @@ -15,8 +10,8 @@ class CommuteHandler(object): It looks for messages starting with @mention of the bot. ''' - def __init__(self): - self.api_key = self.get_api_key() + def initialize(self, bot_handler): + self.api_key = bot_handler.get_config_info('commute', 'Google.com')['api_key'] def usage(self): return ''' @@ -77,17 +72,6 @@ class CommuteHandler(object): you can use the timezone bot. ''' - # adds API Authentication Key to url request - def get_api_key(self): - # commute.config must be moved from - # ~/zulip/api/bots/commute/commute.config into - # ~/commute.config for program to work - # see readme.md for more information - with open(CONFIG_PATH) as settings: - config = cp.ConfigParser() - config.readfp(settings) - return config.get('Google.com', 'api_key') - # determines if bot will respond as a private message/ stream message def send_info(self, message, letter, bot_handler): bot_handler.send_reply(message, letter) @@ -227,15 +211,9 @@ def test_calculate_seconds(): result = handler.calculate_seconds('2016,12,20,23,59,00') assert result == str(1482278340) -def test_get_api_key(): - # must change to your own api key for test to work - result = handler.get_api_key() - assert result == 'abcdefghijksm' - def test_helper_functions(): test_parse_pair() test_calculate_seconds() - test_get_api_key() if __name__ == '__main__': test_helper_functions() diff --git a/bots/commute/readme.md b/bots/commute/readme.md index d81ca13..80ff1b7 100644 --- a/bots/commute/readme.md +++ b/bots/commute/readme.md @@ -11,24 +11,23 @@ units and information in various languages. The bot will respond to the same stream input was in. And if called as private message, the bot will reply with a private message. -To setup the bot, you will first need to move commute.config into -the user home directory and add an API key. +To setup the bot, you will first need to add a valid API key to commute.conf Move ``` -~/zulip/api/bots/commute/commute.config +~/zulip/api/bots/commute/commute.conf ``` into ``` -~/commute.config +~/commute.conf ``` To add an API key, please visit: https://developers.google.com/maps/documentation/distance-matrix/start -to retrieve a key and copy your api key into commute.config +to retrieve a key and copy your api key into commute.conf Sample input and output: