NetRVA

Building a ChatGPT AI Chatbot for Zulip with Google Apps Script: A Step-by-Step Guide

Written by Michael Foutz | Feb 4, 2024 3:30:48 PM

By harnessing the power of the ChatGPT API and the Zulip API, we can unlock a whole new level of seamless and dynamic conversations. With the ChatGPT API, our chatbot becomes a master at understanding and responding to user queries in real-time. And by seamlessly integrating the Zulip API, our chatbot becomes a valuable asset to the Zulip platform, enhancing team communication to new heights. All of this is made possible through the ingenious utilization of Google Apps Script, a free service that adds an extra layer of convenience to the mix. So, let's embark on this exciting journey and create our very own AI-powered ChatBot that will change the way we communicate forever!

 

Prerequisites

Before diving in, ensure you have:

  • A Zulip account with administrative access to add bots.
  • Basic familiarity with Google Apps Script.
  • Get Code https://github.com/Net-RVA/Netty-Zulip-API-Chatbot-AppScript/tree/main
  • An OpenAI API key to integrate ChatGPT functionalities.

Step 1: Setting Up Your Zulip Bot

Firstly, you'll need to create a bot in Zulip. This bot will serve as the bridge between Zulip and the AI functionalities we're about to implement.

  1. Navigate to your Zulip settings and find the Bots section under Your bots.

  2. Click on Add a new bot, select Incoming webhook, and give your bot a name and username (e.g., Netty).

  3. Once created, Zulip will provide you with a bot email and an API key. Keep these details safe; you'll need them for the script.

Step 2: Preparing the Script

Head over to Google Apps Script and create a new project. Here, you'll write the script that connects Zulip with OpenAI's ChatGPT.

code
var zulipBotEmail = "your-bot-email@zulipchat.com";
var zulipBotApiKey = "your-zulip-api-key";
var zulipDomain = "your-zulip-domain.zulipchat.com";
var streamName = "Netty AI";
var lastMessageId = -1;

const apiKey = 'your-openai-api-key'; // OpenAI API key
const apiUrl = 'https://api.openai.com/v1/chat/completions'; // OpenAI API URL

 

Replace placeholder values with your actual Zulip bot email, Zulip API key, domain, and OpenAI API key.

Step 3: Crafting the Core Functions

Your script will need functions to subscribe to streams, poll for new messages, process these messages, and then call the OpenAI API for responses.

Subscribing to Streams
code
function subscribeToStream() {
var apiUrl = "https://" + zulipDomain + "/api/v1/users/me/subscriptions";
var options = {
method: "POST",
headers: {
Authorization: "Basic " + Utilities.base64Encode(zulipBotEmail + ":" + zulipBotApiKey)
},
payload: {
subscriptions: JSON.stringify([{ name: streamName }])
}
};
UrlFetchApp.fetch(apiUrl, options);
}
 
 
Polling for New Messages
code
function pollZulipStream() {
var apiUrl = "https://" + zulipDomain + "/api/v1/messages";
apiUrl += "?anchor=newest&num_before=5&num_after=0";
apiUrl += "&narrow=" + encodeURIComponent(JSON.stringify([{ operator: "stream", operand: streamName }]));
// Additional code to process messages goes here...
}
 

Step 4: Generating AI Responses

Use the OpenAI API to generate responses based on the messages your bot receives. The callChatGptApi function sends the message content to OpenAI and retrieves an AI-generated response.

code
function callChatGptApi(question) {
var prompt = "Question:" + question;
var payload = {
model: "gpt-3.5-turbo",
messages: [{ role: "system", content: "Your name is Netty and you are a Zulip chat assistant." },
{ role: "user", content: prompt }],
temperature: 0.9,
max_tokens: 800,
};
// API call setup and response processing...
}
 

Step 5: Deploying and Testing Your Bot

After implementing the functions, deploy your script. Test it by sending a message in your designated Zulip stream and watch your bot spring into action, providing intelligent, AI-generated responses.

Customization and Beyond

Your ChatGPT AI chatbot is now set up! Feel free to customize the script further to suit your team's needs. Adjust the maxTokens and temperature parameters to fine-tune the bot's responses, or explore more advanced integrations and functionalities.

Conclusion

Integrating a ChatGPT AI chatbot into Zulip using Google Apps Script opens a world of possibilities for enhancing team communication and workflow. By following this guide, you've taken a significant step towards leveraging the power of AI in your daily interactions. Continue to experiment, customize, and innovate to make the most out of your new AI assistant.