Last active
March 11, 2023 13:17
-
-
Save ahxxm/fca75a07d61d8bfa3e000a25a9e060cc to your computer and use it in GitHub Desktop.
a patch for TBXark/ChatGPT-Telegram-Workers to use notion API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function sendMessageToChatGPT(message, history) { | |
try { | |
const payload = { | |
model: "openai-3", | |
spaceId: "", // your space id here | |
isSpacePermission: false, | |
id: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | |
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); | |
return v.toString(16); | |
}), | |
context: { | |
pageTitle: "", | |
// previousContent: [history].join("\n"), // no chat, just completion | |
previousContent: "", | |
restContent: "", | |
type: "helpMeWrite", | |
prompt: message | |
} | |
}; | |
const resp = await fetch("https://www.notion.so/api/v3/getCompletion", { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
accept: "application/json", | |
cookie: `token_v2=${ENV.API_KEY}` | |
}, | |
body: JSON.stringify(payload) | |
}) | |
const text = await resp.text(); | |
const splits = text.trim().split("\n").map(x => JSON.parse(x).completion); | |
const msg = splits.join("").trim(); | |
// just count naive tokens | |
setTimeout(() => updateBotUsage({total_tokens: splits.length}), 0); | |
return msg; | |
} catch (e) { | |
console.error(e); | |
return `\u6211\u4E0D\u77E5\u9053\u8BE5\u600E\u4E48\u56DE\u7B54 | |
> ${e.message}}`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment