チャットGPTをslackで使えるようにする
賢すぎるチャットbotとして、チャットGPTが話題になっています。気軽に業務に活かせるように、slackで実装する方法をご紹介します。
slackで相談できればチームの業務も捗ること間違い無しですね。
・OpenAIのAPIキーの取得方法
・Google Apps Script(GAS)のスクリプト
・Slackのappを作る
OpenAIのAPIキーの取得方法
OpenAIのAPIキーを取得して、サーバ側のスクリプト作成の準備をしましょう。
以下でSign upしてログインします。Google Accountでも登録できます。
ログインして、Create New Seacret Keyをクリックし、API用のキーを取得します。
これでスクリプト作成の準備完了です。
![](https://algo-ai.work/wp-content/uploads/2022/12/50c189c26aa29814b55b11eae65f1d64-1024x643.jpg)
Google Apps Script(GAS)のスクリプト
Google Apps Scriptで、サーバレス環境にてサーバを準備します。
以下のURLにてプロジェクトを作成してスクリプトを作成します。
スクリプトの中の[自身のOpenAIのAPIキー]と[自身のSlack IncommingのURL]には、控えた文字列を記載しましょう。SlackのURLは次のセクションで取得方法を説明します。
function doPost(e)
{
// slack appsのEvent Subscriptionsのchallenge。同期する時に利用。
var params = JSON.parse(e.postData.getDataAsString());
if('challenge' in params)
{
return ContentService.createTextOutput(params.challenge);
}
// ユーザ名とtextを取得
var userName = params.event.user;
var textSlack = params.event.text.substr(0, params.event.text.length);
// chatGPTに問い合わせ
var resultStr = getChatGptMessage(textSlack);
var contents = `<@${userName}> ${resultStr}`;
var options =
{
"method" : "post",
"contentType" : "application/json",
"payload" : JSON.stringify(
{
"text" : contents,
link_names: 1
}
)
};
// slack連携
UrlFetchApp.fetch("https://hooks.slack.com/services/[自身のSlack IncommingのURL]", options);
}
// ChatGPTのAPIを呼び出し応答を取得する
function getChatGptMessage(message) {
var uri = 'https://api.openai.com/v1/completions';
var headers = {
'Authorization': 'Bearer [自身のOpenAIのAPIキー]',
'Content-type': 'application/json',
'X-Slack-No-Retry': 1
};
var options = {
'muteHttpExceptions' : true,
'headers': headers,
'method': 'POST',
'payload': JSON.stringify({
"model": "text-davinci-003",
"max_tokens" : 2500,
"prompt": message})
};
try {
const response = UrlFetchApp.fetch(uri, options);
var json=JSON.parse(response.getContentText());
return json["choices"][0]["text"];
} catch(e) {
console.log('error');
}
}
スクリプトを作成しましたら、New Deployをクリックして、歯車をクリックしてWeb appを選択しましょう。Who hasではanyoneを選択して、Deployをクリックします。Authorized accessでログインして承認します。
DeployをクリックするとWeb URLができますのでコピーします。
![](https://algo-ai.work/wp-content/uploads/2022/12/faa80b4ce23aacc0025eed1cbf884904-1024x220.jpg)
Slackのappを作る
slackにログインして、slack app Directoryへ移動します。
右上のビルドを選択します。
![](https://algo-ai.work/wp-content/uploads/2022/12/4029ffc5af62f51e1249efbf583a0b49.jpg)
Create an appを選択し、Appを作成していきましょう。
![](https://algo-ai.work/wp-content/uploads/2022/12/218a8fa3dc898c92f98ebf0d431109a4-1024x258.jpg)
From scratchを選択し、scratchで作っていきます。
![](https://algo-ai.work/wp-content/uploads/2022/12/405d904d1b92e58f3336d9674337719f.jpg)
App Nameを任意の文字列で記載し、作成したAppを追加するチャンネルを選択します。
Create Appをクリックします。
![](https://algo-ai.work/wp-content/uploads/2022/12/86baa0b38e0f637b0896b19bc655cbec.jpg)
Incoming Webhooksを選択し、Google Apps Scriptから受け取るためのURLを取得得します。
![](https://algo-ai.work/wp-content/uploads/2022/12/d78a715d2ec780fd6adbf32154012492-1024x536.jpg)
Activate Incoming Webhooksを「On」にします。
![](https://algo-ai.work/wp-content/uploads/2022/12/285a49f17484889ef83cd4eae0622638-1024x544.jpg)
WebhookURLをCopyして、前のセクションで作成したGoogle App Scriptのスクリプトに貼り付けます。
![](https://algo-ai.work/wp-content/uploads/2022/12/4106c34348474c5910ad3be85d6b2a94-1024x295.jpg)
左側のサイドバーよりEvent Subscriptionsを選択します。
![](https://algo-ai.work/wp-content/uploads/2022/12/252a5b1cbdbf644591e3746aea4a16e6.jpg)
SlackからGoogle Apps Script側のサーバへ送信するためのURLを記載します。
challengeを含むparameterで、Google Apps ScriptのPost関数に送信され、チェックが入ります。
![](https://algo-ai.work/wp-content/uploads/2022/12/00e389178408253f14ace8214f55599f-1024x511.jpg)
Google Apps ScriptとSlackのEvent Subscriptionsの双方で正しく接続できるようになりましたら、slackでチャットGPTが使えます。
終わりに
チャットGPTをslackでさわれるようにする方法をご紹介しました。
slackを業務で使っている方は、相談役としてチャットGPTを使えるようになり仕事が捗りそうですね。それだけではなく雑談も活発になり、組織力も上がるかも知れません。使ってみてください。
LINEでチャットGPTを使う方法も記事にしています。
興味がある方は併せて読んでくださいね。