feat: add Telegram backend#65
Conversation
Adds a Telegram Bot API backend using long-polling getUpdates so no public webhook endpoint is needed. Implements the full Backend interface: text replies (with reply_parameters), document uploads via multipart, typing indicators, and incoming attachment download (photo/document/audio/video/ voice/video_note) saved under <session>/attachments. - telegram/: new package, stdlib-only (net/http + encoding/json) - config.go / main.go: wire backendTelegram, env vars (OPENCROW_TELEGRAM_TOKEN[_FILE], _API_BASE, _POLL_TIMEOUT, _ALLOWED_USERS), validation, and createTelegramBackend - nix/module.nix: telegram in OPENCROW_BACKEND enum, env options, assertion that the token is provided when backend is telegram - README.md / docs/configuration.md: backend listing and Telegram configuration reference - tests: backend-level (httptest fake API: send, getUpdates flow, allowlist, attachment download) and config-level (defaults, token file, allowlist override) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Warning Rate limit exceeded
To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughThis pull request adds Telegram as a new messaging backend to the OpenCrow system. It includes configuration loading for Telegram tokens and settings, implements a complete Telegram Bot API backend using long-polling, and integrates the backend into the application factory. Configuration, documentation, and tests are updated accordingly. Changes
Sequence Diagram(s)sequenceDiagram
participant Backend as Telegram Backend
participant TelegramAPI as Telegram Bot API
participant Handler as Message Handler
participant Storage as Attachments Storage
loop Long-Polling Loop
Backend->>TelegramAPI: getUpdates(offset)
TelegramAPI-->>Backend: updates[]
alt Has Updates
loop For each update
alt Has Message
Backend->>Backend: Validate AllowedUsers & ConversationID
alt Validation passes
alt Has attachment
Backend->>TelegramAPI: getFile(file_id)
TelegramAPI-->>Backend: file_path
Backend->>TelegramAPI: Download file binary
TelegramAPI-->>Backend: file content
Backend->>Storage: Save to SessionBaseDir/attachments
Backend->>Handler: Deliver message + attachment marker
else Text only
Backend->>Handler: Deliver message
end
end
Backend->>Backend: Advance offset
end
end
end
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~75 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 0/1 reviews remaining, refill in 49 minutes and 13 seconds.Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
telegram/backend.go (1)
635-642: 💤 Low valueComment mentions "n runes" but implementation truncates by bytes.
The function counts bytes via
len(s), not runes. For ASCII this works fine, but multi-byte UTF-8 characters could be cut mid-sequence.Suggested fix for rune-accurate truncation
// truncate returns s shortened to n runes with an ellipsis suffix when cut. func truncate(s string, n int) string { - if len(s) <= n { + runes := []rune(s) + if len(runes) <= n { return s } - return s[:n] + "..." + return string(runes[:n]) + "..." }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@telegram/backend.go` around lines 635 - 642, The truncate function currently measures and slices by bytes (using len(s) and s[:n]) which can split multi-byte UTF-8 runes; update truncate to operate on runes instead: convert the string to a rune slice (or use utf8.RuneCountInString to check length) and then slice the rune slice to at most n runes before converting back to string and appending "..." when truncated; keep the function name truncate and preserve behavior for short strings (return original) and for truncated strings (return first n runes + "...").
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@telegram/backend.go`:
- Around line 635-642: The truncate function currently measures and slices by
bytes (using len(s) and s[:n]) which can split multi-byte UTF-8 runes; update
truncate to operate on runes instead: convert the string to a rune slice (or use
utf8.RuneCountInString to check length) and then slice the rune slice to at most
n runes before converting back to string and appending "..." when truncated;
keep the function name truncate and preserve behavior for short strings (return
original) and for truncated strings (return first n runes + "...").
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: e6e97e40-948a-4fa1-9e3c-08819ccfe543
📒 Files selected for processing (8)
README.mdconfig.goconfig_test.godocs/configuration.mdmain.gonix/module.nixtelegram/backend.gotelegram/backend_test.go
Pi's replies often include CommonMark formatting (**bold**, `code`, fenced blocks, [links](url), bullets). Telegram does not interpret markdown without a parse_mode hint, so those characters previously showed up literally. Convert markdown to the HTML subset Telegram accepts (b/i/s/code/pre/ a/blockquote, "•" for bullets, headings → bold) and send with parse_mode=HTML. If Telegram rejects the rendered HTML (unbalanced markup slipping through the converter), retry once as plain text without parse_mode so messages still get through. Switch MarkdownFlavor to MarkdownFull and update SystemPromptExtra so pi knows it can use markdown freely. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Adds a Telegram Bot API backend using long-polling getUpdates so no public webhook endpoint is needed. Implements the full Backend interface: text replies (with reply_parameters), document uploads via multipart, typing indicators, and incoming attachment download (photo/document/audio/video/ voice/video_note) saved under /attachments.
Summary by CodeRabbit
Release Notes
New Features
Documentation
Tests