DEV Community

Cover image for Sending message from Telegram bot to users
Shakhzhakhan Maxudbek
Shakhzhakhan Maxudbek

Posted on • Edited on • Originally published at args.tech

1

Sending message from Telegram bot to users

Telegram provides API for sending messages to users from bot. You may send messages via HTTP POST method using any programming language. I use Python and Requests library.

The URL address for sending message (require HTTP POST method):

https://api.telegram.org/bot<token_from_botfather>/sendMessage
Enter fullscreen mode Exit fullscreen mode

Body of message:

{
    "chat_id": 123,
    "text": "Hello World!"
}
Enter fullscreen mode Exit fullscreen mode

If you want to markup your message with Markdown - add parse_mode parameter in body of JSON:

{
    "chat_id": 123,
    "text": "Hello World!",
    "parse_mode": "Markdown"
}
Enter fullscreen mode Exit fullscreen mode

Here steps needed for successfully complete the task:

  • Find BotFather in Telegram app
  • Create new bot and receive token
  • Send "/start" command to bot for start conversation. Otherwise, if you don't do this, you won't receive the messages
  • Write script and testing

Example of Python script:

import requests


def send_text_message(TOKEN, chat_id, message):
    url = 'https://api.telegram.org/bot{}/sendMessage'.format(TOKEN)
    data = {'chat_id': chat_id, 'text': message, 'parse_mode': 'Markdown'}
    response = requests.post(url, data=data)
    return response


send_text_message('token_from_botfather', recipient_id, 'Hello World!')
Enter fullscreen mode Exit fullscreen mode

As result we're see personal message from bot with greeting:

demo-telegram-bot-sending-message

Now we are trying to send the document. The URL for sending message, also receives HTTP POST method:

https://api.telegram.org/bot<token_from_botfather>/sendDocument
Enter fullscreen mode Exit fullscreen mode

Message body with required params:

{
    "chat_id": 123,
    "document": "/path/to/any/document.file",
}
Enter fullscreen mode Exit fullscreen mode

Let's try with Python code:

import requests


def send_document(TOKEN, chat_id, file):
    url = 'https://api.telegram.org/bot{}/sendDocument'.format(TOKEN)
    data = {'chat_id': chat_id}
    document = open(file, 'rb')
    files = {'document': document}
    response = requests.post(url, data=data, files=files)
    return response


send_document('token_from_botfather', recipient_id, '/path/to/any/document.file')
Enter fullscreen mode Exit fullscreen mode

Result on screenshot below display picture, attached as file:

demo-telegram-bot-sending-document

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more