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

Man Hay Hong
3 min readSep 27, 2018

--

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 for more.

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

In the code above we have a function named “telegram_bot_sendtext()” with 1 parameter “bot_message”, this is the setup. At row 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 row 5 & 6, perhaps also customize your message to send at row 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:
{"ok":true,"result":[{"update_id":77xxxxxxx,
"message":{"message_id":550,"from":{"id":34xxxxxxx,"is_bot":false,"first_name":"Man Hay","last_name":"Hong","username":"manhay212","language_code":"en-HK"}

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 the cloud server periodically, an easier approach would be to configure the script to report at 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 need help.

At row 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 popular article on Medium and I’m glad it provided at least some value.

As you might already realized from my articles, I’m quite into crypto and trading. If you are as well, I’d like to invite you to checkout Crypto Denada, a crypto trade sharing community I recently launched. It is where you can find successful traders with proven records, visualize how they trade, copy them and even chat with the trader in private.

It is totally free to get started, no PayPal needed, no credit card information. There are even traders sharing their trades for free.

On the other hand, in case you need help building a website, an App, a game or other software, I’d be happy to be in touch. At Denada Development we provide a one-stop development service. Feel free to checkout our portfolio to see what our developers are capable of, let us know what you want and we’ll make it happen!

--

--

Man Hay Hong

Finding the balance between Business and Tech. Crypto lover? Checkout: https://cryptodenada.com/. Need a website/app built? Checkout: https://denadahk.com/.