How to Create a ChatGPT Bot in Zoho Cliq

02.25.23 11:39 AM By Andy

What we are going to do

In this tutorial we are going to go over how to create a bot that uses the ChatGPT API to respond to our questions. Once the bot is ready, we are going to add it to a channel. You will be able to use the bot directly or through the channel. We are going to use the ChatGPT-3 API, that is not the latest version. The ChatGPT-4 API is not available at the time of creating this tutorial. Once the newer API version is available, we will edit this article.


If you would like to learn how to create a Slash Command to ask questions to ChatGPT, check out this link.

Pre-requisites

  • An OpenAI account.

Creating the Bot

  1. In Zoho Cliq, click on your profile picture, and go to "Bots & Tools".
  2. On the Bots tab, click on the "Create Bot" button
  3. Name the Bot: for this example we are going to name it "ChatGPT"
  4. Choose the access level: we are going to choose "organization", so anyone in our organization can use this bot
  5. Under Channel Participation, select "Allow users to add this bot to any channel". Also check "Send messages", "Listen to Messages" and "auto-follow threads". This is important because we are going to add the bot to a channel later on.
  6. Optional: add a bot image
  7. Click on "Save Bot"

Adding the Message Handler Code

  1. On the "Bots" tab, hover over the bot and click on "Edit Handlers"
  2. Now click on "Edit Code" below "Message Handler"
  3. Copy the code below.
  4. Paste the code in the code box.
  5. In OpenAI account, go to the settings and click on the "API Keys" tab (or use this link).
  6. Generate a new secret key and copy it.
  7. Back in Zoho Cliq, replace the key "XXXXXXXXXXXX" with the one you generated.
  8. Cliq on Save.

That's it. The bot should be working. To test it, you can use the side chat panel. The first time you try to use it, you will have to grant some permissions first.
//Message Handler Code
response = Map();
question = message;
// Add Open AI token
token = "Bearer XXXXXXXXXXXX";
//
header = Map();
header.put("Authorization",token);
header.put("Content-Type","application/json");
params = {"model":"text-davinci-003","prompt":question,"temperature":0.4,"top_p":1,"max_tokens":256,"frequency_penalty":0,"presence_penalty":0,"stop":{" Human:"," AI:"}};
fetchCompletions = invokeurl
[
url :"https://api.openai.com/v1/completions"
type :POST
parameters:params.toString()
headers:header
detailed:true
];
info "Fetch completions: " + fetchCompletions;
if(fetchCompletions.get("responseCode") == 200)
{
answer = fetchCompletions.get("responseText").get("choices").getJSON("text");
response.put("text",answer);
}
else if(fetchCompletions.get("responseCode") == 429)
{
response = {"text":"I can't help with this. Try asking something else."};
}
return response;

Adding the Bot to a Channel and edit the Participation Handler

  1. Add the bot to the channel, as you would add a user to the channel.
  2. Now go to "Bots & Tools".
  3. On the "Bots" tab, hover over the ChatGPT Bot and click on "Edit Handlers".
  4. Now click on "Edit Code" below "Participation Handler".
  5. Copy the code below.
  6. Paste the code in the code box.
  7. Save the changes.
  8. Go back to the "Message Handler" code, and copy the ChatGTP API Key.
  9. Back in the "Participation Handler", replace the key "XXXXXXXXXXXX" with the one you copied.
  10. Save the changes again.

The bot is ready to answer to any questions you post on the channel. To test it out, just send a message in the channel and wait for the ChatGPT response.
//Participation Handler Code
response = Map();
if(operation == "message_sent")
{
if(data.get("message").get("type") == "text")
{
response = Map();
question = data.get("message").get("text");
token = "Bearer XXXXXXXXXXXX";
header = Map();
header.put("Authorization",token);
header.put("Content-Type","application/json");
params = {"model":"text-davinci-003","prompt":question,"temperature":0.9,"max_tokens":256,"top_p":1,"frequency_penalty":0,"presence_penalty":0,"stop":{" Human:"," AI:"}};
fetchCompletions = invokeurl
[
url :"https://api.openai.com/v1/completions"
type :POST
parameters:params.toString()
detailed : true
headers:header
];
if(fetchCompletions.get("responseCode") == 200)
{
answer = fetchCompletions.get("responseText").get("choices").getJSON("text");
info "answer" + answer;
response.put("text",answer);
}
else if(fetchCompletions.get("responseCode") == 429)
{
 response = {"text":"I can't help with this. Try asking something else."};
}
}
}
return response ;