Telegram has some drawbacks, and some amazing features, being one of them, the bot function. It is something with a lot of power that allows us to receive messages using it.

So you basically create a Telegram Bot, then after some configurations and preparations, we can use it to send messages to anyone or any group, as long as we know the chat id between that person and out bot, or if we add the bot to a group. I hope you are starting to see the power that this provides.

What we will do here is write a Python script that will take an argument, in fact the text message we want to send, and send it to our bot. Then again, if the bot belongs to a group, all the members of the group will receive it.

Let’s create our bot

On Telegram, we search for @BotFather, and we send this contact a /start message. He will reply something similar to:

I can help you create and manage Telegram bots. If you're new to the Bot API, please [see the manual](https://core.telegram.org/bots).

  
 You can control me by sending these commands:

  
 /newbot - create a new bot  
 /mybots - edit your bots **[beta]**

  
 Edit Bots
 /setname - change a bot's name  
 /setdescription - change bot description  
 /setabouttext - change bot about info  
 /setuserpic - change bot profile photo  
 /setcommands - change the list of commands  
 /deletebot - delete a bot

  
 Bot Settings
 /token - generate authorization token  
 /revoke - revoke bot access token  
 /setinline - toggle inline mode (https://core.telegram.org/bots/inline)  
 /setinlinegeo - toggle inline location requests (https://core.telegram.org/bots/inline#location-based-results)  
 /setinlinefeedback - change inline feedback (https://core.telegram.org/bots/inline#collecting-feedback) settings  
 /setjoingroups - can your bot be added to groups?  
 /setprivacy - toggle privacy mode (https://core.telegram.org/bots#privacy-mode) in groups

  
 Games
 /mygames - edit your games (https://core.telegram.org/bots/games) [beta]  
 /newgame - create a new game (https://core.telegram.org/bots/games)  
 /listgames - get a list of your games  
 /editgame - edit a game  
 /deletegame - delete an existing game

Now we reply with the /newbot and we get:

Alright, a new bot. How are we going to call it? Please choose a name for your bot.

So we type in the name we want it to have. BotFather replies:

Good. Now let's choose a username for your bot. It must end in 'bot'. Like this, for example: TetrisBot or tetris_bot.

Again we follow the instructions and type in the bot username.

Done! Congratulations on your new bot. You will find it at t.me/<your_bot_username>. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you've finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.

Use this token to access the HTTP API:  
<here_we_have_the_token_we_need>  
Keep your token secure and store it safely, it can be used by anyone to control your bot.  
For a description of the Bot API, see this page: https://core.telegram.org/bots/api

We’re done with the bot. Now we need the ChatID. So we have to:

  • Add the bot to the group we want to receive the messages.
  • Send him a dummy message from the Telegram group using this format: dummy_text @my_bot
  • Go to the following URL:

https://api.telegram.org/bot<here_goes_our_bot_token>/getUpdates

And we will get a JSON file containing all the updates that impacted our bot somehow, so we will get the ChatID of the group to which we added our bot. This comes as part of the JSON dataset that belongs to the update the bot received when we sent him the dummy message.

We’re done with the bot and Telegram now. Let’s go with the second part.

Create the Python script

Now that we have all the data we need, the bot token and the bot chatID, we can proceed to write down our script.

import sys, getopt, argparse, requests
def send_text_to_bot(message):
    token = ''
    chatID = ''
    sendtext = 'https://api.telegram.org/bot' + token + '/sendMessage?chat_id=' + chatID + '&parseMode=Markdown&text=' + message
    response = requests.get(sendtext)
    return response.json()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("-m", "--message")
    args = argparse.parse_args()

    if (args.message) is not None:
        send_text_to_bot(args.message)

python push_message.py -m 'Hello from the bot side of Telegram'

The script is ready, we need to call it from the shell like so:

All in all we use a script to call the Telegram API, send our bot a message, that in the end he will forward to the ChatID were it belongs. As simple as that.

I added the script to my GitLab repo in case you want to play around with it. Find it in the following link:

https://gitlab.networkbits.es/acastro/python-telegram-bot-script