How to create a Telegram bot, and send messages with Python
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:
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_chatID at row 5 & 6, perhaps also customize your message to send at row 14, run it, thats it.
Ok maybe a little bit more to do than that 😛 You gotta first create your bot on Telegram to have your own
Creating your bot
- On Telegram, search @ BotFather, send him a “/start” message
- Send another “/newbot” message, then follow the instructions to setup a name and a username
- Your bot is now ready, be sure to save a backup of your API token, and correct, this API token is your
Getting your Chat id
- On Telegram, search your bot (by the username you just created), press the “Start” button or send a “/start” message
- Open a new tab with your browser, enter
<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 the cloud server periodically, an easier approach would be to configure the script to report at your desired frequency:
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.
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!