telegram-python 0.3.4__py3-none-any.whl → 0.4.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.
- telegram_python/__init__.py +1 -1
- telegram_python/telegram_send.py +46 -46
- {telegram_python-0.3.4.dist-info → telegram_python-0.4.0.dist-info}/METADATA +2 -2
- telegram_python-0.4.0.dist-info/RECORD +5 -0
- {telegram_python-0.3.4.dist-info → telegram_python-0.4.0.dist-info}/WHEEL +1 -1
- telegram_python-0.3.4.dist-info/RECORD +0 -5
telegram_python/__init__.py
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
from .telegram_send import send
|
|
1
|
+
from .telegram_send import send
|
|
2
2
|
__all__ = ["send"] # Оголошуємо, що функція send є частиною публічного API пакета
|
telegram_python/telegram_send.py
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
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
|
-
if not bot_token or not chat_id:
|
|
12
|
-
raise ValueError("Bot token or chat ID not set. Please check your .env file.")
|
|
13
|
-
|
|
14
|
-
logging.basicConfig(level=logging.INFO, filename="py_log.log",filemode="w",
|
|
15
|
-
format="%(asctime)s %(levelname)s %(message)s")
|
|
16
|
-
|
|
17
|
-
def send(message: str) -> None:
|
|
18
|
-
"""
|
|
19
|
-
Відправляє повідомлення в Telegram в заданий час (від 0 до 24).
|
|
20
|
-
"""
|
|
21
|
-
|
|
22
|
-
now = datetime.now()
|
|
23
|
-
current_hour = now.hour
|
|
24
|
-
telegram_max_symbol = 4096
|
|
25
|
-
if start_hour <= current_hour and current_hour < end_hour:
|
|
26
|
-
try:
|
|
27
|
-
if len(message) > telegram_max_symbol:
|
|
28
|
-
message = message[:telegram_max_symbol]
|
|
29
|
-
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
|
|
30
|
-
payload = {
|
|
31
|
-
"chat_id": chat_id,
|
|
32
|
-
"text": message
|
|
33
|
-
}
|
|
34
|
-
my_rez=requests.post(url, json=payload, timeout=10)
|
|
35
|
-
|
|
36
|
-
if my_rez.status_code == 200:
|
|
37
|
-
logging.info(f"Message sent successfully to chat_id={chat_id}")
|
|
38
|
-
else:
|
|
39
|
-
logging.error(f"Message sent with reason: {my_rez.reason}")
|
|
40
|
-
|
|
41
|
-
except requests.exceptions.RequestException as e:
|
|
42
|
-
logging.error(f"Error sending message: {e}") # помилки, пов’язані з HTTP-запитами
|
|
43
|
-
except Exception as e:
|
|
44
|
-
logging.error(f"Unexpected error: {e}")
|
|
45
|
-
else:
|
|
46
|
-
logging.info(f"Current time ({current_hour}) is outside the allowed range ({start_hour}-{end_hour}).")
|
|
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
|
+
if not bot_token or not chat_id:
|
|
12
|
+
raise ValueError("Bot token or chat ID not set. Please check your .env file.")
|
|
13
|
+
|
|
14
|
+
logging.basicConfig(level=logging.INFO, filename="py_log.log",filemode="w",
|
|
15
|
+
format="%(asctime)s %(levelname)s %(message)s")
|
|
16
|
+
|
|
17
|
+
def send(message: str) -> None:
|
|
18
|
+
"""
|
|
19
|
+
Відправляє повідомлення в Telegram в заданий час (від 0 до 24).
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
now = datetime.now()
|
|
23
|
+
current_hour = now.hour
|
|
24
|
+
telegram_max_symbol = 4096
|
|
25
|
+
if start_hour <= current_hour and current_hour < end_hour:
|
|
26
|
+
try:
|
|
27
|
+
if len(message) > telegram_max_symbol:
|
|
28
|
+
message = message[:telegram_max_symbol]
|
|
29
|
+
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
|
|
30
|
+
payload = {
|
|
31
|
+
"chat_id": chat_id,
|
|
32
|
+
"text": message
|
|
33
|
+
}
|
|
34
|
+
my_rez=requests.post(url, json=payload, timeout=10)
|
|
35
|
+
|
|
36
|
+
if my_rez.status_code == 200:
|
|
37
|
+
logging.info(f"Message sent successfully to chat_id={chat_id}")
|
|
38
|
+
else:
|
|
39
|
+
logging.error(f"Message sent with reason: {my_rez.reason}")
|
|
40
|
+
|
|
41
|
+
except requests.exceptions.RequestException as e:
|
|
42
|
+
logging.error(f"Error sending message: {e}") # помилки, пов’язані з HTTP-запитами
|
|
43
|
+
except Exception as e:
|
|
44
|
+
logging.error(f"Unexpected error: {e}")
|
|
45
|
+
else:
|
|
46
|
+
logging.info(f"Current time ({current_hour}) is outside the allowed range ({start_hour}-{end_hour}).")
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
telegram_python/__init__.py,sha256=9xZhs4cqhZjpwppmKcsH2ZF3KLSpC3evddvAfKKDUpI,157
|
|
2
|
+
telegram_python/telegram_send.py,sha256=g5s0k7vnYNBoBrYs7RwlCIahkNKErM4c-eh-JDPUHwo,1818
|
|
3
|
+
telegram_python-0.4.0.dist-info/METADATA,sha256=iR9B5EC0vYEOdL321e0XGpnrO7xc28nTyy0Lh5E4-P4,575
|
|
4
|
+
telegram_python-0.4.0.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
|
|
5
|
+
telegram_python-0.4.0.dist-info/RECORD,,
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
telegram_python/__init__.py,sha256=imyb6s9X7XSCuwhyYSTAwWUctgtdiO3GzWBzNKacJDI,158
|
|
2
|
-
telegram_python/telegram_send.py,sha256=wqTUdOa6Gj9SupbcIsb-ApRUn_YSAXLpNMJ8PLl6yd8,1864
|
|
3
|
-
telegram_python-0.3.4.dist-info/METADATA,sha256=vS4BwQa1IVwY6BBcDC36F2thD4d4ByyM06cpAq7ID8k,575
|
|
4
|
-
telegram_python-0.3.4.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
|
|
5
|
-
telegram_python-0.3.4.dist-info/RECORD,,
|