Skip to content

Personal AI, On Personal Devices

Python

split telegram messages based on 4k char limit (#211)

Prathap P186e50fa4fc4
diff --git a/src/openjarvis/channels/telegram.py b/src/openjarvis/channels/telegram.py
index 1063e4d0..a620be17 100644
--- a/src/openjarvis/channels/telegram.py
+++ b/src/openjarvis/channels/telegram.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import logging
import os
+import textwrap
import threading
from typing import Any, Dict, List, Optional
@@ -109,25 +110,33 @@ class TelegramChannel(BaseChannel):
try:
import httpx
+ _TELEGRAM_MAX_LEN = 4096
url = f"https://api.telegram.org/bot{self._token}/sendMessage"
chat_id = conversation_id or channel
- payload: Dict[str, Any] = {
- "chat_id": chat_id,
- "text": content,
- }
- if self._parse_mode:
- payload["parse_mode"] = self._parse_mode
-
- resp = httpx.post(url, json=payload, timeout=10.0)
- if resp.status_code < 300:
- self._publish_sent(channel, content, conversation_id)
- return True
- logger.warning(
- "Telegram API returned status %d: %s",
- resp.status_code,
- resp.text,
+ chunks = textwrap.wrap(
+ content,
+ width=_TELEGRAM_MAX_LEN,
+ break_long_words=True,
+ replace_whitespace=False,
)
- return False
+ for chunk in chunks:
+ payload: Dict[str, Any] = {
+ "chat_id": chat_id,
+ "text": chunk,
+ }
+ if self._parse_mode:
+ payload["parse_mode"] = self._parse_mode
+
+ resp = httpx.post(url, json=payload, timeout=10.0)
+ if resp.status_code >= 300:
+ logger.warning(
+ "Telegram API returned status %d: %s",
+ resp.status_code,
+ resp.text,
+ )
+ return False
+ self._publish_sent(channel, content, conversation_id)
+ return True
except Exception:
logger.debug("Telegram send failed", exc_info=True)
return False
diff --git a/src/openjarvis/evals/configs/gaia-qwen-2b.toml b/src/openjarvis/evals/configs/gaia-qwen-2b.toml
index 409decfd..359885de 100644
--- a/src/openjarvis/evals/configs/gaia-qwen-2b.toml
+++ b/src/openjarvis/evals/configs/gaia-qwen-2b.toml
@@ -28,4 +28,4 @@ name = "gaia"
backend = "jarvis-agent"
agent = "monitor_operative"
max_samples = 50
-tools = ["think", "calculator", "code_interpreter", "web_search", "file_read"]
+tools = ["think", "calculator", "code_interpreter", "web_search", "file_read"]
\ No newline at end of file