Firebase功能出现印地语字符故障

时间:2019-06-14 10:29:37

标签: node.js firebase encoding dialogflow hindi

Firebase函数正在调用this Hindi API来进行dialogflow操作。 firebase函数正在发送Error in API call,但是在与english API一起测试时,它可以正常工作,但是那时我的查询文本也是英语。预期的输出是古兰经的经文。

我认为问题出在查询文本。它需要使用utf-8编码或类似的格式吗? firebase函数如下:

const functions = require('firebase-functions');
var request1 = require('request');

exports.webhook = functions.https.onRequest((request, response) => {
   console.log("request.body.result.parameters: ", request.body.queryResult.parameters);
   let params = request.body.queryResult.parameters;

   if (request.body.queryResult.intent.displayName === "verseByVerse") {
      var options = {
         url: `https://theislam360.me/api/find/hindi/${params.any}`,
         json: true
      }
      request1(options, function (error, res, body) {
         if (error) response.send({ fulfillmentText: "error in API call" });
         else {
            var audio ="";
            var length = Object.keys(body).length;
            var surah_id = [];
            var ayat_no = [];
            for (i = 1; i <= length; i++) {
               surah_id.push(body[`result_${i}`].Surah_No);
               ayat_no.push(body[`result_${i}`].Ayat_No);
               if ((surah_id[i-1]<10) && (ayat_no[i-1]<10)){
                  audio = audio + 
               `Surah Number ${surah_id[i-1]}, Ayat Number ${ayat_no[i-1]}<audio src='https://theislam360.me/api/play/versebyverse/00${surah_id[i-1]}00${ayat_no[i-1]}.mp3'></audio>`;   
               }
               else if ((surah_id[i-1]<10) && (ayat_no[i-1]>9) && (ayat_no[i-1]<100)){
                  audio = audio +
               `Surah Number ${surah_id[i-1]}, Ayat Number ${ayat_no[i-1]}<audio src='https://theislam360.me/api/play/versebyverse/00${surah_id[i-1]}0${ayat_no[i-1]}.mp3'></audio>`;   
               }
               else if ((surah_id[i-1]<10) && (ayat_no[i-1]>99)){
                  audio = audio +
               `Surah Number ${surah_id[i-1]}, Ayat Number ${ayat_no[i-1]}<audio src='https://theislam360.me/api/play/versebyverse/00${surah_id[i-1]}${ayat_no[i-1]}.mp3'></audio>`;   
               }
               else if ((surah_id[i-1]>9) && (surah_id[i-1]<100) && (ayat_no[i-1]<10)){
                  audio = audio +
               `Surah Number ${surah_id[i-1]}, Ayat Number ${ayat_no[i-1]}<audio src='https://theislam360.me/api/play/versebyverse/0${surah_id[i-1]}00${ayat_no[i-1]}.mp3'></audio>`;   
               } 
               else if ((surah_id[i-1]>9) && (surah_id[i-1]<100) && (ayat_no[i-1]>9) && (ayat_no[i-1]<100)){
                  audio = audio +
               `Surah Number ${surah_id[i-1]}, Ayat Number ${ayat_no[i-1]}<audio src='https://theislam360.me/api/play/versebyverse/0${surah_id[i-1]}0${ayat_no[i-1]}.mp3'></audio>`;   
               }
               else if ((surah_id[i-1]>9) && (surah_id[i-1]<100) && (ayat_no[i-1]>99)){
                  audio = audio +
               `Surah Number ${surah_id[i-1]}, Ayat Number ${ayat_no[i-1]}<audio src='https://theislam360.me/api/play/versebyverse/0${surah_id[i-1]}${ayat_no[i-1]}.mp3'></audio>`;   
               } 
               else if ((surah_id[i-1]>99) && (ayat_no[i-1]<10)){
                  audio = audio +
               `Surah Number ${surah_id[i-1]}, Ayat Number ${ayat_no[i-1]}<audio src='https://theislam360.me/api/play/versebyverse/${surah_id[i-1]}00${ayat_no[i-1]}.mp3'></audio>`;   
               }
               else if ((surah_id[i-1]>99) && (ayat_no[i-1]>9) && (ayat_no[i-1]<100)){
                  audio = audio +
               `Surah Number ${surah_id[i-1]}, Ayat Number ${ayat_no[i-1]}<audio src='https://theislam360.me/api/play/versebyverse/${surah_id[i-1]}0${ayat_no[i-1]}.mp3'></audio>`;   
               }
               else if ((surah_id[i-1]>99) && (ayat_no[i-1]>99)){
                  audio = audio +
               `Surah Number ${surah_id[i-1]}, Ayat Number ${ayat_no[i-1]}<audio src='https://theislam360.me/api/play/versebyverse/${surah_id[i-1]}${ayat_no[i-1]}.mp3'></audio>`;   
               }
               if (surah_id[i-1]>90){
                  audio = audio + `<audio src='https://theislam360.me/api/play/versebyverseurdu/${surah_id[i-1]}-${ayat_no[i-1]}.mp3'></audio>`
               }
               // audio = audio +
               // `<audio src="https://theislam360.me/api/play/versebyverse/002005.mp3"/>`;
            }
            response.send({fulfillmentText:`<speak>
            ${audio}
            Let’s play the SSML Trivia Game!
            <break time="500ms"/>
            </speak>`})
   }
}
      )}
});

आदमी表示男人

RAW API响应

{
  "responseId": "8e0f258a-5931-4364-9f45-b4d283d95584-baaf0c1f",
  "queryResult": {
    "queryText": "क़ुरान आदमी के बारे में कहाँ बात करता है?",
    "parameters": {
      "any": "आदमी"
    },
    "allRequiredParamsPresent": true,
    "fulfillmentText": "error in API call",
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            "error in API call"
          ]
        }
      }
    ],
    "intent": {
      "name": "projects/versebyversequran/agent/intents/7c009ede-8b68-4db1-9cf6-b3cf1241691a",
      "displayName": "verseByVerse"
    },
    "intentDetectionConfidence": 0.7840172,
    "diagnosticInfo": {
      "webhook_latency_ms": 122
    },
    "languageCode": "en"
  },
  "webhookStatus": {
    "message": "Webhook execution successful"
  }
}

注意:如果您需要其他物品,请告诉我

1 个答案:

答案 0 :(得分:0)

您将收到Request path contains unescaped character错误,因此您需要encodeURI

var request1 = require('request');
var options = {
    url: encodeURI(`https://theislam360.me/api/find/hindi/आदमी`),
    json: true
 }
 request1(options, function (error, res, body) {
     console.log(error);
     console.log(body);
 })