Building a Telegram Bot with Google Script: Unlocking Automation Potential

On June 22nd, 2023 in Tech
Trying to make a cute robot illustration using Stable Diffusion

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:

  1. 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.
  2. A bot that saves a photo sent to it to a Google Drive folder.
  3. 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

Sous vide Tonkatsu

On May 23rd, 2023 in Food

One of my favorite dishes while living in Japan was Tonkatsu. That perfect cooked pork meat fried with panko and a dash of that sweet sour salt sauce was one of those dishes to be eaten at home on a not very regular day, or on weekends at tonkatsu restaurants.

If you’re looking for a good recipe, I recommend Namiko Hirasawa’s Just One Cookbook Tonkatsu Recipe. Her recipe is simple, in english and works very well. If you know japanese, you can always search for variations at Cookpad, but Namiko keeps her recipe classic.

By the way, when I mean “classic”, I’m talking about the most common recipe you find at restaurant chains in Tokyo where they cook the 豚ロース (pork roast) of the 豚ヒレ (pork fillet). Pork roast would be the pork loin in US or the “lombo” in Brazil. A quite lean and tender meat that, if not cooked properly, can become too dry.

The cut is also quite thick, something like almost 1 inch (like on the photo above). Others regions in Japan have their tonkatsu made different. I’m not exactly sure where, but I think it was in Hiroshima where I ate a tonkatsu that was thin and small.

That’s where the sous vide part begins. I always cooked tonkatsu using a recipe similar to Namiko’s, but sometimes my pork was too rare at the end needing the microwave to finish it. I was always worried to overcook and have an unappetising dry meat.

Cooking the meat using sous vide solves all those problems because you have a already cooked product that only needs to be deep fried and served. For safety, I cooked my pork at 62º C (about 145 F) for 1.5 hour for pasteurization.

For better texture and for a perfect salty meat, I also brine the meat. If you don’t know what is that and what are the advantages, I suggest taking a look at this video from ChefStep.

Instructions

  • Buy a pork loin. If needed, trim the excess fat.
  • Brine the pork loin for 12 hours in a solution of 2% of salt (weight the meat with the water and add 2% of it’s weight in salt). Keep it in the refrigerator.
  • Remove the pork loin from the brine solution, sprinkle black pepper and add it to the ziplock bag.
  • Cook it at 62/63ºC (145F) for 1 hour and 30 minutes.
  • Remove it from the heat and leave at room temperature (if you are planning to cook much later, maybe it’s better to put the cooked meat at the refrigerator).
  • When ready to cook, remove the meat from the bag, remove the excess liquid, cut the desired slices and use them as a regular loin for tonkatsu. DO NOT add salt to the meat because it’s already seasoned.

The difference from Namiko’s recipe for mine are:

  • You shall not season the meat with salt, because it’s already seasoned (but you can season with black pepper)
  • You may fry the pork at 180ºC (360ºF) instead of 170ºC (340ºF). You only need to fry the crust, the meat is already cooked.

And that’s all. You’ll have a perfectly seasoned cooked tonkatsu with a crispy exterior.

For the sauce, I also recommend Namiko’s recipe for Tonkatsu Sauce. I actually prefer her recipe instead of the Bulldog store bought.

Bonus: some tonkatsu restaurant’s recommendations

For those going to Tokyo, I do recommend those restaurants:

  • Tonkatsu Marugo (とんかつ 丸五(まるご)): it’s located near to Akihabara Station. It’s a Michelin Bib Gourmand. The restaurant is small, you MUST arrive before opening to avoid waiting for over an hour.
  • Yamabe Okachimachi (とんかつ 山家(やまべ)御徒町店): it’s located near to Ueno Station. The restaurant is small and delicious. I do recommend arriving before opening to avoid waiting too long.
  • Tonkatsu Maisen restaurants: there are a lot of Maisen’s restaurants in Tokyo. It’s very possible that you find one in your way. It’s delicious and rice, cabbage and miso soup are refill.
Tags: 2023 tonkatsu