mm-telegram 0.1.1__py3-none-any.whl → 0.1.2__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.
mm_telegram/simple_message.py
CHANGED
@@ -3,30 +3,60 @@ import asyncio
|
|
3
3
|
from mm_std import Result, http_request
|
4
4
|
|
5
5
|
|
6
|
-
async def send_message(
|
6
|
+
async def send_message(
|
7
|
+
bot_token: str,
|
8
|
+
chat_id: int,
|
9
|
+
message: str,
|
10
|
+
timeout: float = 5,
|
11
|
+
inter_message_delay_seconds: int = 3,
|
12
|
+
) -> Result[list[int]]:
|
13
|
+
"""
|
14
|
+
Sends a message to a Telegram chat.
|
15
|
+
|
16
|
+
If the message exceeds the Telegram character limit (4096),
|
17
|
+
it will be split into multiple messages and sent sequentially
|
18
|
+
with a delay between each part.
|
19
|
+
|
20
|
+
Args:
|
21
|
+
bot_token: The Telegram bot token.
|
22
|
+
chat_id: The target chat ID.
|
23
|
+
message: The message text to send.
|
24
|
+
timeout: The HTTP request timeout in seconds. Defaults to 5.
|
25
|
+
inter_message_delay_seconds: The delay in seconds between sending
|
26
|
+
parts of a long message. Defaults to 3.
|
27
|
+
|
28
|
+
Returns:
|
29
|
+
A Result object containing a list of message IDs for the sent messages
|
30
|
+
on success, or an error details on failure. The 'extra' field in the
|
31
|
+
Result contains the raw responses from the Telegram API.
|
32
|
+
"""
|
7
33
|
messages = _split_string(message, 4096)
|
8
34
|
responses = []
|
9
|
-
|
35
|
+
result_message_ids = []
|
10
36
|
while True:
|
11
|
-
|
12
|
-
params = {"chat_id": chat_id, "text":
|
13
|
-
res = await http_request(
|
37
|
+
text_part = messages.pop(0)
|
38
|
+
params = {"chat_id": chat_id, "text": text_part}
|
39
|
+
res = await http_request(
|
40
|
+
f"https://api.telegram.org/bot{bot_token}/sendMessage", method="post", json=params, timeout=timeout
|
41
|
+
)
|
14
42
|
responses.append(res.to_dict())
|
15
43
|
if res.is_err():
|
16
|
-
return Result.err(res.error or "error", extra={"responses":
|
44
|
+
return Result.err(res.error or "error sending message", extra={"responses": responses})
|
17
45
|
|
18
46
|
message_id = res.parse_json_body("result.message_id", none_on_error=True)
|
19
47
|
if message_id:
|
20
|
-
|
48
|
+
result_message_ids.append(message_id)
|
21
49
|
else:
|
22
|
-
|
50
|
+
# Log the unexpected response for debugging?
|
51
|
+
return Result.err("unknown_response_structure", extra={"responses": responses})
|
23
52
|
|
24
53
|
if len(messages):
|
25
|
-
await asyncio.sleep(
|
54
|
+
await asyncio.sleep(inter_message_delay_seconds)
|
26
55
|
else:
|
27
56
|
break
|
28
|
-
return Result.ok(
|
57
|
+
return Result.ok(result_message_ids, extra={"responses": responses})
|
29
58
|
|
30
59
|
|
31
60
|
def _split_string(text: str, chars_per_string: int) -> list[str]:
|
61
|
+
"""Splits a string into a list of strings, each with a maximum length."""
|
32
62
|
return [text[i : i + chars_per_string] for i in range(0, len(text), chars_per_string)]
|
@@ -0,0 +1,6 @@
|
|
1
|
+
mm_telegram/__init__.py,sha256=Nl5I50id12u28Q0H_uHziyd2t_bjySeZAH1etBOVW3A,57
|
2
|
+
mm_telegram/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
|
+
mm_telegram/simple_message.py,sha256=nbBWG58PjKU74NYqxwMANBLax0HJqgJ0B6r3FOAeVxg,2285
|
4
|
+
mm_telegram-0.1.2.dist-info/METADATA,sha256=SX9nfEVX-V_8fdzTahfrY_RuDRMpNR0yq_BXugWXDd0,109
|
5
|
+
mm_telegram-0.1.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
6
|
+
mm_telegram-0.1.2.dist-info/RECORD,,
|
@@ -1,6 +0,0 @@
|
|
1
|
-
mm_telegram/__init__.py,sha256=Nl5I50id12u28Q0H_uHziyd2t_bjySeZAH1etBOVW3A,57
|
2
|
-
mm_telegram/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
|
-
mm_telegram/simple_message.py,sha256=fT0zhmUPqq_w2EvTdOKLC0JhVMh3nZmthApY8dmY_fk,1193
|
4
|
-
mm_telegram-0.1.1.dist-info/METADATA,sha256=8Iusxz_r-QCJMu7dN8BNW9VN2EFNUZBxO3G0NwNYnEc,108
|
5
|
-
mm_telegram-0.1.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
6
|
-
mm_telegram-0.1.1.dist-info/RECORD,,
|
File without changes
|