Skip to main content

Simple helper for sending Telegram notifications

Project description

smsn_telegram_notify

ไลบรารี Python สำหรับส่งข้อความ รูปภาพ ไฟล์ หรือวิดีโอไปยัง Telegram ได้อย่างรวดเร็ว

การติดตั้ง

ติดตั้งจาก PyPI:

pip install smsn-telegram-notify

ติดตั้งจากซอร์สโค้ดในเครื่อง:

pip install .

เริ่มต้นใช้งาน

from smsn_telegram_notify import TelegramNotify

# อ่านค่าจากไฟล์ config.toml
notifier = TelegramNotify(config_path="config.toml")

# หรือกำหนด token และ chat_id โดยตรง
notifier = TelegramNotify(token="YOUR_TOKEN", chat_id="CHAT_ID")

ตัวอย่างสคริปต์แบบง่ายในการส่งข้อความหนึ่งครั้ง:

from smsn_telegram_notify import TelegramNotify
import time

if __name__ == "__main__":
    notifier = TelegramNotify(config_path="config.toml")
    notifier.start_send_text("Hello, World")
    time.sleep(2)  # รอให้คิวส่งเสร็จก่อนปิดโปรแกรม

ตัวอย่างไฟล์ config.toml:

[smsn_telegram_notify]
token = "123456:ABCDEFG"
chat_id = "-100123456"
notify_interval_sec = 60

การหา Chat ID

หากยังไม่ทราบ chat_id ของกลุ่ม สามารถใช้สคริปต์ get_chat_id.py ในโปรเจกต์ได้ดังนี้:

export TELEGRAM_BOT_TOKEN="YOUR_TOKEN"
python get_chat_id.py

สคริปต์จะพิมพ์ชื่อกลุ่มและ chat_id ที่พบจากข้อมูล getUpdates ของบอท

ฟังก์ชันที่เกี่ยวข้องกับภาพหรือวิดีโอจำเป็นต้องติดตั้ง opencv-python เพิ่มเติม

Public Send Methods

เมทอดทั้งหมดเป็น non-blocking และมีตัวเลือก time_interval เพื่อกำหนดช่วงเวลาขั้นต่ำระหว่างการส่ง (ค่าเริ่มต้น 5 วินาที หรือค่าจาก notify_interval_sec).

start_send_text(msg, time_interval=None)

ส่งข้อความตัวอักษรธรรมดา

import time
notifier.start_send_text("hello")
time.sleep(3)          # เมทอดนี้ทำงานแบบไม่รอผล จึงควรรอให้คิวส่งเสร็จก่อนปิดโปรแกรม
# หรือ notifier.send_queue.join()

start_send_image(msg, image, time_interval=None)

ส่งรูปภาพจากอ็อบเจ็กต์ภาพของ OpenCV

import cv2
img = cv2.imread("test.jpg")
notifier.start_send_image("รูปทดสอบ", img)

start_bytes_send_file(msg, bytes_data, time_interval=None)

ส่งไฟล์จากข้อมูลแบบไบต์โดยตรง

with open("report.pdf", "rb") as f:
    data = f.read()
notifier.start_bytes_send_file("ส่งจาก bytes", data)

start_frame_send_file(msg, frame, time_interval=None)

ส่งเฟรมภาพ (เช่น เฟรมจากกล้อง)

frame = cv2.imread("frame.jpg")
notifier.start_frame_send_file("ส่งเฟรม", frame)

start_send_file(msg, path_file, time_interval=None)

ส่งไฟล์จากพาธบนดิสก์

notifier.start_send_file("ส่งไฟล์", "document.txt")

start_send_video(msg, path_file, time_interval=None)

ส่งวิดีโอจากพาธบนดิสก์

notifier.start_send_video("ส่งวิดีโอ", "clip.mp4")

Direct Send Methods (Synchronous)

เมทอด tg_send_* ส่งข้อมูลโดยตรงโดยไม่ผ่านคิว — เหมาะสำหรับกรณีที่ต้องการให้ส่งทันที

เมทอด ใช้งาน
tg_send_text(msg) ส่งข้อความ
tg_send_image(msg, image) ส่งภาพจาก OpenCV array
tg_byte_send_file(msg, bytes_data, filename=None) ส่งไฟล์จาก bytes
tg_frame_send_file(msg, frame) ส่งเฟรมภาพจาก OpenCV
tg_send_file(msg, path_file) ส่งไฟล์จากพาธบนดิสก์
tg_send_video(msg, path_file) ส่งวิดีโอจากพาธบนดิสก์
# ส่งแบบ synchronous (รอผลทันที)
notifier.tg_send_text("hello")
notifier.tg_send_file("รายงาน", "report.pdf")

การทดสอบ

ใช้ pytest เพื่อรันชุดการทดสอบของโปรเจกต์

pytest

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

smsn_telegram_notify-0.1.2.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

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

smsn_telegram_notify-0.1.2-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file smsn_telegram_notify-0.1.2.tar.gz.

File metadata

  • Download URL: smsn_telegram_notify-0.1.2.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for smsn_telegram_notify-0.1.2.tar.gz
Algorithm Hash digest
SHA256 e1c4a3f604ac4220b665e0b32f35904767add8da2bd4406a8483d5e3395f9c4e
MD5 3e7732757cd77e6e32c9fd1c11112b29
BLAKE2b-256 b1a567f9c12e8a89e9fd30d5a6280ce5f801526164d446c9204a461c3a853126

See more details on using hashes here.

File details

Details for the file smsn_telegram_notify-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for smsn_telegram_notify-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c0174914ee0b84bbcd717242fc7fbaf2ab3967afa928116980ca8b6aa1da7fae
MD5 316f63f42b8ba7aa7ae621819a0e3beb
BLAKE2b-256 6909194017eead96b6b1191d8a46d6313ce6dfdf64ca0fc542bed8dcfce7717f

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