Build the Telegram Plant Identifier Bot (Node.js + Plant.id + Google Sheets)

By the end of this tutorial, you’ll have a functioning Telegram bot.

AI

Alesia

9/13/20255 min read

Have you ever been on a walk, spotted an interesting plant, and wondered what it was? In this tutorial, I’ll show you how to build a practical Telegram bot that can identify plants from photos. The bot leverages the power of the Plant.id API for accurate plant recognition and can save the results to Google Sheets for your personal plant collection.

By the end of this tutorial, you’ll have a functioning Telegram bot that:

  • Accepts plant photos from users

  • Processes images using Plant.id’s recognition API

  • Returns plant names, confidence scores, and descriptions

  • Offers users the option to save results to Google Sheets

What We’ll Build: The User Flow

Here’s how our finished bot will work:

  1. User starts a conversation with the bot using the /start command

  2. Bot welcomes the user and prompts them to upload a plant photo

  3. User sends a photo of a plant they want to identify

  4. Bot processes the image through the Plant.id API

  5. Bot returns the identified plant name, confidence score, and description

  6. User sees two buttons: “Save to Google Sheets” or “No thanks”

  7. If “Save” is selected, data is stored in a Google Sheet for future reference

Technologies We’ll Use

Our bot will be powered by:

  • Node.js as our backend runtime

  • Node Telegram Bot API to interface with Telegram

  • Plant.id API for plant recognition

  • Google Sheets API to store plant information

  • Environment variables to securely manage API keys

Prerequisites

Before we start coding, make sure you have:

  • Basic knowledge of JavaScript and Node.js

  • Node.js installed on your computer

  • A code editor (I’ll be using VS Code)

  • A Telegram account

Step 1: Setting Up Your Telegram Bot

First, we need to create a Telegram bot using BotFather:

  1. Open Telegram and search for “BotFather” (the official Telegram bot for creating and managing bots)

  2. Send the /start command

  3. Send the /newbot command and follow the instructions:

  • Provide a display name for your bot (e.g., “Plant Identifier”)

  • Choose a username ending with “bot” (e.g., “plant_identifier_bot”)

  1. BotFather will generate a unique API token — keep this secure as it grants full control of your bot

Step 2: Setting Up Your Node.js Project

Let’s set up our project structure:

For our project structure, we’ll need:

  • A main directory for our bot project

  • A .env file for storing API keys and tokens

  • A .gitignore file to avoid committing sensitive data

  • An index.js file as the main entry point

  • A utils folder with helper modules:

  • plant.js for Plant.id API integration

  • googleSheets.js for Google Sheets API integration

You’ll also need to install the necessary dependencies including node-telegram-bot-api, axios, dotenv, and the Google APIs packages. Remember to update your package.json to use ES modules by adding “type”:

“module”.

This enables modern ES module syntax with import/export statements.

Step 3: Obtaining API Keys

Plant.id API Key

  1. Go to plant.id

  2. Click on “Get API Key”

  3. Sign up or log in

  4. Navigate to your dashboard and click on “API Keys”

  5. Copy your API key

Google Cloud Setup for Sheets API

  1. Go to Google Cloud Console

  2. Create a new project

  3. Enable the Google Sheets API:

  • Navigate to “APIs & Services” > “Library”

  • Search for “Google Sheets API”

  • Click “Enable”

Create a service account:

  • Go to “IAM & Admin” > “Service Accounts”

  • Click “Create Service Account”

  • Name your service account and click “Done”

Generate a private key:

  • Click on your newly created service account

  • Go to the “Keys” tab

  • Click “Add Key” > “Create new key”

  • Choose JSON format

  • This will download a JSON file containing your credentials

Create a Google Sheet:

  • Go to Google Sheets

  • Create a new sheet

  • Copy the ID from the URL (the long string between “/d/” and “/edit”)

  • Share the sheet with your service account’s email address (with Editor permission)

Step 4: Setting Up Environment Variables

Create a .env file in your project root and add your API keys:

In your .env file, you'll need to include:

  • TELEGRAM_TOKEN

  • PLANT_ID_API_KEY

  • GOOGLE_SHEET_ID

  • GOOGLE_CLIENT_EMAIL

  • GOOGLE_PRIVATE_KEY

Also, create a .gitignore file to exclude your node_modules directory and .env file to keep your sensitive information secure.

Step 5: Building the Plant Identification Module

For our plant.js utility module, we'll create a function that handles plant identification. This function needs to:

  1. Download the image from the Telegram URL

  2. Convert the image to base64 format

  3. Send the base64 image to the Plant.id API

  4. Process the response and extract useful information

The key elements of our plant identification function include:

  • Using axios to fetch the image from Telegram

  • Converting the image data to base64 format

  • Sending a POST request to the Plant.id API with our API key

  • Extracting the top match from the results

  • Checking the confidence score (rejecting results below 50% confidence)

  • Formatting the results with plant name, confidence, description, and source

Step 6: Setting Up Google Sheets Integration

For our Google Sheets integration, we’ll create a module that:

  1. Authenticates with Google using our service account credentials

  2. Creates a Google Sheets client

  3. Implements a function to append plant data to our spreadsheet

The key components include:

  • Using JWT from Google Auth library to create an authenticated session

  • Setting up the proper scopes for Google Sheets API access

  • Creating a function that extracts description and source information

  • Formatting the data with a timestamp

  • Appending the data as a new row in our Google Sheet

Step 7: Building the Main Bot Logic

Our main bot logic in index.js will handle:

  1. Initializing the Telegram bot with our token

  2. Creating a session store to track identification results

  3. Setting up command handlers

  4. Processing incoming photos

  5. Managing user interactions with buttons

Here’s what our bot will do:

  • Respond to the /start command with a welcome message

  • Process incoming photos by getting the file path from Telegram

  • Send the image to our Plant.id integration

  • Format and display the identification results

  • Provide inline buttons for saving to Google Sheets

  • Handle button clicks to either save or discard the information

  • Manage user sessions to keep track of data between messages

One important design note: We’ll use a Map object to store session data, which allows us to track plant identification results for each user separately. This prevents confusion when multiple users are interacting with the bot simultaneously.

Step 8: Running Your Bot

Once you’ve set up all the components, running your bot is straightforward:

  1. Make sure all your environment variables are properly configured

  2. Run your bot with Node.js

  3. You should see a confirmation message that your bot is running

When everything is set up correctly, your bot will be accessible in Telegram. Find it by searching for the username you specified when creating it with BotFather.

Testing Your Bot

Let’s walk through a complete test:

  1. Send the /start command to your bot

  2. The bot should welcome you and ask for a plant photo

  3. Send a clear photo of a plant

  4. Wait for the bot to process the image (this may take a few seconds)

  5. The bot should respond with the plant name, confidence score, and description

  6. Choose to save or discard the information

  7. If saved, the data should appear in your Google Sheet

Troubleshooting Common Issues

  • Bot not responding: Check if your TELEGRAM_TOKEN is correct and if your bot is running

  • Plant identification fails: Ensure you’re sending clear photos and your PLANT_ID_API_KEY is valid

  • Google Sheets errors: Verify your service account has edit access to the sheet and the credentials are correct

  • Parse mode errors: If you’re getting Markdown formatting errors, check for special characters in plant descriptions

Extending Your Bot

Here are some ways you could enhance your bot:

  • Add more details about identified plants (taxonomy, care tips)

  • Implement rate limiting to avoid API quota issues

  • Add multi-language support

  • Create a daily usage limit to manage costs

  • Store user preferences in a database

Conclusion

Congratulations! You’ve built a practical Telegram bot that can identify plants from photos and save the data to Google Sheets. This project demonstrates how to integrate multiple APIs into a cohesive application that provides real value to users.

The skills you’ve learned here — working with the Telegram Bot API, external APIs for image recognition, and Google’s services — can be applied to numerous other projects. Maybe your next bot will identify birds, landmarks, or even help users track their daily habits!

Feel free to share your creations or modifications in the comments below. Happy coding! 🌱