In our last post, we built a private Telegram bot powered by Gemini 2.5 Flash that turns raw vocabulary words into beautiful study cards. We turned Google Apps Script into a fully functional, zero-cost cloud endpoint. If you missed that setup, you can catch up here
Right now, our bot is a great conversationalist, but it has short-term memory loss. The second you clear your chat history, your words vanish. Plus, every time you send it a word, you pay the "AI token tax"—even if you look up the exact same word three times a day.
Today, we are going to fix both problems. We will extend our bot to automatically check a Google Sheet database first to save AI tokens, save fresh words permanently, and build an automated production log tracker. Along the way, we'll peel back the curtain on how Google Cloud Platform (GCP) handles your script's architecture under the hood—and why setting up a custom GCP project fixes silent runtime crashes.

Here is the updated blueprint of our optimized system:
[ Telegram Webhook Request ]
│
▼
┌──────────────────────────────────┐
│ Google Apps Script (GAS) │
└─────────────────┬────────────────┘
│
┌───────────────────────┴───────────────────────┐
▼ ▼
【 OLD DEFAULT SETUP 】 【 NEW CUSTOM GCP PROJECT 】
• Anonymous runtime identity • Verified OAuth Owner Identity
• Low-priority processing • High-priority processing
• Silent write dropping • Bulletproof data commits
│ │
└───────────────────────┬───────────────────────┘
│
▼
┌──────────────────────────────┐
│ Step 1: Sheet Cache Check │
└──────────────┬───────────────┘
│
┌──────────────┴───────────────┐
│ Step 2: Gemini 2.5 API │ (Only if not in sheet!)
└──────────────┬───────────────┘
│
▼
┌──────────────────────────────┐
│ Step 3: Google Sheets │
└──────────────┬───────────────┘
│
┌───────────────────────┴───────────────────────┐
▼ ▼
[ Vocabulary Tab ] [ Logs Tab ]
• Word (A) • Timestamp (A)
• Created Time (B) • Input Word (B)
• Description (C) • Status (C)
• Sentence (D) • Details/Error Trace (D)
• Synonyms (E) ▲
• Way to Remember (F) │
│ │
└───────────────[ SpreadsheetApp.flush() ]──────┘
Forces instant recording!
Part 1: The Code Upgrade
We're adding a brand new lookup and save layer in sheet.gs and configuring main.gs to run a "cache-first" check before burning any AI tokens.
1. Preparing Your Google Sheet Database
Before writing code, create a fresh Google Sheet (type sheets.new into your browser bar) and name your first tab Vocabulary. Set up your header row in Row 1 across columns A to F exactly like this:
Word | Created Time | Description | Usage | Synonyms | Way to Remember
Create a second tab at the bottom and name it Logs. This will act as our private production terminal window.
Now, grab your unique Spreadsheet ID from your browser's URL bar (it's the long string of letters and numbers between /d/ and /edit).
Go to your Apps Script Project Settings (Gear Icon), scroll down to Script Properties, and add it:
-
SPREADSHEET_ID➔ (Your copied Spreadsheet ID string)
2. Updating Your Code Files
Open your workspace editor. Leave your telegram.gs and gemini.gs code completely as they are. Head over to the Gist link below and update the remaining files:
-
Create
sheet.gs: Copy the database query logic from the Gist. This handles searching through Column A to find matching records, caching data frames, and managing your execution logs. -
Update
main.gs: Overwrite your existing main function with the new optimized script block. This routes traffic dynamically—loading saved entries instantly from your Google Sheet or querying Gemini when a brand new word drops.
We are keeping our code modular and clean. All the raw files we are working with today are updated and available in the gist:
🔗 Get the Complete Updated Source Files on GitHub Gist
Make sure you deploy a New Version inside the deployment manager dropdown pane so the architecture update takes effect!
Part 2: The Invisible Production Crash (And Why Your Logs Are Missing)
Once you copy this code over and deploy a New Version, you might run into a frustrating rite of passage in the Apps Script world: The bot works fine when you click "Run" manually inside the editor, but fails silently when you message it via Telegram.
Even worse, when you check your Apps Script dashboards, the "Cloud Logs" option next to your failed production executions is grayed out or completely disabled.
What gives?
By default, every Apps Script runs on a hidden, micro-scoped background GCP project managed entirely by Google. Because you don't own that background container, Google strips away your administrative logging privileges. When an anonymous webhook executes via Telegram, it runs on low-priority infrastructure. If it runs out of processing breath, it dies mid-flight—and your cached spreadsheet writes (appendRow) vanish into thin air.
Part 3: The GCP Architecture Explained (Simply)
To make our bot bulletproof, we need to connect it to our own Standard GCP Project and configure an OAuth Consent Screen. Let's break down exactly what that means.
Think of a Google Cloud Project as a secure Digital Office Building that you own. Inside this building, different specialized departments work together.
When our bot runs inside your custom GCP project building, the flow changes completely:
-
The Front Gate (Telegram Webhook): Telegram hits your
doPostURL endpoint. - The Security Desk (OAuth Consent Screen): Because you set up an OAuth profile, your script isn't running as an anonymous internet stranger anymore. It holds a verified digital security badge. It inherits permissions seamlessly via Implicit Scopes—meaning Apps Script pre-scanned your code, realized you were touching Sheets, and automatically authorized your badge to read/write to your spreadsheet file as you.
- High-Priority Execution: Because you own the building, Google allocates a dedicated, high-priority processing runtime. Your script gets the server bandwidth it needs to safely check your sheet cache, talk to Gemini if needed, and drop the data straight into your spreadsheet grids.
- The Security Cameras (Cloud Logging Explorer): Every single print statement and error stack trace is permanently caught on camera. Because you are the building administrator, you can open the Log Explorer dashboard anytime to review production behaviors.
Step 4: Connecting Your Script to Your GCP Project
Ready to make your bot production-grade? Follow these steps:
- Head over to the Google Cloud Console and create a new project.
- Search for OAuth Consent Screen in the top bar, set User Type to External, and fill out the basic app name and your email (you can leave scopes completely blank—Apps Script's implicit scopes will handle that on deployment!).
- Go to Project Settings in the GCP dashboard and copy your Project Number.
- Return to your Apps Script editor, click the gear icon (Project Settings), click Change project, and paste your Project Number.
- While you are in your Apps Script Project Settings, make sure to check the box labeled "Log uncaught exceptions to Cloud Operations".
Deploy a New Version of your Web App one last time (Deploy > Manage Deployments > Edit > New Version > Deploy).
The Verdict
Go back to Telegram and send your bot a word you already looked up. It will reply almost instantly, stamped with appropriate marker.
Send it a completely new word, and you will watch it slide into your Vocabulary spreadsheet in real time, with an execution confirmation flag stamped cleanly in your Logs tab.
You now have an incredibly optimized, AI-powered vocabulary engine that tracks your learning database for life without wasting API credits.
Drop a comment below if your token-saving sheet integration is humming along perfectly, and happy hacking! 🚀



Top comments (0)