TradingView Chart Telegram Bot (Part1)

CHART-IMG
5 min readMar 31, 2023

--

In this tutorial, I will help you set up, configure and run the TradingView Chart Telegram Bot based on CHART-IMG API using Docker, Local Server with NodeJS NPM, and Serverless Cloudflare Workers. By the end of this tutorial, you should be able to run your Telegram Bot with customized preset indicators.

Before we continue, I recommend reviewing the project on GitHub and
the CHART-IMG API documentation.

The upcoming update to the CHART-IMG website will allow users to run a fully managed Telegram Bot Server with customized configuration.

Live Telegram Bot

https://t.me/chartImgBot

TELEGRAM BOT WELCOME MESSAGE

Requirement

To set up the server, you will need the following requirements:

  • Telegram Bot API Token
  • CHART-IMG API Access Key
  • NGROK Auth Token if you want to run your server
  • Cloudflare Account if you want to use Serverless

Telegram Bot API Token

The first step in building a Telegram bot is to chat with the Telegram Bot Father. To create a new bot, use the /newbot command and choose a name and username. The Bot Father will provide a token for the authentication.

CHART-IMG API Access Key

To obtain your access key, log in to chart-img.com using your Google account and generate it. Please note that without the subscription, the chart will have a watermark and a maximum resolution of 800x600, which should be enough for this project.

NGROK Auth Token (Deploy option #1)

To run your server, you need a secure proxy tunnel to forward the Telegram webhook payload to your local server. I recommend using NGROK. You can create an account at ngrok.com and get a free Auth Token. The free version has restricted bandwidth and usage. Please refer to their price plan for more detail.

Cloudflare Account (Deploy option #2)

Cloudflare Workers is a Serverless platform that enables instant code deployment globally, ensuring exceptional performance, reliability, and scalability. You don’t have to manage your server, and it’s free for up to 100,000 requests per day, which is more than enough. Create an account at cloudflare.com.

Clone the project repository

Let’s download the project source code to your computer. If you have git installed, run the following command:

git clone https://github.com/hawooni/chart-img-telegram-bot.git

If you do not have git installed, you can download the ZIP file from the GitHub repository.

After the git clone command or unzipping the file, go to the root directory of the project by entering the following command:

cd chart-img-telegram-bot

Configuration

config.json

This file is a vital component of the project, as it allows for customization of the preset inputs and messages for the Telegram Bot.

{
"version": 1,
"/chart": {
"intervals": ["5m", "15m", "1h", "4h", "1D", "1W"],
"default": {
"symbol": "CAPITALCOM:US100",
"interval": "1D",
"theme": "dark"
}
},
"commands": [
{
"command": "/start",
"description": "Introduction"
},
{
"command": "/chart",
"description": "TradingView <exchange:symbol> <interval>"
},
{
"command": "/example",
"description": "Command Examples"
}
],
"messages": [
{
"name": "/start",
"text": "Hello, Welcome message..."
},
{
"name": "/example",
"text": "List of examples..."
}
],
"private": {
"enabled": true,
"allowFromIds": [12345678, 12345679]
},
"group": {
"enabled": false
}
}

To better understand the differences between API versions, I suggest reviewing the API documentation for both version 1 and version 2 of the TradingView Advanced Chart.

Additionally, look at the config1.json and config2.json files in the examples folder. These files contain the same indicator inputs but are for each API version. The project documentation outlines specific configuration requirements that you have to follow.

wrangler.toml

This file is an important part of the project as it stores the credentials necessary to access the required APIs.

Replace the variables with the previous requirements mentioned earlier.

name = "chart-img-telegram-bot"

main = "src/index.js"
compatibility_date = "2022-11-30"


[vars]
NGROK_TOKEN = "YOUR_NGROK_TOKEN_IF_NEEDED"
CHART_IMG_API_KEY = "YOUR_CHART_IMG_API_KEY"
TELEGRAM_API_TOKEN = "YOUR_TELEGRAM_API_TOKEN"
TELEGRAM_SECRET_TOKEN = "somethingRandomHere"

TELEGRAM_SECRET_TOKEN is to make sure the webhook payload is from the Telegram Server. Replace the variable with a random string that only you know. NGROK_TOKEN is only necessary if you intend to run your local server without your own proxy to forward the webhook payload.

Deploy

Make sure to have two files wrangler.toml and config.json ready with the necessary credentials and configurations.

Deploy with Docker Image

If you have Docker installed, this would be the simplest way to run the server with just two files.

Replace <PATH> with the absolute file path for each wrangler.toml and config.json.

docker run --restart=always --name telegram-bot -v <PATH>/wrangler.toml:/chart-img-telegram-bot/wrangler.toml -v <PATH>/config.json:chart-img-telegram-bot/config.json -d hawooni/chart-img-telegram-bot:latest

Example if you use Linux:

docker run --restart=always --name telegram-bot \
-v $(pwd)/wrangler.toml:/chart-img-telegram-bot/wrangler.toml \
-v $(pwd)/config.json:/chart-img-telegram-bot/config.json \
-d hawooni/chart-img-telegram-bot:latest

Deploy with NodeJS & NPM

Ensure you have the latest NPM and NodeJS, preferably using a Node version manager like Volta or NVM to avoid permission issues and easily change NodeJS versions. You can download the latest NodeJS at nodejs.org.

Let’s install the necessary packages before we can run the server.

npm install

Run the server locally with port 8080.

npm run server-local -- --ip 127.0.0.1 --port 8080
> chart-img-telegram-bot@1.0.3 server-local
> npx wrangler dev --ip 127.0.0.1 --port 8080

⛅️ wrangler 3.3.0
------------------
wrangler dev now uses local mode by default, powered by 🔥 Miniflare and 👷 workerd.
To run an edge preview session for your Worker, use wrangler dev --remote
Your worker has access to the following bindings:
- Vars:
- NGROK_TOKEN: "HIDDEN..."
- CHART_IMG_API_KEY: "HIDDEN..."
- TELEGRAM_API_TOKEN: "HIDDEN..."
- TELEGRAM_SECRET_TOKEN: "HIDDEN..."
⎔ Starting a local server...
[mf:inf] Ready on http://127.0.0.1:8080/
╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ [b] open a browser, [d] open Devtools, [l] turn off local mode, [c] clear console, [x] to exit │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

Although you have started the server on your machine, it cannot receive the webhook sent by Telegram. You need NGROK or a secured proxy to forward the payload to your local server.

npm run ngrok -- --port 8080 -t
> chart-img-telegram-bot@1.0.3 ngrok
> node --no-warnings src/setup/ngrok --port 8080 -t

ngrok: connected
------------------------------------------------------
https://2360-104-255-13-171.ngrok-free.app => 127.0.0.1:8080
------------------------------------------------------

This will run the NGROK proxy tunnel to forward the payload after updating the Telegram Webhook URL.

Your local server should be able to receive incoming messages and respond to your command with customized input indicators. If you encounter any issues or have questions, please don’t hesitate to share them in the comments section below.

In Part 2, I will guide you through deploying serverless using Cloudflare Workers.

--

--

No responses yet