Make chat GPT available in slack

2023年1月7日

Chat GPT is a hot topic as a chat bot that is too smart. We will introduce how to implement it with slack so that you can easily use it in your work.
If you can consult on slack, there is no doubt that the work of the team will progress.

How to get OpenAI API key

Get your OpenAI API key and get ready for server-side scripting.

Sign up and log in below. You can also register with a Google Account.

Log in and click Create New Secret Key to get a key for your API.
Now you are ready to script.

Google Apps Script (GAS) scripts

Prepare a server in a serverless environment with Google Apps Script.
Create a project at the following URL and create a script.

In the script, write down the character strings for [Your own OpenAI API key] and [Your own Slack Incomming URL]. The Slack URL will be explained in the next section on how to get it.

function doPost(e)
{

  var params = JSON.parse(e.postData.getDataAsString());
  if('challenge' in params)
  {
    return ContentService.createTextOutput(params.challenge);
  }


  var userName = params.event.user;
  var textSlack = params.event.text.substr(0, params.event.text.length);


  var resultStr = getChatGptMessage(textSlack);
 var contents = `<@${userName}> ${resultStr}`;

  var options =
  {
    "method" : "post",
    "contentType" : "application/json",
    "payload" : JSON.stringify(
      {
        "text" : contents,
        link_names: 1
      }
    )
  };
  UrlFetchApp.fetch("https://hooks.slack.com/services/[自身のSlack IncommingのURL]", options);
}

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');
  }
}

JavaScriptCopy

After creating the script, click New Deploy, click the gear and select Web app. For Who has, select anyone and click Deploy. Login and authorize with Authorized access.

Click Deploy to create a Web URL, copy it.

Create a Slack app

Login to slack and go to slack app directory.
Select Build on the top right.

Select Create an app to create your App.

Select From scratch and create with scratch.

Enter the App Name with any character string and select the channel to add the created App.
Click Create App.

Select Incoming Webhooks and get the URL to receive from Google Apps Script.

Set Activate Incoming Webhooks to “On".

Copy the WebhookURL and paste it into the Google App Script script created in the previous section.

Select Event Subscriptions from the left sidebar.

Enter the URL to send from Slack to the Google Apps Script side server.
A parameter including challenge is sent to the Google Apps Script Post function and checked.

If you can connect properly with both Google Apps Script and Slack’s Event Subscriptions, you can use chat GPT with slack.

Conclusion

I introduced how to make chat GPT touchable with slack.
If you are using slack for business, you will be able to use chat GPT as a consultant and your work will progress. Not only that, chatting will become more active, and organizational strength may increase. Please try using it.

We also have an article on how to use chat GPT on LINE.
If you are interested, please read along.

AI