How to create a Telegram bot, and send messages with Python

Man Hay Hong
Sep 27, 2018 · 3 min read

Telegram has amazing features, bot functionality being one of them

Following my previous article on creating a crypto trading bot, I’m using Telegram for regular trade activities reporting. It is also possible to do a whole lot other stuffs with Telegram bots, go checkout their official bot page

but for this article we’ll just keep it simple. Sending messages is as easy as running a few lines of Python codes:

This is Python3 btw

Line 3–11 is a function, named “telegram_bot_sendtext()” with 1 parameter “bot_message”, this is the setup. At line 14, the function is called, with the message to send “Testing Telegram bot” as the parameter. Learn more about Python functions

and yes, copy the above code, key in your bot_token and bot_chatID at line 5 & 6, perhaps also customize your message to send at line 14, run it, thats it

How to run a Python script?

Ok maybe a little bit more to do than that 😛 You gotta first create your bot on Telegram to have your own bot_token and bot_chatID


Creating your bot

  1. On Telegram, search @ BotFather, send him a “/start” message
  2. Send another “/newbot” message, then follow the instructions to setup a name and a username
  3. Your bot is now ready, be sure to save a backup of your API token, and correct, this API token is your bot_token
The part blurred in blue is where you will find your API token

Getting your Chat id

  1. On Telegram, search your bot (by the username you just created), press the “Start” button or send a “/start” message
  2. Open a new tab with your browser, enter https://api.telegram.org/bot<yourtoken>/getUpdates , replace <yourtoken> with your API token, press enter and you should see something like this:

Look for “id”, for instance, 34xxxxxxx above is my chat id. Look for yours and put it as your bot_chatID in the code above

Now you are all set, run the code, and enjoy receiving messages from yourself :)


Sending messages regularly

If you indeed created a trading script, and hosted it on a cloud to have it running 24/7, you might want to regularly keep an eye on how it is performing. Instead of connecting to your cloud server periodically, an easier approach would be to configure your script to report to you with your desired frequency

Reporting your account balance everyday at noon

Here we are making use of a library called “schedule”, you would first need to install the library by running pip3 install schedule. Check here if you don’t know how to do it

At line 24 we are scheduling the script to call the “report()” function everyday at noon, inside the function we are fetching the latest account balance, putting it into a customized “my_message” variable, and sending the message out via Telegram

Checkout the official “schedule” library page to see what else you can do with it, and learn more about message formatting

Now you have a personal assistant that reports to you everyday :)


Nov 2019 updates:

Hey, thanks for reading, this is by far my most viewed article on Medium and I’m glad it provided some value, plus sparked interest in some of you

In case you are interested in crypto / trading / earning passive income, I just want to share with you one more thing that you might also be interested in

In July this year I have launched Crypto Denada — a crypto trade sharing community, a platform where traders meet

What can you do with it?

  1. Be a Leader, share your portfolio with just a few clicks, and get paid when others follow you
  2. Be a Follower, follow crazy gainers/whales, monitor and copy how they trade
  3. Be both, or be none. Simply complete the tasks, and get paid immediately

There is also a blog where trading insights will be shared from time to time, be sure to stay tuned!

It’s free to create an account and to share a portfolio, there’s even a reward for doing so during promotion period! Go ahead and check it out, and see if its your thing :)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade