yuvraj singh
yuvraj singh

Reputation: 65

error with non english language like russian in dialogflow api v2 version

I have own HTTP server with static IP. on this server I am running my flask application to get requests from dialogue flow-fulfilment which is running on v2 API. when I am using my agent with the English language then it parses it correctly at flask application but when I use the Russian language then at flask side I getting garbage output which containing sequence of ????????.

I try to set charset = Unicode at dialog box but it still not working.

import json,os,sys,logging
from flask import Flask, request 
from flask import jsonify
from configparser import ConfigParser
import objectpath

def setup_custom_logger(name):
    formatter = logging.Formatter(fmt='%(asctime)s %(levelname)-8s 

%(message)s',datefmt='%Y-%m-%d %H:%M:%S')
handler = logging.FileHandler('log.txt', mode='w')
handler.setFormatter(formatter)
screen_handler = logging.StreamHandler(stream=sys.stdout)
screen_handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
logger.addHandler(screen_handler)
return logger

logger = setup_custom_logger('myapp')
app = Flask(__name__) 

@app.route('/flightSearchByRoute', methods=['POST'])
def interactionPredictions():
    try:
        response = {
                "status": 0,
                "query": "ERROR"
            }
        if request.json['result']: #if key doesn't exist, returns None
            print("request",request)
            print("key output",request.json['queryResult']['queryText'])
            print("parsed",json.dumps(request.json,indent=4, sort_keys=True))
            toLocation = request.json['queryResult']['parameters']['to']
            fromLocation = request.json['queryResult']['parameters']['from']
            qTime = request.json['queryResult']['parameters']['time']
            outputContexts = request.json['session']#['outputContexts'][0]['name']
            print("parameters",fromLocation,toLocation,qTime,outputContexts)
            response = { "fulfillmentText": "Success API HIT to: "+toLocation+" from: "+fromLocation+ " time: "+qTime ,"fulfillmentMessages":[ { "text": { "text": [ "Success API HIT to: "+toLocation+" from: "+fromLocation+ " time: "+qTime,] } } ] ,"source":"" }
print(json.dumps(response,indent=4, sort_keys=True))
        return jsonify(response),200
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        print(exc_type, fname, exc_tb.tb_lineno, e)
        response = {
                "status": 0,
                "query": "ERROR"
            }
        return jsonify(response),400


@app.errorhandler(404)
def page_not_found(e):
    response = {
            "status": 0,
            "query": "ERROR"
        }
    return jsonify(response),400

if __name__ == '__main__':
    app.run(host='0.0.0.0',debug=True, port=8888) #run app in debug mode on port 5344

response send from dialog flow:
{
  "id": "97feddb0-c24d-4a4e-bec5-5823a3374fb2",
  "timestamp": "2019-04-11T13:46:07.884Z",
  "lang": "ru",
  "result": {
    "source": "agent",
    "resolvedQuery": "из Лондона в Нью-Йорк 5 утра",
    "action": "",
    "actionIncomplete": false,
    "parameters": {
      "time": "05:00:00",
      "from": "Лондона",
      "to": "Нью-Йорк"
    }
response getting at flask end
{
    "originalDetectIntentRequest": {
        "payload": {}
    },
    "queryResult": {
        "allRequiredParamsPresent": true,
        "fulfillmentMessages": [
            {
                "text": {
                    "text": [
                        "okk"
                    ]
                }
            }
        ],
        "fulfillmentText": "okk",
        "intent": {
            "displayName": "bookFlight",
            "name": "projects/flightquery-82f02/agent/intents/c032518b-f882-4f7f-a49b-fcf57155eeaf"
        },
        "intentDetectionConfidence": 1.0,
        "languageCode": "ru",
        "parameters": {
            "from": "???????",
            "time": "2019-04-12T05:00:00+05:30",
            "to": "???-????"
        },
        "queryText": "?? ??????? ? ???-???? 5 ????"
    },
    "responseId": "ec09cc39-6c41-44f9-819a-dd7564ef4f92",
    "session": "projects/flightquery-82f02/agent/sessions/559a2540-07d1-fc35-4002-04d179f0b55f"
}

actual queryText is: "из Лондона в Нью-Йорк 5 утра" received queryText is : "?? ??????? ? ???-???? 5 ????"

Upvotes: 0

Views: 342

Answers (1)

yuvraj singh
yuvraj singh

Reputation: 65

I have resolved my problem. I was setting header as "Content-Type: application/json" in fulfilment of dialogflow and because of this is creating the problem. after removing this I am getting the Unicode text at flask point and now it works fine. thanks, overflow team.

Upvotes: 1

Related Questions