""" Japanese chatting """
import logging
import traceback
from time import time
from configparser import ConfigParser
import json
from pytz import timezone
from datetime import datetime
import requests
from minette.session import Session
from minette.dialog import Message, DialogService
from minette.util import date_to_str

class ChatDialogService(DialogService):
    def __init__(self, request, session, logger=None, config=None, tzone=None, connection=None, api_key="", replace_values=None, debug=False):
        super().__init__(request=request, session=session, logger=logger, config=config, tzone=tzone, connection=connection)
        self.api_key = api_key
        self.replace_values = replace_values if replace_values else {}
        self.debug = debug

    def process_request(self):
        chat_req = {
            "utt": self.request.text,
            "context": self.session.chat_context,
            "mode": "srtr" if self.session.mode == "srtr" else "",
            }
        if self.request.user.nickname != "":
            chat_req["nickname"] = self.request.user.nickname
        chat_res = requests.post("https://api.apigw.smt.docomo.ne.jp/dialogue/v1/dialogue?APIKEY=" + self.api_key, json.dumps(chat_req)).json()
        self.session.chat_context = chat_res["context"]
        self.session.mode = chat_res["mode"] if chat_res["mode"] == "srtr" else ""
        chat_str = str(chat_res["utt"])
        for k, v in self.replace_values.items():
            chat_str = chat_str.replace(k, v)
        self.session.data = chat_str
        self.debug_chat_message(chat_res)

    def compose_response(self):
        self.session.keep_mode = True if self.session.mode == "srtr" else False
        return self.request.get_reply_message(str(self.session.data))

    def debug_chat_message(self, chat_json):
        """
        :param chat_json: JSON to log
        """
        if self.debug is False:
            return
        try:
            start_time = time()
            chat_json_str = json.dumps(chat_json)
            now = datetime.now(self.timezone)
            cursor = self.connection.cursor()
            sql = "insert into chatlog(timestamp, overhead, text) values (%s,%s,%s)"
            overhead = int((time() - start_time) * 1000)
            cursor.execute(sql, (date_to_str(now, True), overhead, chat_json_str))
            self.connection.commit()
        except Exception as ex:
            self.logger.error("Error occured in logging chat message for debug: " + str(ex) + "\n" + traceback.format_exc())

    @classmethod
    def prepare_table(cls, connection_provider):
        """
        :param connection_provider: MySQLConnectionProvider to create table if not existing
        :type connection_provider: MySQLConnectionProvider
        """
        connection = connection_provider.get_connection()
        cursor = connection.cursor()
        cursor.execute("create table chatlog(timestamp DATETIME, overhead INT, text VARCHAR(4000))")
        connection.commit()

@@ -492,19 +492,23 @@
 ne, 
-debug=False
+chat_logfile=%22%22
 ):%0A 
@@ -750,21 +750,676 @@
 elf.
-debug = debug
+chat_logger = None%0A        if chat_logfile:%0A            logger = logging.getLogger(__name__)%0A            logger.setLevel(logging.DEBUG)%0A            formatter = logging.Formatter('%25(asctime)s - %25(name)s - %25(message)s')%0A            stream_handler = logging.StreamHandler()%0A            stream_handler.setLevel(logging.DEBUG)%0A            stream_handler.setFormatter(formatter)%0A            logger.addHandler(stream_handler)%0A            file_handler = logging.FileHandler(filename=chat_logfile)%0A            file_handler.setLevel(logging.DEBUG)%0A            file_handler.setFormatter(formatter)%0A            logger.addHandler(file_handler)%0A            self.chat_logger = logger
 %0A%0A  
@@ -2211,833 +2211,111 @@
     
+if 
 self.
-debug_chat_message(chat_res)%0A%0A    def compose_response(self):%0A        self.session.keep_mode = True if self.session.mode == %22srtr%22 else False%0A        return self.request.get_reply_message(str(self.session.data))%0A%0A    def debug_chat_message(
+chat_logger:%0A            try:%0A                
 self
-, 
+.
 chat_
-json):%0A        %22%22%22%0A        :param chat_json: JSON to log%0A        %22%22%22%0A        if self.debug is False:%0A            return%0A        try:%0A            start_time = time()%0A            chat_json_str = json.dumps(chat_json)%0A            now = datetime.now(self.timezone)%0A            cursor = self.connection.cursor()%0A            sql = %22insert into chatlog(timestamp, overhead, text) values (%25s,%25s,%25s)%22%0A            overhead = int((time() - start_time) * 1000)%0A            cursor.execute(sql, (date_to_str(now, True), overhead, chat_json_str))%0A            self.connection.commit()%0A
+logger.debug(json.dumps(chat_res))%0A    
     
@@ -2350,24 +2350,28 @@
             
+    
 self.logger.
@@ -2480,465 +2480,182 @@
     
-@classmethod%0A    def prepare_table(cls, connection_provider):%0A        %22%22%22%0A        :param connection_provider: MySQLConnectionProvider to create table if not existing%0A        :type connection_provider: MySQLConnectionProvider%0A        %22%22%22%0A        connection = connection_provider.get_connection()%0A        cursor = connection.cursor()%0A        cursor.execute(%22create table chatlog(timestamp DATETIME, overhead INT, text VARCHAR(4000))%22)%0A        connection.commit(
+def compose_response(self):%0A        self.session.keep_mode = True if self.session.mode == %22srtr%22 else False%0A        return self.request.get_reply_message(str(self.session.data)
 )%0A

Fix #10 Output changed: MySQL to Logfile