-
Notifications
You must be signed in to change notification settings - Fork 9
feat(emails): Admin Telegram notification on every email sent #718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sweetmantech
wants to merge
5
commits into
test
Choose a base branch
from
feat/emails-telegram-notify
base: test
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+255
−22
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5c5cb7c
feat(emails): charge 1 credit per send via POST /api/emails
sweetmantech ecf0594
feat(emails): post an Admin Telegram notification on every email sent
sweetmantech f514770
fix(emails): wrap sendEmailHandler in try/catch → controlled 500 (rev…
sweetmantech fd178c1
feat(emails): stamp usage_events.model_id = "POST /api/emails"
sweetmantech affd249
Merge billing branch (try/catch + modelId) into telegram branch
sweetmantech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { notifyEmailSent } from "../notifyEmailSent"; | ||
|
|
||
| const mockSendMessage = vi.fn(); | ||
| vi.mock("@/lib/telegram/sendMessage", () => ({ | ||
| sendMessage: (...args: unknown[]) => mockSendMessage(...args), | ||
| })); | ||
|
|
||
| describe("notifyEmailSent", () => { | ||
| beforeEach(() => vi.clearAllMocks()); | ||
|
|
||
| it("posts a Markdown message with the email details", async () => { | ||
| await notifyEmailSent({ | ||
| accountId: "acct-1", | ||
| to: ["dest@example.com"], | ||
| cc: ["cc@example.com"], | ||
| subject: "Weekly report", | ||
| resendId: "resend-123", | ||
| }); | ||
|
|
||
| expect(mockSendMessage).toHaveBeenCalledTimes(1); | ||
| const [message, options] = mockSendMessage.mock.calls[0]; | ||
| expect(options).toEqual({ parse_mode: "Markdown" }); | ||
| expect(message).toContain("acct-1"); | ||
| expect(message).toContain("dest@example.com"); | ||
| expect(message).toContain("cc@example.com"); | ||
| expect(message).toContain("Weekly report"); | ||
| expect(message).toContain("resend-123"); | ||
| }); | ||
|
|
||
| it("omits the CC line when there is no cc", async () => { | ||
| await notifyEmailSent({ | ||
| accountId: "acct-1", | ||
| to: ["dest@example.com"], | ||
| subject: "Hi", | ||
| resendId: "r-1", | ||
| }); | ||
| const [message] = mockSendMessage.mock.calls[0]; | ||
| expect(message).not.toContain("*CC:*"); | ||
| }); | ||
|
|
||
| it("never throws when Telegram fails (best-effort)", async () => { | ||
| mockSendMessage.mockRejectedValue(new Error("telegram down")); | ||
| await expect( | ||
| notifyEmailSent({ accountId: "a", to: ["x@y.com"], subject: "s", resendId: "r" }), | ||
| ).resolves.toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { sendMessage } from "@/lib/telegram/sendMessage"; | ||
|
|
||
| export interface EmailSentNotification { | ||
| accountId: string; | ||
| to: string[]; | ||
| cc?: string[]; | ||
| subject: string; | ||
| resendId: string; | ||
| } | ||
|
|
||
| /** | ||
| * Formats a sent-email notification into a Telegram message. | ||
| * | ||
| * @param n - The sent-email details. | ||
| * @returns A Markdown-formatted message string. | ||
| */ | ||
| function formatEmailSentMessage(n: EmailSentNotification): string { | ||
| const lines = [ | ||
| "*Email sent* (`POST /api/emails`)", | ||
| "", | ||
| `*Account:* ${n.accountId}`, | ||
| `*To:* ${n.to.join(", ")}`, | ||
| ]; | ||
| if (n.cc && n.cc.length > 0) { | ||
| lines.push(`*CC:* ${n.cc.join(", ")}`); | ||
| } | ||
| lines.push(`*Subject:* ${n.subject}`); | ||
| lines.push(`*Resend ID:* ${n.resendId}`); | ||
| lines.push(""); | ||
| lines.push(`*Time:* ${new Date().toISOString()}`); | ||
| return lines.join("\n"); | ||
| } | ||
|
|
||
| /** | ||
| * Posts an Admin Telegram notification for each email sent (the same | ||
| * `TELEGRAM_CHAT_ID` channel as other alerts), so the team can review the | ||
| * quality and frequency of outgoing email. Best-effort — never throws or | ||
| * blocks the send, mirroring `sendErrorNotification`. | ||
| * | ||
| * @param n - The sent-email details. | ||
| */ | ||
| export async function notifyEmailSent(n: EmailSentNotification): Promise<void> { | ||
| try { | ||
| await sendMessage(formatEmailSentMessage(n), { parse_mode: "Markdown" }); | ||
| } catch (err) { | ||
| console.error("Error in notifyEmailSent:", err); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Raw dynamic fields are interpolated into Telegram Markdown without escaping. Special characters in addresses/subject can make sendMessage fail, so successful emails may not be reported.
Prompt for AI agents