Skip to main content

Telegram Bot Short Message Service: Small requests wrapper to send message to chat via Telegram Bot API

Project description

tgbsms - Telegram Bot Short Message Service

tgbsms is a simple Python utility for sending short messages and/or images via Telegram using the Telegram Bot API.

Features

  • Automatically trims text to fit Telegram's maximum length
  • Supports sending text and images
  • Compatible with HTML, Markdown, and MarkdownV2 parse modes

Installation

You can install tgbsms directly from PyPI using pip:

pip install tgbsms

Usage

To send a message using tgbsms, you can use the send_message function. Here's an example:

from tgbsms import ParseMode
from tgbsms import send_message

send_message(
    text="__message__",
    image=open("image.png", "rb"),
    parse_mode=ParseMode.MarkdownV2,
    telegram_chat_id="<chat_id>",
    telegram_bot_token="<your_bot_token>",
)

Parameters

Parameter Description Optional
text The message to send (string). If the message exceeds 4000 characters, it will be cropped. Yes. If there is an image passed to function
image file-like object, BytesIO, or URL (ыек) representing the image to send Yes. If there is a text passed to function
parse_mode One of ParseMode.HTML, ParseMode.Markdown, or ParseMode.MarkdownV2 (from enums) Yes. Defaults to HTML
telegram_bot_token Your bot's token provided by BotFather. Yes. If you export environment variable TELEGRAM_BOT_TOKEN
telegram_chat_id The chat ID where the message will be sent. Can be an integer or string. Yes. If you export environment variable TELEGRAM_CHAT_ID

Environment Variables

If you prefer not to pass the telegram_bot_token and telegram_chat_id directly, you can set them as environment variables:

  • TELEGRAM_BOT_TOKEN: Your Telegram bot's token.
  • TELEGRAM_CHAT_ID: The chat ID for the message.

Once the environment variables are set, the function can be used without passing these parameters directly

export TELEGRAM_BOT_TOKEN="<your_bot_token>"
export TELEGRAM_CHAT_ID="<chat_id>"

License

This project is licensed under the MIT License. See the LICENSE file for details.

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

tgbsms-1.3.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

tgbsms-1.3.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file tgbsms-1.3.0.tar.gz.

File metadata

  • Download URL: tgbsms-1.3.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.1.dev0+g94f810c.d20240510 CPython/3.12.6

File hashes

Hashes for tgbsms-1.3.0.tar.gz
Algorithm Hash digest
SHA256 354e5a658e803b2d65567d3ea53a9ff1d2308a2d727bda9081bcdb7a50e21c48
MD5 535bbb0857476c273140c6039ecfd9f8
BLAKE2b-256 e87f107b5da1e296c0eb848f5b44e3e0fee02afa41b5c72094938774d2164c46

See more details on using hashes here.

File details

Details for the file tgbsms-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: tgbsms-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.1.dev0+g94f810c.d20240510 CPython/3.12.6

File hashes

Hashes for tgbsms-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b5f45fd8dfeccbfb6e53a65bfcae20b4c2fd963edad13de12c52424cae8a89ec
MD5 01e769ab0ba6d571f5b1e03411a866d4
BLAKE2b-256 ec2f8954e73492b3be98064b8376cb52a840b827751ede1bfb041d00b20d3396

See more details on using hashes here.

Supported by

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