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 via Telegram using the Telegram Bot API.

Features

  • Trims the text to fit Telegram's maximum length
  • Sends text/images with Markdwon
  • HTML/Markdown/MarkdownV2 parse_modes

Installation

You can install tgbsms directly from PyPI using pip:

pip install tgbsms

Usage

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

from tgbsms import send_message

# Send text
send_message("message", telegram_chat_id="<chat_id>", telegram_bot_token="<your_bot_token>")
# Send image with text
send_message("message", telegram_chat_id="<chat_id>", telegram_bot_token="<your_bot_token>", image=open("image.png", "rb"))
# Send text with Markdown
send_message("__message__", telegram_chat_id="<chat_id>", telegram_bot_token="<your_bot_token>", parse_mode=ParseMode.MarkdownV2)
# Send text with Markdown and image
send_message("__message__", telegram_chat_id="<chat_id>", telegram_bot_token="<your_bot_token>", parse_mode=ParseMode.MarkdownV2, image=open("image.png", "rb"))

Parameters:

  • text: The message to send (string). If the message exceeds 4000 characters, it will be cropped.
  • image: BytestIO object,
  • parse_mode: ParseMode.HTML/Markdown/MarkdownV2 (from enums import ParseMode),
  • telegram_chat_id: (Optional) The ID of the chat where the message will be sent. This can be an integer or a string.
  • telegram_bot_token: (Optional) Your bot's TOKEN provided by Telegram.x

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>"

And then:

from tgbsms import send_message

send_message("test")

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.1.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

tgbsms-1.1.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tgbsms-1.1.0.tar.gz
Algorithm Hash digest
SHA256 3445c1c6fa56b9f8b6d0a875e690285c658be5ea12978b89fdd0d47d68628bce
MD5 b01c5a2b60f0dcd07a1629b65e48305e
BLAKE2b-256 a692bc6111fd37970234ed8935a9dd9a7cfbaa73209bdab9272afbd029259663

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for tgbsms-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c1ec9b28f722d4091bca6dde187046a2962cec33e8c95150c556c169d83e8be2
MD5 70ab5ff522d351313332bd3cfa74b3aa
BLAKE2b-256 11fc902dfe4fdab6fbde076322a119a4f9e80be8933f9d61dafbdbdf87dad13a

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