Twilio Studio 和 Rebrandly link 缩短器 - Return 被解析而不是字符串
Twilio Studio and Rebrandly link shortener - Return parsed rather than string
我修改了一些说明,以便 Twilio Studio 通过与 Rebrandly API 对话的函数获得缩短的 URL。它工作正常,除了返回的数据是字符串而不是解析的,如下所示:
Rebrandly 提供的代码是:
exports.handler = function(context, event, callback) {
let response = { get_started: true };
let request = require("request");
let linkRequest = {
destination: "https://www.carecalls.co.uk/conferma-ricevuta?num=" + event.receiver,
domain: { fullName: "link.carecalls.co.uk" }
//, slashtag: "A_NEW_SLASHTAG"
//, title: ""
}
let requestHeaders = {
"Content-Type": "application/json",
"apikey": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"workspace": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
request({
uri: "https://api.rebrandly.com/v1/links",
method: "POST",
body: JSON.stringify(linkRequest),
headers: requestHeaders
}, (err, response, body) => {
let link = JSON.parse(body);
console.log(`Long URL was ${link.destination}, short URL is ${link.shortUrl}`);
callback(null, response);
});
};
我需要更改什么才能获得工作室可用于填充变量的解析列表?我是一个非常绿色的开发人员,所以希望这对某人来说是显而易见的!帮助非常感谢
在回调中,response 是返回给 Studio 的内容。如果响应是一个对象,Twilio Functions 会为您将其字符串化并将其发送回 Studio 进行解析。
https://www.twilio.com/docs/runtime/functions/invocation#callback-function
Callback Function
When you have finished processing your request, you
need to invoke the callback function to emit a response and complete
execution. The callback method will automatically determine the data
type of your response and serialize the output appropriately.
这里是 Twilio 开发人员布道者。
在您提供的函数中,您正在 return 将整个响应对象返回给 Twilio。该对象是 JSON 字符串化的,但由于响应的主体已经是 JSON,它已被双重转义。 Studio 将解析响应,但这只会解析正文一次,而不是所需的两次。
我建议不要 return 整个响应,而是 return 响应的正文。您已经将正文解析为变量 link
,因此只需 return 将该变量解析为 callback
函数即可:
}, (err, response, body) => {
let link = JSON.parse(body);
console.log(`Long URL was ${link.destination}, short URL is ${link.shortUrl}`);
callback(null, link); // << return link here
});
};
这样做将为您提供您想要使用的已解析响应正文。
我修改了一些说明,以便 Twilio Studio 通过与 Rebrandly API 对话的函数获得缩短的 URL。它工作正常,除了返回的数据是字符串而不是解析的,如下所示:
Rebrandly 提供的代码是:
exports.handler = function(context, event, callback) {
let response = { get_started: true };
let request = require("request");
let linkRequest = {
destination: "https://www.carecalls.co.uk/conferma-ricevuta?num=" + event.receiver,
domain: { fullName: "link.carecalls.co.uk" }
//, slashtag: "A_NEW_SLASHTAG"
//, title: ""
}
let requestHeaders = {
"Content-Type": "application/json",
"apikey": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"workspace": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
request({
uri: "https://api.rebrandly.com/v1/links",
method: "POST",
body: JSON.stringify(linkRequest),
headers: requestHeaders
}, (err, response, body) => {
let link = JSON.parse(body);
console.log(`Long URL was ${link.destination}, short URL is ${link.shortUrl}`);
callback(null, response);
});
};
我需要更改什么才能获得工作室可用于填充变量的解析列表?我是一个非常绿色的开发人员,所以希望这对某人来说是显而易见的!帮助非常感谢
在回调中,response 是返回给 Studio 的内容。如果响应是一个对象,Twilio Functions 会为您将其字符串化并将其发送回 Studio 进行解析。
https://www.twilio.com/docs/runtime/functions/invocation#callback-function
Callback Function When you have finished processing your request, you need to invoke the callback function to emit a response and complete execution. The callback method will automatically determine the data type of your response and serialize the output appropriately.
这里是 Twilio 开发人员布道者。
在您提供的函数中,您正在 return 将整个响应对象返回给 Twilio。该对象是 JSON 字符串化的,但由于响应的主体已经是 JSON,它已被双重转义。 Studio 将解析响应,但这只会解析正文一次,而不是所需的两次。
我建议不要 return 整个响应,而是 return 响应的正文。您已经将正文解析为变量 link
,因此只需 return 将该变量解析为 callback
函数即可:
}, (err, response, body) => {
let link = JSON.parse(body);
console.log(`Long URL was ${link.destination}, short URL is ${link.shortUrl}`);
callback(null, link); // << return link here
});
};
这样做将为您提供您想要使用的已解析响应正文。