Development tools
Avetis Grigoryan
Senior Developer
October 19, 2023
A low-code platform blending no-code simplicity with full-code power 🚀
Get started free
October 19, 2023
6m
min read

A brief retelling of the google document with a translation into the desired language

Avetis Grigoryan
Senior Developer
Table of contents
Are you tired of copy-pasting your documents into translation software just to communicate with your international clients? Or maybe you're automating collaborating on a research paper with a partner who speaks a different language. Whatever the case may be, we've got some exciting news for you! Thanks to the integration of Google Docs and OpenAI, and with the help of Latenode.com, we're proud to offer a streamlined workflow that allows you to give a link to your Google document and translate it into the desired language. No more wasting time on manual translations - let technology work for you!

Preparing

First, as always, make sure that we have everything we need to create a chain: an account on ChatGPT and Personal key from it, as well as a Google account and the same token;

In Latenode not so long ago added integration with many services, while the exact documents have not yet been implemented, but soon should tighten it up as well. So for now we'll get the token in the old way according to the instructions in our article, only in the list choose "Google Docs API v..." scope "https://www.googleapis.com/auth/documents";

Personal key in ChatGPT can be viewed or created here in the profile settings:

Creating a chain in Latenode

The first thing we do, as always, is to create a node: "+ Add Node" => "Http" => "Webhook";

Let's copy his address at once;

Save the changes;

We will also prepare a preliminary request for the rest client (Postman, Insomnia):

REQ for rest client:


  curl --request POST
  --url https://webhook.latenode.com/69/dev/8b8b0b68-df1f-45ab-b1a3-94baf3a0626d
  --header 'Content-Type: application/x-www-form-urlencoded'
  --data 'personalTokenChatGPT=Your ChatGPT token'
  --data 'googleDocToken=Your google token'
  --data 'docID=ID of desired google document'
  --data 'language=german'

Change the Webhook link to yours;

Then let's substitute our ChatGPT key into personalTokenChatGPT;

Next, put your Google Doc token into googleDocToken;

Change docID to the ID of the desired document, it can be taken from the address bar;

If necessary, you can change the translation language in the language parameter;

Next add the "HTTP request" node to get the content of the document: "+ Add Node" => "Http" => "HTTP request";

Save;
Bind the nodes together;
Start and call the chain to transfer data between the nodes;

Let's form the "Url" field: let's start with "https://docs.googleapis.com/v1/documents/" address, followed by the parameter body.docID from the "Webhook" node;

Let's check that the default method is "GET";

In Headers let's add two:

- Content-Type with value: application/json
- Authorization in the value enter Bearer, then select the parameter from the "Webhook" node with the name: body.googleDocToken
Let's save the changes;

Bind the nodes together;
Start and call the chain to transfer data between the nodes;

Next add a "JavaScript" node to get the content of the document: "+ Add Node" => "Code" => "JavaScript";

Insert the content from the block below into it:


  const responseResult = JSON.parse(data["{{2.body.body.content}}"]);
  let docText = "";

  function parseResponse(response) {
     const tempArray = [];
     if (typeof response !== String) {
        tempArray.push(...response);
     } else {
        tempArray.push(JSON.parse(...response));
     }
     Array.isArray(response)
          ? response.forEach(
              (arrayItem) => { 
                 if (arrayItem.paragraph) {
                    arrayItem.paragraph.elements.forEach(paragraphItem => {
                       docText += `${paragraphItem.textRun.content}`;
                    });
                 }
              }
           )
          : Object.fromEntries(
              Object.entries(response)
                  .map(([objectItemKey, objectItemValue]) => [objectItemKey, parseResponse(objectItemValue, s)]));
  }
  parseResponse(responseResult);
  const resultRawJSON = JSON.stringify({
    "model":"text-davinci-003",
    "prompt":`Write a brief retelling of the main point of the text on ${data["{{1.body.language}}"]}: ${docText}`,
    "temperature":0,
    "max_tokens":1400
  });
  return {
     resultRawJSON
  }

Save;
Bind the nodes together;
Start and call the chain to transfer data between the nodes;

Open the "JavaScript" node settings again;

Let's check in all constructions like "data["{{2.body.body.content}}"]" and "data["{{2.body.body.content}}"]" that the node numbers correspond to those from which we receive data, if not we replace them with the necessary ones (remind that 1 is the node number which is written immediately below the node name above the node type, then if it is a query then we select the object field responsible for the type of data sent/received - body for forms or query for query parameters);

If there were changes, let's save them;

Now let's add another "Http request": "+ Add node" => "Http" => "HTTP request";

Save;
Bind the nodes together;
Start and call the chain to transfer data between the nodes;

Let's form the "Url" field: let's put "https://api.openai.com/v1/completions" address in the beginning;

Change the request method to "POST";

At the body in "raw": let's substitute the variable resultRawJSON from the previous "JavaScript" node;

In Headers let's add two:

- Content-Type with value: application/json
- Authorization with the value: Bearer, then select the parameter from the "Webhook" node with the name: body.personalTokenChatGPT
Let's save the changes;

At the end, to return the text received from ChatGPT let's add the "Webhook response" node: "+ Add Node" => "Http" => "Webhook response";

Save;
Bind the nodes together;
Run and call the chain to transfer data between the nodes;

Fill in the response with the ready variant if there was a successful response from the previous request body.choices.[0].text:

Let's run the full chain and call it;

After that, the answer should return a brief paraphrase of the document in English;

The finished chain will look like this at the end;

Thanks for your attention, see you in new articles!😉

Links:


Basic:

Latenode

ChatGPT

ChatGPT API Key

Info:

ChatGPT API Documentation

Google API Documentation

Related Blogs

Use case

Backed by