bots: Update commute bot to use get_config_info().

This commit is contained in:
Robert Hönig 2017-06-18 16:56:13 +02:00 committed by showell
parent 4ce5521516
commit c4a85b2f74
3 changed files with 6 additions and 29 deletions

View file

@ -2,11 +2,6 @@ from __future__ import print_function
import datetime as dt import datetime as dt
import requests import requests
from os.path import expanduser
from six.moves import configparser as cp
home = expanduser('~')
CONFIG_PATH = home + '/commute.config'
class CommuteHandler(object): class CommuteHandler(object):
''' '''
@ -15,8 +10,8 @@ class CommuteHandler(object):
It looks for messages starting with @mention of the bot. It looks for messages starting with @mention of the bot.
''' '''
def __init__(self): def initialize(self, bot_handler):
self.api_key = self.get_api_key() self.api_key = bot_handler.get_config_info('commute', 'Google.com')['api_key']
def usage(self): def usage(self):
return ''' return '''
@ -77,17 +72,6 @@ class CommuteHandler(object):
you can use the timezone bot. 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 # determines if bot will respond as a private message/ stream message
def send_info(self, message, letter, bot_handler): def send_info(self, message, letter, bot_handler):
bot_handler.send_reply(message, letter) bot_handler.send_reply(message, letter)
@ -227,15 +211,9 @@ def test_calculate_seconds():
result = handler.calculate_seconds('2016,12,20,23,59,00') result = handler.calculate_seconds('2016,12,20,23,59,00')
assert result == str(1482278340) 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(): def test_helper_functions():
test_parse_pair() test_parse_pair()
test_calculate_seconds() test_calculate_seconds()
test_get_api_key()
if __name__ == '__main__': if __name__ == '__main__':
test_helper_functions() test_helper_functions()

View file

@ -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 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. private message, the bot will reply with a private message.
To setup the bot, you will first need to move commute.config into To setup the bot, you will first need to add a valid API key to commute.conf
the user home directory and add an API key.
Move Move
``` ```
~/zulip/api/bots/commute/commute.config ~/zulip/api/bots/commute/commute.conf
``` ```
into into
``` ```
~/commute.config ~/commute.conf
``` ```
To add an API key, please visit: To add an API key, please visit:
https://developers.google.com/maps/documentation/distance-matrix/start 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: Sample input and output: