Skip to content

Commit

Permalink
fix bug in basic handler, add support to get method and set headers i…
Browse files Browse the repository at this point in the history
…n single dialogue
  • Loading branch information
noahzark committed Jul 12, 2018
1 parent 8c5ed84 commit bcb6826
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from dns_cache import _set_dns_cache

GRADER_VERSION = '1.3.5'
GRADER_VERSION = '1.3.6'

if __name__ == '__main__':
report_logger.info("QiwuGrader ver {0}".format(GRADER_VERSION))
Expand Down
6 changes: 3 additions & 3 deletions controller/basic_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def pre_chat(self, from_name, msg):
return True

@abstractmethod
def process_chat(self, from_name, msg, skip_welcome=True, max_wait=None):
def process_chat(self, from_name, msg, skip_welcome=True, max_wait=None, login_wait=None):
return ''

def post_chat(self, from_name, result):
Expand All @@ -65,7 +65,7 @@ def post_chat(self, from_name, result):

return result

def handle_chat(self, from_name, msg, max_wait=None):
def handle_chat(self, from_name, msg, max_wait=None, login_wait=None):
msg_id = msg
# pre process and check if we really need to handle this message
handle_chat = self.pre_chat(from_name, msg)
Expand All @@ -76,7 +76,7 @@ def handle_chat(self, from_name, msg, max_wait=None):
if msg_id in self.msg:
result = self.msg[msg_id]
else:
result = self.process_chat(from_name, msg, max_wait=max_wait)
result = self.process_chat(from_name, msg, max_wait=max_wait, login_wait=login_wait)
self.msg[msg_id] = result

# post chat stuff
Expand Down
2 changes: 1 addition & 1 deletion controller/private_msg_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def pre_chat(self, from_name, msg):
return True

# Process the message, and returns response
def process_chat(self, from_name, msg, login_wait=None):
def process_chat(self, from_name, msg, max_wait=None, login_wait=None):

result = ''
skip_first = False
Expand Down
2 changes: 1 addition & 1 deletion controller/single_dialogue_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def pre_chat(self, from_name, msg):
return False

# Process the message, and returns response
def process_chat(self, from_name, msg, skip_welcome=True, max_wait=None):
def process_chat(self, from_name, msg, skip_welcome=True, max_wait=None, login_wait=None):
if msg.find('START_ROBOT') > -1:
return '你好'
# Get reply
Expand Down
19 changes: 14 additions & 5 deletions model/single_dialogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,37 @@ def __init__(self, service_config):
self.host = server_config['host']
self.port = server_config['port']
self.endpoint = server_config['api']
self.method = server_config.get('method', 'POST')

request_config = service_config.get_config('request')

self.payload = request_config['payload']
self.threshold = request_config.get('threshold', None)
self.answer_key = request_config.get('answer', 'reply')
self.type = request_config.get('type', 'application/json')
self.headers = request_config.get('headers', None)

self.url = self.to_uri()

def chat(self, data):
payload = encode_str(self.payload % data)
headers = {
'content-type': self.type
}

if not self.headers:
self.headers = {
'content-type': self.type
}
else:
self.headers['content-type'] = self.type

try:
r = requests.post(self.url, data=payload, headers=headers, timeout=5)
if self.method == 'GET':
r = requests.get(self.url, params=payload, headers=self.headers, timeout=5)
else:
r = requests.post(self.url, data=payload, headers=self.headers, timeout=5)
result = r.json()
logger.debug(result)
except Exception as e:
self.logger.exception(e)
self.logger.warning("Error process: " + r.text)
return SingleDialogue.ERROR_REPLY

if not self.threshold:
Expand Down

0 comments on commit bcb6826

Please sign in to comment.