TradingView Chart Telegram Bot (Part2)

CHART-IMG
4 min readApr 11, 2023

--

In this second part of the tutorial, I will help you set up running a Serverless Telegram Bot using Cloudflare Workers. You will need Cloudflare Account and the credential to log in using Wrangler to publish your Serverless Worker. As this is a continuation of part 1, I recommend reading it first.

Cloudflare Workers

Cloudflare Workers is a Serverless computing platform that enables developers to build scalable applications by running their code on Cloudflare’s global network. You do not have to manage your server, and it is free for up to 100,000 requests per day, which is more than enough for this project.

Wrangler

Wrangler is a command-line tool that simplifies the process of building, previewing, and publishing Serverless applications on the Cloudflare Workers platform, which I will use to deploy Telegram Server.

Once you have cloned the project repository, installed the necessary dependencies, customized the config.json, and modified the wrangler.toml with your credentials as mentioned in part 1, you can log in to Wrangler using your Cloudflare Account.

npx wrangler login

Click Allow to grant access to your Cloudflare Account.

GRANTED AUTORIZATION TO WRANGLER

Now you are ready to deploy your Serverless Telegram Bot.

Deploy Worker

Now that you have successfully logged in to Cloudflare with Wrangler and completed all the necessary steps, let’s deploy your Serverless Telegram Bot.

npx wrangler deploy
> chart-img-telegram-bot@1.0.3 publish-server-cf
> npx wrangler deploy

⛅️ wrangler 3.3.0
------------------
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..."
Total Upload: 92.42 KiB / gzip: 15.80 KiB
Uploaded chart-img-telegram-bot (0.81 sec)
Published chart-img-telegram-bot (0.20 sec)
https://chart-img-telegram-bot.<WORKER_SUB_DOMAIN>.workers.dev
Current Deployment ID: 8faf02f1-2219-474b-aa15-eaeaacf662dc

If you have not registered a Workers domain name, Wrangler will prompt you to register a workers.dev subdomain. It must be unique and not yet taken. <WORKER_SUB_DOMAIN> will be replaced with your worker’s subdomain name.

Setup Telegram Webhook

After you publish your Cloudflare Worker, you need to set up Telegram Webhook with the published worker’s URL.

npm run setup-telegram
> chart-img-telegram-bot@1.0.1 setup-telegram
> node --no-warnings src/setup/telegram

? Enter the server https base URL : https://chart-img-telegram-bot.<WORKER_SUB_DOMAIN>.workers.dev

Successfully setup Telegram Webhook!

Enter your published URL so Telegram Server can post the payload to your Cloudflare Worker.

You should now have a working Serverless Telegram Bot.

Encrypt Variables (Optional)

For added security, it is best to encrypt your variables in Cloudflare Worker Settings or by using Wrangler command. If you want to encrypt variables using Wrangler, remove variables from file wrangler.toml, or you will get a global variable already set error message.

name = "chart-img-telegram-bot"

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


[vars]

Let’s assign necessary secret variables to your worker using Wrangler.

npx wrangler secret put CHART_IMG_API_KEY
 ⛅️ wrangler 3.3.0
------------------
✔ Enter a secret value: … ****************************************
🌀 Creating the secret for the Worker "chart-img-telegram-bot"
✨ Success! Uploaded secret CHART_IMG_API_KEY
npx wrangler secret put TELEGRAM_API_TOKEN
 ⛅️ wrangler 3.3.0
------------------
✔ Enter a secret value: … ****************************************
🌀 Creating the secret for the Worker "chart-img-telegram-bot"
✨ Success! Uploaded secret TELEGRAM_API_TOKEN
npx wrangler secret put TELEGRAM_SECRET_TOKEN
 ⛅️ wrangler 3.3.0
------------------
✔ Enter a secret value: … ****************************************
🌀 Creating the secret for the Worker "chart-img-telegram-bot"
✨ Success! Uploaded secret TELEGRAM_API_TOKEN

You can check encrypted variables in Cloudflare worker’s settings > variables.

Real-time Logs (Optional)

To access Cloudflare Worker’s real-time logs, navigate to the Worker dashboard > Logs > Real-time Logs and click Begin log stream.

Alternatively, you can check the real-time logs of Cloudflare Worker using Wrangler.

npx wrangler tail --format pretty
 ⛅️ wrangler 3.3.0
------------------
Successfully created tail, expires at 2023-04-11T02:39:15Z
Connected to chart-img-telegram-bot, waiting for logs...
POST https://chart-img-telegram-bot.<WORKER_SUB_DOMAIN>.workers.dev/webhook - Ok @ 2023-04-10, 8:39:23 p.m.
(info) :: info :: private message from <TELEGRAM_NAME>(<TELEGRAM_FROM_ID>) - /start
POST https://chart-img-telegram-bot.<WORKER_SUB_DOMAIN>.workers.dev/webhook - Ok @ 2023-04-10, 8:39:26 p.m.
(info) :: info :: private message from <TELEGRAM_NAME>(<TELEGRAM_FROM_ID>) - /crypto
POST https://chart-img-telegram-bot.<WORKER_SUB_DOMAIN>.workers.dev/webhook - Ok @ 2023-04-10, 8:39:32 p.m.
(info) :: info :: private callback_query from <TELEGRAM_NAME>(<TELEGRAM_FROM_ID>)

To whitelist specific users and allow them access to your bot, you may need to access the logs to get <TELEGRAM_FROM_ID>.

For detailed instructions on how to customizing your Telegram Bot, refer to the configuration section in the project’s README.md.

Conclusion

Congratulations on successfully setting up your Telegram Bot using various methods, including Docker, Local Server, and Serverless with Cloudflare Workers. I hope this tutorial was helpful and provided valuable insights.

If you encounter any difficulties setting up or have any questions, you can contact me via email at support@chart-img.com or leave a comment.

Happy Charting!

CHART-IMG

--

--

No responses yet