ローカルで日本語を発話するようになった

This commit is contained in:
Akira
2026-03-03 12:37:26 +09:00
parent fe9ee0147c
commit 07258bb46d
5 changed files with 175 additions and 23 deletions

View File

@@ -130,31 +130,35 @@ app.post('/speak', async function(req, res) {
console.log(' -> ' + target.accountName + ' (type=' + target.deviceType + ', serial=' + target.serialNumber + ')');
// locale: '' はローカルPCでは日本語発話成功サーバーからは要検証
// Alexa.SpeakSsml 系は全滅のため Alexa.Speak に戻す
var sequenceObj = {
'@type': 'com.amazon.alexa.behaviors.model.Sequence',
startNode: {
'@type': 'com.amazon.alexa.behaviors.model.OpaquePayloadOperationNode',
type: 'Alexa.Speak',
operationPayload: {
deviceType: target.deviceType,
deviceSerialNumber: target.serialNumber,
customerId: customerId,
locale: '',
textToSpeak: text
},
},
};
// ★ 重要: sequenceJson の non-ASCII日本語等を \uXXXX エスケープに変換してから送る
// raw UTF-8 のまま送ると Amazon 側でフィルタリングされ日本語が発話されない(解決済み 2026-03-03
var sequenceObj = {
'@type': 'com.amazon.alexa.behaviors.model.Sequence',
startNode: {
'@type': 'com.amazon.alexa.behaviors.model.OpaquePayloadOperationNode',
type: 'Alexa.Speak',
operationPayload: {
deviceType: target.deviceType,
deviceSerialNumber: target.serialNumber,
customerId: customerId,
locale: 'ja-JP',
textToSpeak: text
},
},
};
var rawSequenceJson = JSON.stringify(sequenceObj).replace(
/[\u0080-\uffff]/g,
function(c) { return '\\u' + ('0000' + c.charCodeAt(0).toString(16)).slice(-4); }
);
var bodyStr = JSON.stringify({
behaviorId: 'PREVIEW',
sequenceJson: JSON.stringify(sequenceObj),
sequenceJson: rawSequenceJson,
status: 'ENABLED',
});
// リクエストボディをログ出力(認証・パラメータ確認用)
console.log('[DEBUG] sequenceJson:', JSON.stringify(sequenceObj, null, 2));
console.log('[DEBUG] textToSpeak:', text);
var ttsRes = await httpsRequest('/api/behaviors/preview', {
method: 'POST',

View File

@@ -61,7 +61,7 @@ async function main() {
});
// プレハブを探す
const target = devices.find(d => d.serialNumber === 'G0922H08525302K5');
const target = devices.find(d => d.serialNumber === 'G0922H08525302K5'); // オフィスの右エコー(以前成功したデバイス)
console.log('\nTarget device:', target ? `${target.accountName}` : 'NOT FOUND');
if (!target) { process.exit(1); }
@@ -72,7 +72,7 @@ async function main() {
const customerId = bootstrap.authentication?.customerId;
console.log(' Customer ID:', customerId);
// 3. TTSリクエスト
// 3. TTSリクエスト新Cookie + Alexa.Speak + locale:'ja-JP' + 日本語テキスト)
const sequenceObj = {
'@type': 'com.amazon.alexa.behaviors.model.Sequence',
startNode: {
@@ -83,19 +83,30 @@ async function main() {
deviceSerialNumber: target.serialNumber,
customerId: customerId,
locale: 'ja-JP',
textToSpeak: 'テストです。聞こえますか',
textToSpeak: '\u3053\u308c\u306f\u65e5\u672c\u8a9e\u306e\u30c6\u30b9\u30c8\u3067\u3059', // 「これは日本語のテストです」
},
},
};
// non-ASCII を \uXXXX に強制エスケープ
// Amazon のパーサーが sequenceJson 内の raw UTF-8 を処理できない場合の回避策
const rawSequenceJson = JSON.stringify(sequenceObj).replace(
/[\u0080-\uffff]/g,
c => '\\u' + ('0000' + c.charCodeAt(0).toString(16)).slice(-4)
);
const bodyObj = {
behaviorId: 'PREVIEW',
sequenceJson: JSON.stringify(sequenceObj),
sequenceJson: rawSequenceJson,
status: 'ENABLED',
};
const body = JSON.stringify(bodyObj);
console.log('\n[3] TTS送信...');
// 送信内容確認textToSpeakの部分が\uXXXXエスケープになっているか
const ttsIdx = body.indexOf('textToSpeak');
console.log(' textToSpeak部分:', body.substring(ttsIdx, ttsIdx + 80));
const ttsRes = await makeRequest('https://alexa.amazon.co.jp/api/behaviors/preview', {
method: 'POST',