Sending Messages to Users through a Telegram Bot

First, create a Telegram bot. The official verified bot for creating and configuring bots is @BotFather. Open it in Telegram and send this command:

/newbot 

Choose a name and username for your bot. Everything happens directly in the Telegram chat.

Once the bot has been created and configured, BotFather sends you its address and token. Save the token: you need it to make API requests.

Registering users with the bot

Next, each user who should receive messages must open the bot and start a conversation, for example:

 /start

Getting the list of registered users

Request the list of users who have interacted with the bot by opening this URL in a browser:

https://api.telegram.org/botTOKEN/getUpdates

Here, TOKEN is the bot token you received when registering it.

{"ok":true,"result":[
{"update_id":123456789, "message":{"message_id":5,"from":{"id":111111111,"is_bot":false,"first_name":"","last_name":"","username":"you_username","language_code":"ru"},"chat":{"id":222222222,"first_name":"","last_name":"","username":"you_username","type":"private"},"date":1665073131,"text":"/start","entities":[{"offset":0,"length":6,"type":"bot_command"}]}},
{"update_id":754465035,
"message":{"message_id":6,"from":{"id":111111111,"is_bot":false,"first_name":"","last_name":"","username":"you_username","language_code":"ru"},"chat":{"id":222222222,"first_name":"","last_name":"","username":"you_username","type":"private"},"date":1665073268,"text":"/help","entities":[{"offset":0,"length":5,"type":"bot_command"}
]}}]}

The JSON response contains the users, their IDs and usernames. That completes the preparation. 

Sending messages

Use this URL to send a message:

https://api.telegram.org/botTOKEN/sendMessage?chat_id=BOTID&text=TEXT

Here, TOKEN is your bot's token, BOTID is the recipient's user ID, and TEXT is the message itself. 

The message has been sent to the user on behalf of the Telegram bot.