const ai = function (prompt) { return new Promise(async function (resolve, reject) { try { var getToken = function () { return new Promise(function (resolve, reject) { var requestOptions = { method: "GET", headers: { "Authorization": "Bearer bk0BD43805-46E6-4733-A0C6-32F002164731", "Content-Type": "application/json" }, redirect: "follow", }; UrlFetchApp.fetch("https://bitoai.bito.co/ai/token/", requestOptions) .then((response) => response.getContentText()) .then((result) => { result = JSON.parse(result).token; resolve(result); }); }); }; var body = { prompt: prompt, uId: "3fea5b09b3b4c756e2d4487d7a078167751ae88c917fde7c5f878f47dc52aa", }; var requestOptions = { method: "POST", headers: { "Authorization": "Bearer " + (await getToken()), "Content-Type": "application/json" }, body: JSON.stringify(body), redirect: "follow", }; var result = JSON.parse(await ( await UrlFetchApp.fetch("https://bitoai.bito.co/ai/chat/", requestOptions) ).getContentText()); resolve(result.response); } catch (e) { reject(e); } }); }