in functions/source/bot_fulfillment/bot_fulfillment.py [0:0]
def prepare_response(query, response_type, content, slots):
response = ''
logger.info('Generated Query :: ' + str(query))
# once query ready need to enable the db connection and close it
db_connection = get_redshift_connection()
db_cursor = db_connection.cursor()
if is_continue:
try:
db_cursor.execute(query)
rows = db_cursor.fetchall()
column_names_pre_title = [desc[0] for desc in db_cursor.description]
column_names = [t.title() for t in column_names_pre_title]
result = []
param_formatter = ParamFormatter()
if response_type == 'text':
response = get_text_response(column_names, content, param_formatter, rows, slots)
elif response_type == 'table' and len(rows) > 0:
response = get_table_response(column_names, content, param_formatter, response_type, result, rows,slots)
else:
content = "Sorry, I don't seem to find information related to your query in the database."
response = get_default_response(content)
except Exception as ex:
logger.error('Exception while trying to execute query :: ' + str(ex))
content = "Sorry, I am having trouble connecting to the database at the moment. Please try after sometime. If the problem persists, contact IT support."
response = get_default_response(content)
return response
finally:
if db_cursor != None:
db_cursor.close()
if db_connection != None:
db_connection.close()
else:
# default response.
logger.debug("Default Response")
response = {
"sessionAttributes": {
"card": ""
},
"dialogAction": {
"type": "Close",
"fulfillmentState": "Fulfilled",
"message": {
"contentType": "PlainText",
"content": "Sorry, I am having trouble connecting to the database at the moment. Please try after sometime. If the problem persists, contact IT support."
}
}}
logger.debug("Response is : " + str(response))
# return response["Response"]
return response