Create a Telegram Bot to watch Bitcoin’s Volatility using Google Apps Script

Toan Nhu
Chatbots Life
Published in
6 min readAug 25, 2021

--

Cryptocurrencies are Changing the World We Live In.

A decade ago, cryptocurrency is a strange concept that not many people understand and everyone thought it was a joke. Who cares about some kind of money that is not even real and existed? At the time we talk about cryptocurrency, this market capitalization value rises to $2.06 trillion U.S. dollars by August 2021. Especially, during COVID-19 pandemic, investing in crypto seems to be a good choice for investors. But crypto is a choppy market, it’s hard to follow up the market every minute, and you need a tool to help you watch any changes in price. With Google App Script, I’ll show you how to make a Telegram Bot to watch Bitcoin’s Volatility in realtime.

What we are going to do exactly

  • Get API of prices of cryptocurrencies (BTC, ETH,…)
  • Create a Telegram Bot
  • Write an App Script code, schedule it to run periodically

1. Get API of prices of cryptocurrencies

You can use any API that can get the prices of the cryptocurrency market. But in this tutorial, I’m using CoinMarketCap API. They have a free mock API for testing and free user can register a basic plan account with allow to call maximum of 300 requests everyday for production.

CoinMarketCap Developer API

This is mock request example:

curl --location --request GET 'https://sandbox-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=BTC' \
--header 'X-CMC_PRO_API_KEY: b54bcf4d-1bca-4e8e-9a24-22ff2c3d462c' \
--header 'Accept: application/json' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'start=1' \
--data-urlencode 'limit=5000' \
--data-urlencode 'convert=USD'

After call the API, you can get the JSON below. We will extract this data to make a notification message.

{
"status": {
"timestamp": "2021-08-24T09:43:54.669Z",
"error_code": 0,
"error_message": null,
"elapsed": 1,
"credit_count": 1,
"notice": null
},
"data": {
"BTC": {
"id": 1517,
"name": "zebnlm430ma",
"symbol": "BTC",
"slug": "ip0oiwund1",
"is_active": 6184,
"is_fiat": null,
"circulating_supply": 3483,
"total_supply": 4251,
"max_supply": 2864,
"date_added": "2021-08-24T09:43:54.669Z",
"num_market_pairs": 4058,
"cmc_rank": 8412,
"last_updated": "2021-08-24T09:43:54.669Z",
"tags": [
"vba025s1i5a",
"6mtkiszjcsu"
],
"platform": null,
"quote": {
"USD": {
"price": 0.1433590851317259,
"volume_24h": 0.134253812239719,
"percent_change_1h": 0.29250982964859773,
"percent_change_24h": 0.8096078224440977,
"percent_change_7d": 0.8945916148953752,
"percent_change_30d": 0.5112814691736627,
"market_cap": 0.3431455837033053,
"market_cap_dominance": 8640,
"fully_diluted_market_cap": 0.11635441670516311,
"last_updated": "2021-08-24T09:43:54.669Z"
}
}
}
}
}

Trending Bot Articles:

1. How Conversational AI can Automate Customer Service

2. Automated vs Live Chats: What will the Future of Customer Service Look Like?

3. Chatbots As Medical Assistants In COVID-19 Pandemic

4. Chatbot Vs. Intelligent Virtual Assistant — What’s the difference & Why Care?

2. Create a Telegram Chatbot

First at all, start with creating a Telegram Bot. Navigate over to Telegram and search for the account “BotFather”. Tap the START button at the bottom of the chat screen with BotFather if you haven’t already, which launches the list of things you can do with it . Then, tap on /newbot and give your bot a name.

After giving your new bot a name, you’ll be given an access token for the HTTP API. Keep it safe and store it somewhere, you’ll need it for integration with Apps Script later.

Next, create a chat group and add a bot. You should add the bot as a member of that channel and promote it as an admin. We also need to get a chat ID of this group. For getting chat ID you can follow the instruction in Stackoverflow.

There you have a bot! Go ahead and click on the t.me link to follow your bot. Now its time for a few enhancements to make your bot functional.

3. Write Apps Script code

At last but not least, all the things we prepare up til now for just the appearance of vedette: Apps Script code to send Telegram notification about the prices of crypto. But before that what is Google App Script?

What is Google App Script?

Google Apps Script is a development platform that makes it fast and easy to create scripts and small applications that integrate with G Suite. With Apps Script, you can:

  • Write code in JavaScript and have access to built-in libraries for favorite G Suite applications like Gmail, Calendar, Drive, and more.
  • Have nothing to install — A code editor is available in your browser, and your scripts run on Google’s servers.
  • Don’t have to worry about complex topics such as security and data access permissions, since the platform handles it for you.

Apps Script can be used to create a variety of different applications, from chatbots to web apps. However one of the original and the most popular is use it to add additional functionality to a Google Sheets spreadsheet.

Apps Script services have daily quotas and limitations on some features, you can see more about this.

Now let's get the job done!

First thing, go to https://script.google.com/ and create your new Apps Script.

A default function named myFunction() is automatically created for you, and you're dropped into the editor to start coding. That's it... you're now ready to write your application!

Editing/replacing the (template) code

The “template” code you’re given is empty and doesn’t do much, so let’s replace that with our application.

The function getCryptoPrice() will get data from CoinMarketCap API and parse JSON result data before next step.

After get all the data we need, we just format this data with HTML tag message before sending to Telegram (Telegram support HTML tag and Markdown for Rich Text Message). Also there is the flag isAlert in this code because I only want to send notifications when BTC prices change in 24h is larger than 1.5%.

Now everything is ready. Let’s run the code.

Hooray!!! The code works like a charm. But wait, this job has not done yet. You have to schedule the script to run periodically. Apps Script provides Triggers to do this and you just have to do some simple setup for using.

You can find full tutorial code in here:

Hope you find the tutorial interesting!

Resources

Don’t forget to give us your 👏 !

--

--