Telegram bots have become a popular way to automate tasks and enhance user experiences. They allow us to interact with various online services, retrieve information, and perform actions through a user-friendly interface.
I’ve been using Telegram Bot platform for the last 3 years, but only as a tool to send message and not to interact with the automation. This year, due to work demands, I’ve started searching for a way of creating interactive Telegram bots without the need of running a server or paying a service to provide 24/7 availability to run my bots.
In this blog post, I’ll try to show how to leverage the power of Google Script to create and run a Telegram bot.
DISCLAIMER: I’ll not show code here, but references that I gathered that may be useful for those looking for creating bots.
The limitations
To begin with, we must let noted some limitations of using the Google Platform:
1. Google limit the use of consumer accounts to 6 minutes of execution and 30 simultaneous execution.
2. Google script works using Javascript and you won’t have ready to go libraries to make your work easy. There are some people that have been trying to provide some tools, but none of them are robust like python-telegram-bot.
3. You’ll have to use what’s available at the Google Platform as tools. Probably you’ll be using a Google Spreadsheet as your database, for example.
Now to the real stuff.
An introduction to Telegram Bots
Before diving into the technical aspects, let’s have a brief overview of Telegram bots. Bots are special Telegram accounts that are operated by software rather than humans. They can interact with users through messages, process commands, and perform actions based on user input. Telegram provides a comprehensive API that allows developers to create bots with ease and integrate them into various workflows.
To create a Telegram bot, you need to start by creating an account on Telegram and obtaining an API token. This token serves as a unique identifier for your bot and enables communication with Telegram’s API. Once you have the token, you can proceed to create a new Google Script project in Google Drive.
I won’t write the details of how to do it. It’s well documented all over the web, including Telegram website, with clear steps and images.
An introduction to Google Script
Google Script is a cloud-based scripting platform offered by Google. It enables users to write and run JavaScript code within various Google products, such as Google Sheets, Gmail, and Google Drive. The power of Google Script lies in its ability to automate tasks and interact with external APIs, making it an excellent choice for integrating Telegram bots.
How to combine both?
The interactions of the Telegram Bot and Google Script happens using Telegram Bot Webhook and Google Script WebAPI.
We must setup the Telegram bot sending the webhook address, after that, all messages sent to the bot will be processed by the Google Script. This video shows in details how to do it.
Handling User Commands
Telegram allows us to create “User Commands” to trigger specific actions. For example, a user might type “/weather London” to request the weather in London. In Google Script, you can define functions that parse incoming messages, extract commands, and execute corresponding actions. By using regular expressions or string manipulation techniques, you can extract the desired information from user messages and respond accordingly.
Data storage
Regarding storing and Retrieving Data, Google Script provides various options for data storage, such as using Google Sheets, Properties Service, or external databases. Depending on the complexity of your bot, you can choose the most suitable method for storing and retrieving data. I personally use Google Spreadsheets due to the ready to use classes that Google Script provides.
Handling coversations
For handling conversations, the best code available is the python-telegram-bot. In JavaScript, I highly recommend using Francesco Miccolis’s “Google App Script Conversational Telegram Bot” (GAS-conversational-telegram-bot).
He worked “translating” python-telegram-bot to a useful repository. It’s ready to use and it works perfectly.
My projects so far
I’ve been creating some projects using what I learned:
- A bot conversation bot that I inform how much and with what I spent my money. It’s much easier than opening my Google Spreadsheet to add a new line. Also, my partner feels more motivated to also contribute.
- A bot that saves a photo sent to it to a Google Drive folder.
- A bot that saves a photo sent to it to a Google Drive folder, edit the photo using various Google Apps and return it edited.
Conclusion
Integrating a Telegram bot with Google Script provides a powerful and flexible solution for automating tasks and interacting with users. By combining the capabilities of Google Script and Telegram’s API, you can build bots that retrieve data, perform actions, and streamline workflows seamlessly.
Ps: this post was built with the assistance of ChatGPT with a lot if modifications to fit my needs.
Tags: 2023 automations