Send and Receive Messages with the Telegram API

Will Kelly
3 min readJun 16, 2019

Telegram is a modern cross-platform messaging app that I use frequently for group messages. Since I use it everyday and it has an open API, I thought it would be a convenient interface for some small project ideas I’ve had recently.

So, I started by poking around the Telegram API . You can find it at

To register a bot, you need to talk with “TheBotfather” in the Telegram app.

You can find it here or by searching for “botfather” in the Telegram app.

In the chat with The Botfather, enter “/newbot”. It will ask you for a name then a username for your bot. Once you provide both, The Botfather will provide you with a link to your bot and an API token.

Follow the link to your bot by clicking the link that looks like{yourBotUsername}. This is where you will receive messages.

Go ahead and send a message to your bot. To prevent spam, bots cannot initiate a chat, so we need to send it a message to enable the bot to message us back. We will read this message via the Telegram API to get the chat_id in the next step.

Next, lets call the Telegram API to get our chat_id. If you have curl installed, you can read the message you just sent via the terminal with the getUpdates method:


Note: the format is /botXXXXXXXXXXXXXXXXXXXXXXXXX/, there are no backslashes or delimiters between the word bot and your API Token.

or with Python 3:

>>> import requests
>>> token = {YOUR_API_KEY_HERE}
>>> url = f'{token}/getUpdates'

It should then return a JSON payload that looks something like this:

"message":{"message_id":30,"from":{"id":{YOUR_ID},"is_bot":false,"first_name": "{YOUR_NAME}"

If you just got this, you might have forgotten to send a message to your bot:


Take the {YOUR_CHAT_ID} value, it should be around 9 numbers.

Now you can send messages in a similar fashion using the sendMessage method:

curl -d chat_id={YOUR_CHAT_ID} -d text="test msg from curl"{YOUR_API_KEY_HERE}/sendMessage

or with python3

>>> import requests
>>> token = {YOUR_API_KEY_HERE}
>>> url = f'{token}/sendMessage'
>>> data = {'chat_id': {YOUR_CHAT_ID}, 'text': 'python msg'}
>>>, data).json()

and you should get back a receipt, but more importantly you now have a message from your bot in the Telegram app!

Up next, we will run a basic Telegram bot from AWS Lambda. Check it out here: Running a Serverless Telegram Bot from AWS Lambda

Sign up for more tutorials & updates at

More sign ups ⇒ more content

Check out my personal site:

Thanks for reading.