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 a48da1e6ca
commit d91691be2f
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.
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:

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

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

View File

@ -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(