{"content":"⚠️ Important Note on Security. This tutorial does not cover methods for authentication. While it’s possible to require authentication for additional security, this guide focuses on a quick demonstration of how to connect a custom GPT to a Google Doc. For sensitive applications, consider adding authentication steps to protect your data.\n\nStep-by-Step Guide: Connecting a Custom GPT to a Google Doc via Google Apps Script\nThis guide will walk you through the process of connecting a custom GPT to a Google Doc by setting up a Google Apps Script API. By following these steps, you’ll enable your custom GPT to access and retrieve the latest content from a Google Doc (such as this one), allowing for easy updates and seamless integration. This setup is ideal for sharing non-confidential instructions or data stored in Google Docs directly with your custom GPT, making it a versatile tool for automating workflows and keeping information up-to-date.\n\nFor more personalized support, try also using the ‘Google Docs to Custom GPT Assistant’ GPT.\nInstructions for Connecting a Custom GPT to a Google Doc\n1. Create and Share the Google Doc\nCreate a new Google Doc: Open Google Docs and create a new document.\nSet sharing permissions: Go to Share > General Access and set the sharing settings to Anyone with the link as Viewer.\n⚠️ Important: This will make the document’s content accessible to anyone with the link, so avoid adding any confidential or sensitive information.\n2. Open Google Apps Script and Add API Code\nOpen Apps Script: In the Google Doc, go to Extensions > Apps Script to open the Apps Script editor.\nGet help from ChatGPT to create an API: Start a new ChatGPT conversation, prompt it with a request like: \"Please write JavaScript code for Google Apps Script to retrieve the text content of a Google Doc by its ID and make it accessible via a web app URL in JSON format. The code should include a doGet function, which will allow accessing the document content when the web app URL is visited. Here is the link to the Google Doc for testing: [include document link here]\"\nProvide your Google Doc link: Share your Google Doc link with ChatGPT to ensure it has the correct document ID if needed.\nCopy the generated JavaScript code: Once ChatGPT provides the code, copy it and paste it into the Apps Script editor, replacing any default placeholder code.\n3. Deploy the API as a Web App\nDeploy as a web app:\nIn Apps Script, go to Deploy > New deployment.\nChoose Web app as the deployment type.\nSet Who has access to Anyone.\n⚠️ Important: Since anyone with access to this API can view your document's content, only include non-confidential information in the Google Doc.\n4. Test the API\nCopy the Web App URL: After deploying, copy the Web App URL from the deployment dialog.\nVerify the API with ChatGPT:\nReturn to your ChatGPT conversation and share the Web App URL.\nPrompt ChatGPT to generate a test link, such as: \"Create a test URL to retrieve my Google Doc content using this Web App URL.\"\nRun the test: Enter the test URL in a new browser tab to ensure it returns the Google Doc content successfully.\n5. Create a Custom GPT and Action in ChatGPT\nStart a new GPT:\nIn ChatGPT, go to Explore GPTs > + Create to start creating a new custom GPT.\nSet up a custom Action:\nIn the GPT setup, go to Actions > Create new action.\nClick Get help from ActionsGPT to open the schema generation tool.\n6. Create the OpenAPI Schema for the Action\nGenerate a schema:\nIn the ActionsGPT prompt, type: \"Create a simple schema based on this code, which creates an API to a Google Doc. Ensure the path is /exec:. Here is my Apps Script code and corresponding web app url:\"\nIn the same prompt, be sure to provide ActionsGPT with:\nThe Google Apps Script code used in Apps Script.\nThe Web App URL used in step 4 above (also found in Deploy > Manage deployments in Apps Script).\nAdjust schema path if needed: Verify that the schema includes path: /exec in the paths section. If ActionsGPT outputs path: /, change it to path: /exec for correct routing.\n7. Complete the Custom GPT Setup\nPaste the schema: Copy the generated schema from ActionsGPT and paste it into the Schema box in your new GPT.\nAdd finishing details: Click Back and complete the GPT setup by adding conversation starters, instructions, and an image. In the instructions, make sure to mention that the GPT can access the Google Doc for guidance.\n8. Test the Custom GPT\nRun a test: Interact with your new custom GPT to confirm it can retrieve and use content from the Google Doc.\nExamples\n1. Apps Script Sample Code\nBelow is an example of Google Apps Script code that creates an API endpoint to retrieve the content of a specific Google Doc.\nAssuming your Google Doc URL is: \nhttps://docs.google.com/document/d/1d8F9WcjqP-gB07OxVBML-1YX3e_GWqbFkHB3M_3Mq3g/\nUse the following code in Apps Script to create an endpoint that returns the document’s text:\n// API endpoint to return the content of a Google Doc\nfunction doGet(e) {\n  // Replace this with your document ID\n  const docId = \"1d8F9WcjqP-gB07OxVBML-1YX3e_GWqbFkHB3M_3Mq3g\";\n  \n  try {\n    // Open the document and fetch the body text\n    const doc = DocumentApp.openById(docId);\n    const docContent = doc.getBody().getText();\n    \n    // Return the content as a JSON response\n    return ContentService.createTextOutput(JSON.stringify({ content: docContent }))\n      .setMimeType(ContentService.MimeType.JSON);\n  } catch (error) {\n    // Handle errors, such as missing permissions or incorrect doc ID\n    return ContentService.createTextOutput(JSON.stringify({ error: error.message }))\n      .setMimeType(ContentService.MimeType.JSON);\n  }\n}\n\nExplanation of Code:\ndocId: Replace \"1d8F9WcjqP-gB07OxVBML-1YX3e_GWqbFkHB3M_3Mq3g\" with your Google Doc’s ID, found in the document’s URL.\nResponse:\nSuccess (200): Returns the document’s content as { \"content\": \"Your document text here\" }.\nError (400): Returns an error message if issues arise (e.g., missing permissions or invalid docId), formatted as { \"error\": \"error message here\" }.\n2. Custom GPT Sample Schema\nThis example OpenAPI schema specifies the setup for a custom GPT action to call the Google Apps Script API and retrieve the Google Doc content. Replace the sample URL with your Web App URL found in Deploy > Manage Deployments.\nopenapi: 3.1.0\ninfo:\n  title: Google Docs Content API\n  description: API to retrieve the content of a specific Google Doc by its document ID.\n  version: 1.0.0\nservers:\n  - url: https://script.google.com/macros/s/AKfycbzoRmi2_hUAlR6slEtTvaNkvRtg_i_AsTODw0qV2ky7YO3hTPqCTual5CuvQyl1ZRVs\n    description: Google Apps Script Web App URL\npaths:\n  /exec:\n    get:\n      operationId: getDocContent\n      summary: Retrieve the content of a Google Doc\n      description: Returns the text content of a Google Doc specified by the document ID hardcoded in the script.\n      responses:\n        '200':\n          description: Successfully retrieved content\n          content:\n            application/json:\n              schema:\n                type: object\n                properties:\n                  content:\n                    type: string\n                    description: The text content of the Google Doc\n        '400':\n          description: Error in retrieving content\n          content:\n            application/json:\n              schema:\n                type: object\n                properties:\n                  error:\n                    type: string\n                    description: Error message indicating the cause of failure\n\nKey Notes:\nservers URL: Replace with your Web App URL (found under Deploy > Manage Deployments in Apps Script).\npaths section: Ensure the path is set to /exec as required by the Google Apps Script deployment.\nThis schema will allow the custom GPT to call the Google Apps Script API endpoint, enabling it to retrieve the content of the Google Doc when the action is invoked.\n\r\n\n\n\n"}