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()

# หากต้องการส่งแบบ synchronous
notifier.tg_send_text("hello")

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

การทดสอบ

ใช้ 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.1.tar.gz (6.9 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.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: smsn_telegram_notify-0.1.1.tar.gz
  • Upload date:
  • Size: 6.9 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.1.tar.gz
Algorithm Hash digest
SHA256 97851554b24048481f4d9e9494206fcab77762a1024bbd34e74c4c88ca80d417
MD5 1dc9b98623f711f0a36a378e6518fe01
BLAKE2b-256 055bc9d3b021c2f31a7ce0c102820d3a4f6d5fccf28db3359bfde5e67dccfde8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for smsn_telegram_notify-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1912fa4643f0e295b74b84fdb57edce73e7a3b27d082134995b76198965590f4
MD5 0b73173badaa915b6615cb1e01b10fb4
BLAKE2b-256 8472ed88e20a30ad6d2387adfd942226e8be3b327064ee0cd4fee32740fb1ac0

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