Skip to main content

A very simple Python package to send and receive Telegram messages

Project description

telegram-easy

PyPI version License: MIT Python 3.8+

A minimal, lightweight Python package to send and receive Telegram Bot messages, with both synchronous and asynchronous APIs.

Designed for simple automation and monitoring use cases where only basic Telegram Bot interactions are required.

For more advanced features, see the official Telegram Bot Python examples.

Features

  • Send text messages to Telegram bots.
  • Receive update messages.
  • Synchronous and asynchronous APIs.
  • Environment variable support for credentials.
  • Command Line Interface (CLI).
  • No external Telegram frameworks required.

Installation

pip install telegram-easy

Python version

  • Python 3.8+

Configuration

Create a Telegram bot using BotFather and save your Bot Token.

Avoid hardcoding credentials. You can configure the package using environment variables:

export TELEGRAM_TOKEN="your_bot_token"
export TELEGRAM_CHAT_ID="your_chat_id"

If token or chat_id parameters are not explicitly provided, the package will try to read them from these variables.

Receive messages

Synchronous using environment variables (Recommended)

from telegram_easy import get_updates

updates = get_updates() 

Synchronous with no environment variables (Not recommended)

from telegram_easy import get_updates

updates = get_updates(token="xxx")
print(updates)

Asynchronous using environment variables (Recommended)

import asyncio
from telegram_easy.aio import get_updates

updates = asyncio.run(get_updates())
print(updates)

Asynchronous with no environment variables (Not recommended)

import asyncio
from telegram_easy.aio import get_updates

updates = asyncio.run(get_updates(token="xxx"))
print(updates)

CLI using environment variables (Recommended)

telegram-easy get_updates

CLI with no environment variables (Not recommended)

telegram-easy get_updates --token XXX

Get the Chat ID

To get the chat_id id you have different methods.

The easiest method if you have access to the chat is to open the chat in Telegram Web and look in the URL:

https://web.telegram.org/a/#1234567890

In this case the chat_id is 1234567890.

If you don't have access to the chat or if the chat url does not have a number, you can use the following method. Send (or request the owner to send) manually a message to your bot from the Telegram App or Web, then run:

from telegram_easy import get_updates

updates = get_updates()
for update in updates.get("result", []):
    chat_id = update["message"]["chat"]["id"]
    user_name = update["message"]["from"].get("first_name", "Unknown")
    print(f"User: {user_name}, Chat ID: {chat_id}")

Save that CHAT_ID to use it when sending messages to the bot.

Send a text message

Synchronous using environment variables (Recommended)

from telegram_easy import send_text_message

send_text_message(message="Hello World!")

Synchronous with no environment variables (Not recommended)

from telegram_easy import send_text_message

send_text_message(message="Hello World!", token="123",chat_id="1234")

Asynchronous using environment variables (Recommended)

import asyncio
from telegram_easy.aio import send_text_message

asyncio.run(send_text_message(message="Hello World!"))

Asynchronous with no environment variables (Not recommended)

import asyncio
from telegram_easy.aio import send_text_message

asyncio.run(send_text_message(message="Hello World!", token="123",chat_id="1234"))

CLI using environment variables (Recommended)

telegram-easy send_message "hello world"

CLI with no environment variables (Not recommended)

telegram-easy send_message "hello world" --token XXX --client_id XXX

Next steps

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

telegram_easy-0.2.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

telegram_easy-0.2.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file telegram_easy-0.2.0.tar.gz.

File metadata

  • Download URL: telegram_easy-0.2.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for telegram_easy-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8afff92a4a0a44eb7146f14015479ab35522d09be5d3e4ff1f9e503a546fcd4e
MD5 e08db00eecb863133a034851c6f348ec
BLAKE2b-256 908aa9cce2ca6d7269dbe4812b9815b252bd9e66eca6ca069fe9ddba07941cec

See more details on using hashes here.

File details

Details for the file telegram_easy-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: telegram_easy-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for telegram_easy-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 10230da73b07b0e7c07bf34d573dcba8f8b126b3bca78a8186f8699809e700ad
MD5 9076b8d1a4ff8e356eeacc980ab2d6d6
BLAKE2b-256 2e9bdf5a68fcd8e8a57124bcf022f624977610c75cb5cf1d8872f8b95a89c551

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page