bots: Update yoda bot to use get_config_info().

This commit is contained in:
Robert Hönig 2017-06-18 17:35:16 +02:00 committed by showell
parent ff015cf7b6
commit fcfda08f4d
4 changed files with 10 additions and 16 deletions

View file

@ -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. On the pop-up that comes up, click on the **COPY** button.
This is your Mashape API key. It is used This is your Mashape API key. It is used
to authenticate. Store it in the `yoda.config` file. to authenticate. Store it in the `yoda.conf` file in the bot's
directory.
The `yoda.config` file should be located at `~/yoda.config`.
Example input: Example input:

2
bots/yoda/yoda.conf Normal file
View file

@ -0,0 +1,2 @@
[yoda]
api_key=12345678

View file

View file

@ -1,7 +1,6 @@
# See readme.md for instructions on running this code. # See readme.md for instructions on running this code.
from __future__ import print_function from __future__ import print_function
import os
import logging import logging
import ssl import ssl
import sys import sys
@ -35,6 +34,9 @@ class YodaSpeakHandler(object):
It looks for messages starting with '@mention-bot'. 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): def usage(self):
return ''' return '''
This bot will allow users to translate a sentence into 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. Before running this, make sure to get a Mashape Api token.
Instructions are in the 'readme.md' file. Instructions are in the 'readme.md' file.
Store it in the 'yoda.config' file. Store it in the 'yoda.conf' file.
The 'yoda.config' file should be located at '~/yoda.config'. The 'yoda.conf' file should be located in this bot's directory.
Example input: Example input:
@mention-bot You will learn how to speak like me someday. @mention-bot You will learn how to speak like me someday.
''' '''
@ -95,7 +97,7 @@ def handle_input(message, bot_handler):
else: else:
sentence = format_input(original_content) sentence = format_input(original_content)
try: 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: except ssl.SSLError or TypeError:
reply_message = 'The service is temporarily unavailable, please try again.' 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) 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): def send_message(bot_handler, message, stream, subject):
# function for sending a message # function for sending a message
bot_handler.send_message(dict( bot_handler.send_message(dict(