A decade ago, cryptocurrency is a strange concept that not many people understand and people 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 seem 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.
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.
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'
After call the API, you can get the JSON below. We will extract this data to make a notification message.
Trending Bot Articles:
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:
- 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!
Don’t forget to give us your 👏 !
Create a Telegram Bot to watch Bitcoin’s Volatility using Google Apps Script was originally published in Chatbots Life on Medium, where people are continuing the conversation by highlighting and responding to this story.