node.js - Google Node.js SDK unicode 上的操作
问题描述
如何在 Google Assistant 中非常简单地显示欧元符号?每次我尝试以不同的编码获得符号时。我错过了什么简单的事情?
谷歌上的行动 SDK 版本 2.0.1
const { dialogflow } = require('actions-on-google')
const app = dialogflow({ debug: true })
app.intent('euro-intent', (conv) => {
console.log('€')
conv.ask('€')
})
exports.myBot = app
我的操作是调用 AWS API Gateway 上的 Webhook,该 Webhook 使用 Node.js v 8.10 连接到 Lambda 函数。CloudWatch 日志显示
{
"payload": {
"google": {
"expectUserResponse": true,
"richResponse": {
"items": [
{
"simpleResponse": {
"textToSpeech": "€"
}
}
]
},
"userStorage": "{\"data\":{}}"
}
},
"outputContexts": [
{
"name": "projects/newagent-9bde7/agent/sessions/1525808242247/contexts/_actions_on_google",
"lifespanCount": 99,
"parameters": {
"data": "{}"
}
}
]
}
但是,我在模拟器中得到以下信息。
解决方案
在您的 webhook 中,为EURO SIGN使用 unicode 。EURO SIGN的 unicode是U+20AC
在您的Node.js 实现中,使用'\u20ac'表示法。
在字符串值中,“\u”表示法表示该值是由四个十六进制数字表示的 unicode 字符。
这是具有 unicode 值的版本:
const { dialogflow } = require('actions-on-google')
const app = dialogflow({ debug: true })
app.intent('euro-intent', (conv) => {
console.log('\u20ac')
conv.ask('\u20ac')
})
exports.myBot = app