Integrate OpenAI’s ChatGpt within Slack using MuleSoft.

Enalist Tamang
Cloudwerx
Published in
5 min readAug 1, 2023

--

Slack is an instant workplace messaging tool through which you can send messages and files.

ChatGPT is a highly advanced language model that can generate human-like responses. The versatile language model can respond to questions and compose various written content, including articles, social media posts, essays, code, and emails. It is proficient with STEM disciplines and can debug or write code rarely setting any limitation to the types of questions to ask ChatGPT.

In this blog, we will show you how to integrate ChatGPT with Slack and use it to questions answer and have conversations about your favorite topics.

Why we built the ChatGPT app for Slack?

Bringing ChatGPT, a model that has this immense knowledge and capability for information processing, into the place where you’re already communicating and working with your colleagues is magic.

We want people to be able to use the app right within Slack, where they’re working through complex problems and making decisions, without context switching back and forth between different applications. Having this powerful AI technology embedded within Slack to deliver instant conversation summaries, research tools, and writing assistance will help millions of people work more productively.

Pre-requirements:

· Slack

· Anypoint Studio

· Anypoint Platform

· OpenAI API Key

Step1: Register an app with Slack and obtain tokens

  1. Log in to your Slack workspace.
  2. Go to the Slack API website Click on “Create an app” and select “From scratch”

Give your app a name, and select your Slack workspace.

4. In Basic information > Add features and functionality. Click on “Permissions” and in Scopes add in Bot Token Scopes: channels:history ; channels:read ; chat:write; im:history. (As per your requirement)

Step 2: Create Mule application and Deploy to Cloudhub.

  1. Create the mule application.
  2. Add Slack connector module from the exchange.
  3. Deploy the application to Cloudhub to get the application URL.
  4. Configure your Slack connector.

Guide through the steps are as follows:

a. Navigate to your Slack app (chatGpt in my case). Go to Basic information. Explore the App credentials. You can find the clientId(consumer key) and clientSecret(consumer secret).

b. Also add scopes as per your requirement.

c. Configure your HTTPS_Listener_config.

Set Callback Path as /callback

Set Authorize Path as /authorize

Set your external callback URL: https://{cloudhub-basepath}/callback

Step 3. Redirect URLs in the Slack app.

You will need to configure redirect URLs in order to automatically generate the Add to Slack button or to distribute your app. Therefore, go to the “OAuth & Permissions” section and install your app in your workspace.

Step 4. Perform OAuth Dance

In the browser hit the URL: https://slack-system-connect-api.au-s1.cloudhub.io/authorize

Click on Allow, you’ll see the below message which indicates that the token is retrieved.

Step 5. Enable Events in Slack App

Your app can subscribe to be notified of events in Slack at a URL you choose. Enabling even can trigger your MuleSoft application automatically.

Enter your MuleSoft application endpoint: https://slack-system-connect-api.au-s1.cloudhub.io/event.

Slack will send HTTP POST requests to this URL when events occur. As soon as you enter a URL, a challenge parameter will be sent, and your endpoint must respond with the challenge value.

In the mule application use one transform message to receive challenge and to send back challenge value back to slack with status code 200 ok.

Once the channel is established, you will see verified ✅

Step 6. Mule Flow Trigger

When a text is sent from the slack app, an event is triggered. Send a request to Open AI chatGPT “https://api.openai.com:443/v1/chat/completions” by passing the Authorization Bearer Token.

After getting a response from the chatGPT, send the payload to the slack app via https://slack.com/api/chat.postMessage passing the Authorization Bearer Token in the Header.

Demo of working app :

The basics are now in your hands to tweak the above and solve your own use case.

Also, don’t forget to explore the Interactive features to achieve much more.

Happy Learning!

Special Note: If you found our information valuable and would like to hear more about how Cloudwerx can help take you and your business to the next level, we’d love to hear from you. Get in touch with us at
🌐www.cloudwerx.co or 📧 hello@cloudwerx.co.

--

--