Created
April 13, 2023 17:45
-
-
Save Ancient-Scapes/ab884b15fad80932c24ac03de14a2feb to your computer and use it in GitHub Desktop.
GPT-4 APIからSlack Webhookに投稿する奴
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
function generateResponse() { | |
// OpenAI APIキーとChatGPTのエンドポイントを設定する | |
const openaiApiKey = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxx"; | |
const openaiEndpoint = "https://api.openai.com/v1/chat/completions"; | |
// OpenAIに送信するテキストを設定する | |
const prompt = `xxxxxx`; | |
// OpenAIにリクエストを送信する | |
const payload = { | |
model: "gpt-4", | |
messages: [ | |
{ | |
role: "user", | |
content: prompt | |
} | |
] | |
}; | |
Logger.log("Open AI API Requst..."); | |
const response = UrlFetchApp.fetch(openaiEndpoint, { | |
method: "post", | |
headers: { | |
"Content-Type": "application/json", | |
"Authorization": `Bearer ${openaiApiKey}` | |
}, | |
payload: JSON.stringify(payload) | |
}); | |
Logger.log("Open AI API Requst Done!"); | |
// OpenAIからのレスポンスをパースする | |
const result = JSON.parse(response.getContentText()); | |
Logger.log(result); | |
const message = result.choices[0].message.content; | |
// Slackに投稿する | |
// Slack Incoming Webhook URLを設定する | |
const slackWebhookUrl = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; | |
// Webhookをトリガーするペイロードを作成する | |
const payloadSlack = { | |
text: message | |
}; | |
Logger.log("Slack Webhook Requst..."); | |
UrlFetchApp.fetch(slackWebhookUrl, { | |
method: "post", | |
headers: { | |
"Content-Type": "application/json" | |
}, | |
payload: JSON.stringify(payloadSlack) | |
}); | |
Logger.log("Slack Webhook Requst Done!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment