Skip to main content

Introduction

To develop and host a Telegram bot on Square Cloud, it’s essential to follow a structured sequence of configurations and prerequisites. This technical guide will cover the entire process, from initial setup to production deployment.

Prerequisites

  • Square Cloud Account: Register through the signup page using your email.
  • Active plan: Ensures dedicated resources and optimized performance for your application. Check our available plans and choose the most suitable for your needs.
This guide runs on the Node.js runtime or the Python runtime.

Create the project

A Telegram account is essential for testing and interacting with your bot. If you don’t have one, visit the official Telegram website to create your account.

Bot application on Telegram

Find BotFather

Access Telegram Web and search for the official BotFather bot, responsible for creating and managing bots on the platform.
Finding BotFather on Telegram

Get the token

Start a conversation with BotFather and type /start. Then, select the /newbot option to create a new bot. The system will ask for a name for your bot and, after confirmation, will automatically generate the necessary authentication token.
Generating Telegram bot token with BotFather
Keep your token secret: this token grants full control over the bot. Never write it in your code or put it in your ZIP: the examples below read it from the TELEGRAM_BOT_TOKEN environment variable.

Develop the bot

Write the bot with grammY (Node.js) or pyTelegramBotAPI (Python). Both examples echo every text message:
  1. Verify that Node.js is installed on your system. Otherwise, download it from the official Node.js website.
  2. Initialize a new Node.js project:
Terminal
  1. Install the grammY library:
Terminal
  1. Create the main file (index.js) with the following structure:
index.js

Configure Square Cloud

Create a squarecloud.app file at the root of your project, next to your main file. It tells Square Cloud how to run your bot:
  • MAIN is the file Square Cloud runs, and MEMORY is the bot’s RAM in MB. Bots need at least 256 MB; 512 MB leaves room to grow.
  • AUTORESTART=true starts the bot again if it crashes. See how auto restart works.
  • There is no SUBDOMAIN: with long polling, the bot needs no public URL.
See the configuration file reference for every field.

Set the bot token

Your code reads the token from the TELEGRAM_BOT_TOKEN environment variable, so the token never goes into your files. Set it in one of these ways:
  • Dashboard: on the upload page, open Advanced configuration and add a variable named TELEGRAM_BOT_TOKEN with your token as the value. For a bot that is already deployed, add it on the application’s Environment Variables page and restart the bot.
  • CLI: right after you upload the bot (see Deploy and verify), set it with squarecloud app env set and restart the bot:
The bot reads its environment variables when it starts, so restart it after every change. See Environment variables for the other ways to manage them.
To run the bot on your computer first, set the variable in your terminal: export TELEGRAM_BOT_TOKEN=your_token_here on macOS and Linux, or $env:TELEGRAM_BOT_TOKEN="your_token_here" in PowerShell.

What goes in the ZIP

  • Your main file (index.js or main.py) and your other source files.
  • package.json or requirements.txt.
  • squarecloud.app.
Leave out node_modules, .venv and __pycache__: Square Cloud installs your dependencies when the bot starts.

Deploy and verify

Create the ZIP described above, then upload it:

Via dashboard

1

Access the Upload Page

Access the upload page and upload your project zip file.
2

Configure Your Environment

After uploading your zip, you will need to configure the name, main file or runtime environment and other settings for your project.
If you are uploading a web project, make sure to select “Web Publication” and set a subdomain to your project.
3

Deploy Your Project

Finally, click on the “Deploy” button to host your project on Square Cloud.
After deployment, you can monitor your project’s status and logs from the dashboard.
Uploading application to Square Cloud
4

Confirm Your App Is Live

Your first deploy usually takes less than a minute. In the dashboard, wait for your application status to show as running and check the logs for any startup errors.
If you deployed a website or API, open https://<your-subdomain>.squareweb.app in your browser: you should see your application responding. If you deployed a bot, send it a command to confirm it is online.
App not starting? See the Troubleshooting guide for the most common causes and fixes.

Via CLI

To use this method, your project needs a configuration file named squarecloud.app at its root. It tells Square Cloud how to run your application.

Configuration file guide

Learn how to create the squarecloud.app configuration file that defines your application’s environment.
1

Install the CLI

Install the Square Cloud CLI. If you already have it, run the same command to update it:
2

Log In

Run the command below. It opens your browser: approve the login there and the CLI is ready. There is no API key to copy. For scripts and CI, see CLI authentication.
3

Upload Your Project

From your project folder, run the command below. The CLI zips the current folder, leaving out what squarecloud.ignore lists, and uploads it:
To upload a zip you created yourself, pass it with --file:
4

Confirm Your App Is Live

Your first deploy usually takes less than a minute. Check your application status and logs directly from the terminal:
If you deployed a website or API, open https://<your-subdomain>.squareweb.app in your browser: you should see your application responding. If you deployed a bot, send it a command to confirm it is online.
App not starting? See the Troubleshooting guide for the most common causes and fixes.

Test the bot

After the deploy, find your bot on Telegram and send it a message. The bot replies with the same text, which confirms it is running.
Testing the Telegram bot after deployment

Common errors

Error 409: Conflict: terminated by other getUpdates request

Another copy of the bot is reading updates with the same token, for example the one you started on your computer. Telegram delivers updates to only one long polling connection per bot. Stop the other copy, or revoke the token in BotFather if you don’t know where it runs, then set the new token and restart the bot.

Error 401: Unauthorized

The token is wrong or was revoked. Copy the current token from BotFather, set it again and restart the bot.

Empty token or KeyError: 'TELEGRAM_BOT_TOKEN'

The TELEGRAM_BOT_TOKEN variable is not set: grammY stops with Empty token!, and the Python example stops with a KeyError. Set the variable, as in Set the bot token, and restart the bot.

Next steps

App won't start

Fix missing modules, memory errors and other startup failures.

Environment variables

Manage your bot’s token and other secrets.

python-telegram-bot with aiohttp

Using python-telegram-bot? Fix frequent NetworkError exceptions.
For more about these libraries, see the official grammY documentation and the pyTelegramBotAPI page on PyPI.

Contact us

If you continue facing technical difficulties, our specialized support team is available to assist you. Contact us and we’ll be happy to help you resolve any issue: support quality is a big part of why developers rate Square Cloud 4.9/5 across 402 reviews on Google and Trustpilot.