telegram-python 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: telegram-python
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: A Python package for sending Telegram messages
|
|
5
|
+
Author: Tetiana
|
|
6
|
+
Author-email: Tatiana <durachevska@ukr.net>
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
|
|
9
|
+
poetry add telegram_send
|
|
10
|
+
|
|
11
|
+
poetry run python demo/demo.py
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
telegram_send/__init__.py,sha256=imyb6s9X7XSCuwhyYSTAwWUctgtdiO3GzWBzNKacJDI,158
|
|
2
|
+
telegram_send/telegram_send.py,sha256=a33Jf5aROlZXZzkiDhn2MlAJtRkReLbCQFRePs1RXCI,1755
|
|
3
|
+
telegram_python-0.3.0.dist-info/METADATA,sha256=OAT82CxyuXVWLO7NDDjR9yWquwB-5pCZcrmiRt8GL-E,284
|
|
4
|
+
telegram_python-0.3.0.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
|
|
5
|
+
telegram_python-0.3.0.dist-info/top_level.txt,sha256=p1caPsfF4vqshvK4zD3ZoFUjy9TbzrLwuIUFSMgnEGs,14
|
|
6
|
+
telegram_python-0.3.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
telegram_send
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
import os
|
|
4
|
+
import logging
|
|
5
|
+
|
|
6
|
+
start_hour = int(os.getenv("TELEGRAM_START_HOUR", 0))
|
|
7
|
+
end_hour = int(os.getenv("TELEGRAM_END_HOUR", 24))
|
|
8
|
+
bot_token = os.getenv("TELEGRAM_BOT_TOKEN")
|
|
9
|
+
chat_id = os.getenv("TELEGRAM_CHAT_ID")
|
|
10
|
+
|
|
11
|
+
logging.basicConfig(level=logging.INFO, filename="py_log.log",filemode="w",
|
|
12
|
+
format="%(asctime)s %(levelname)s %(message)s")
|
|
13
|
+
|
|
14
|
+
def send(message: str) -> None:
|
|
15
|
+
"""
|
|
16
|
+
Відправляє повідомлення в Telegram в заданий час (від 0 до 24).
|
|
17
|
+
"""
|
|
18
|
+
now = datetime.now()
|
|
19
|
+
current_hour = now.hour
|
|
20
|
+
telegram_max_symbol = 4096
|
|
21
|
+
if start_hour <= current_hour and current_hour < end_hour:
|
|
22
|
+
try:
|
|
23
|
+
if len(message) > telegram_max_symbol:
|
|
24
|
+
message = message[:telegram_max_symbol]
|
|
25
|
+
|
|
26
|
+
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
|
|
27
|
+
payload = {
|
|
28
|
+
"chat_id": chat_id,
|
|
29
|
+
"text": message
|
|
30
|
+
}
|
|
31
|
+
my_rez=requests.post(url, json=payload, timeout=10)
|
|
32
|
+
print(my_rez)
|
|
33
|
+
|
|
34
|
+
if my_rez.status_code == 200:
|
|
35
|
+
logging.info(f"Message sent successfully to chat_id={chat_id}")
|
|
36
|
+
else:
|
|
37
|
+
logging.error(f"Message sent with reason: {my_rez.reason}")
|
|
38
|
+
|
|
39
|
+
except requests.exceptions.RequestException as e:
|
|
40
|
+
logging.error(f"Error sending message: {e}") # помилки, пов’язані з HTTP-запитами
|
|
41
|
+
except Exception as e:
|
|
42
|
+
logging.error(f"Unexpected error: {e}")
|
|
43
|
+
else:
|
|
44
|
+
logging.info(f"Current time ({current_hour}) is outside the allowed range ({start_hour}-{end_hour}).")
|