// Cloud function: synthesize the 4 fixed training-voice prompts. // // We do NOT cache on the server. Two reasons: // 1. Caching via cloud storage requires self-constructing a fileID of // the form `cloud:///...`, but `getWXContext().ENV` // only returns the simple env id. Mismatched → unopenable fileID. // 2. Caching via the cloud database requires `cloud.database()` at // top level, which on this WeChat cloud runtime crashes the SCF // framework's `writeRuntimeFile` step (it can't toString() the // error object the SDK throws). // // The client (`utils/voice.js`) caches audioUrls in memory per session, // so each of the 4 prompts is synthesized at most once per app launch. // Re-synthesizing on app restart is fine — 800万 chars/month free tier // covers 4 × ~30 chars × thousands of restarts per month. // // Configuration (云开发控制台 → 云函数 → tts → 函数配置 → 环境变量): // TTS_SECRET_ID — Tencent Cloud API key id // TTS_SECRET_KEY — Tencent Cloud API key // TTS_REGION — (optional) defaults to ap-guangzhou // // NOTE: env var names cannot start with SCF_ / QCLOUD_ / TENCENTCLOUD_ — // those prefixes are reserved by Tencent Cloud SCF. Use the TTS_ prefix. const cloud = require('wx-server-sdk') cloud.init() // Polished, fixed prompts. const PROMPTS = { halfway: '已完成一半啦,坚持就是胜利!', last30: '最后30秒,保持呼吸,稳住姿势!', last10: '最后10秒,再加把劲!', complete: '太棒了!今天的目标已完成,继续加油!' } const CACHE_DIR = 'tts-cache' const _synthesize = async (text, promptKey) => { let TtsClient try { TtsClient = require('tencentcloud-sdk-nodejs').tts.v20190823.Client } catch (e) { throw new Error('tencentcloud-sdk-nodejs not installed. Run `npm i tencentcloud-sdk-nodejs` in this cloud function directory.') } const secretId = process.env.TTS_SECRET_ID const secretKey = process.env.TTS_SECRET_KEY if (!secretId || !secretKey) { throw new Error('TTS_SECRET_ID / TTS_SECRET_KEY not configured.') } const client = new TtsClient({ credential: { secretId, secretKey }, region: process.env.TTS_REGION || 'ap-guangzhou' }) const res = await client.TextToVoice({ Text: text, SessionId: `${promptKey}-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`, VoiceType: 101001, // 智瑜,温柔女声 Codec: 'mp3', SampleRate: 16000, Speed: 0, Volume: 5 }) return Buffer.from(res.Audio, 'base64') } const _upload = async (key, buffer) => { const cloudPath = `${CACHE_DIR}/${key}.mp3` // uploadFile returns the canonical fileID (e.g. "cloud://env.xxx/..."). // We must use THAT — not the relative cloudPath we passed in — when // calling getTempFileURL. A self-constructed fileID is invalid. const res = await cloud.uploadFile({ cloudPath, fileContent: buffer }) return res.fileID } exports.main = async (event) => { const { promptKey } = event || {} const text = PROMPTS[promptKey] if (!text) { return { success: false, error: `Unknown prompt key: ${promptKey}` } } try { const buffer = await _synthesize(text, promptKey) const newFileID = await _upload(promptKey, buffer) const urlRes = await cloud.getTempFileURL({ fileList: [newFileID] }) const url = urlRes.fileList[0].tempFileURL if (!url) { return { success: false, error: 'getTempFileURL returned empty URL', text } } return { success: true, audioUrl: url, text } } catch (e) { return { success: false, error: e.message, text } } }