bots: Update commute bot to use get_config_info().
This commit is contained in:
parent
4ce5521516
commit
c4a85b2f74
|
@ -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()
|
||||
|
|
|
@ -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:
|
||||
|
||||
|
|
Loading…
Reference in a new issue