Twilio Autopilot自定义通道Memory参数未传入Function如何解决?
Twilio Autopilot Memory参数传递失败解决方法
问题原因
你将Memory参数放在Autopilot自定义通道的请求URL查询参数中是不符合规范的,Twilio不会将URL上的自定义参数注入到后续触发的Twilio Function的event对象中,自定义Memory必须放在POST请求的表单字段内才能被正确传递。
修复步骤
- 把Memory参数从请求URL中移除,改为添加到POST请求体的表单字段中
- 无需手动编码,使用
URLSearchParams.append添加字段时会自动完成URL编码,避免JSON中的特殊字符导致解析错误
修复后的后端代码
// 构造自定义Memory数据 const context = JSON.stringify({ accountHolder: "foo bar", balance: 123 }, null, ""); // 构造请求参数 const urlencoded = new URLSearchParams(); urlencoded.append("Language", "en-US"); urlencoded.append("UserId", message.Author); urlencoded.append("Text", message.Body); // Memory放在请求体字段中 urlencoded.append("Memory", context); const requestConfig: AxiosRequestConfig = { headers: { "content-type": "application/x-www-form-urlencoded", "accept": "application/json", "authorization" : "Basic " + Buffer.from(accountSid + ":" + authToken).toString("base64") }, method: "POST", // 删除URL中的Memory参数 url: `https://channels.autopilot.twilio.com/v2/${accountSid}/${assistantSid}/custom/webchat`, data: urlencoded }; // 触发机器人请求 const twilioResponse = this.httpService.request(requestConfig); const result = await lastValueFrom(twilioResponse.pipe(map((response) => response.data)));
后续验证
你原本的Twilio Function代码无需修改,部署后即可正常从event.Memory中拿到你传递的JSON字符串,解析后就能读取accountHolder和balance字段的值。
注意:Autopilot不会自动持久化Memory内容,如果你需要在多轮对话中持续传递自定义数据,每次给Autopilot发送请求时都要携带当前最新的Memory字段。
内容的提问来源于stack exchange,提问作者xanadev
相关产品推荐
相关产品推荐

