Skip to content

grafana/grafana#89393 telegram 2-step send to fix formatting errors#448

Open
danilgotvyansky wants to merge 6 commits intografana:mainfrom
danilgotvyansky:grafana/grafana#89393/telegram-fix-formatting
Open

grafana/grafana#89393 telegram 2-step send to fix formatting errors#448
danilgotvyansky wants to merge 6 commits intografana:mainfrom
danilgotvyansky:grafana/grafana#89393/telegram-fix-formatting

Conversation

@danilgotvyansky
Copy link

Issue: Fixes grafana/grafana#89393

Description:

Here is how I tried to fix the issue with Grafana and Telegram communication being corrupted by Telegram failing to validate parsing syntaxes if the message is truncated

I am not a Go developer, but I've tried to show my idea:

  • Format message after the truncation to get guaranteed delivery with none parse mode
  • To achieve this, we send a message with none parse mode always and then edit it separately after successful delivery with HTML or Markdown* syntaxes.

I've verified that a new approach works for HTML, but it doesn't fully work for Markdown since Markdown is not currently working in the master branch version, either, and in Grafana Cloud, according to my tests.

Here is also some technical info for these changes:

More

  1. Webhook Infrastructure Enhancement (http/client.go, http/webhook.go, receivers/webhook.go)
  • SendWebhook method signature changed: Now returns (*receivers.WebhookResponse, error) instead of just error
  • Added WebhookResponse struct: Captures StatusCode, Body, and Headers from HTTP responses
  • Enhanced error handling: Maintains response data even when validation fails
  • Purpose: Enables receivers to access HTTP response details for processing (critical for Telegram message ID extraction)
  1. Telegram Message Editing Feature (receivers/telegram/v1/telegram.go)
  • Added message ID tracking: lastMessageID field with thread-safe access via mutex
  • Two-step sending process:
    1. Send message with parse_mode="" (None) to ensure delivery
    2. Edit message with configured parse_mode to apply formatting
  • New methods:
    • EditMessage(): Edits existing messages using Telegram's editMessageText API
    • extractMessageID(): Parses Telegram API response to extract message ID
    • GetLastMessageID() / SetLastMessageID(): Thread-safe message ID management
  • API endpoint support: Added editMessageText alongside existing sendMessage and sendPhoto
  1. Test Updates (receivers/telegram/v1/telegram_test.go)
  • Updated expectations: Tests now expect two webhook calls per message (send + edit)
  • Parse mode handling: First call sends without formatting, second applies configured parse mode
  • Response mocking: Added mock response bodies with message IDs for testing
  1. All Receivers Updated (19 receiver implementations)
  • Signature compatibility: All SendWebhook calls updated to handle new return signature
  • Minimal impact: Most receivers just ignore the response with _ since they don't need it
  • Maintained functionality: No behavioral changes, just API compatibility

…thod

Add WebhookResponse struct to capture HTTP response details (status code, body, headers).
Change SendWebhook signature to return (*WebhookResponse, error) instead of just error.
This enables receivers to access response data for post-processing needs (e.g. messageId for telegram).

Signed-off-by: Danylo Hotvianskyi <danilgotvyansky@gmail.com>
…ponse signature

Update all receiver implementations to handle the new SendWebhook return signature.

Signed-off-by: Danylo Hotvianskyi <danilgotvyansky@gmail.com>
…atting

Implement two-step message sending: send first with no parse_mode to achieve 100% guaranteed delivery,
then edit with configured parse_mode to apply formatting. This prevents 400 errors
from formatting issues with truncated messages.

- Add message ID tracking with thread-safe access
- Add EditMessage method using editMessageText API
- Add extractMessageID for parsing Telegram responses

Signed-off-by: Danylo Hotvianskyi <danilgotvyansky@gmail.com>
Update test expectations to validate the two-step send-then-edit process.
Tests now expect two webhook calls per message with appropriate parse modes.

Signed-off-by: Danylo Hotvianskyi <danilgotvyansky@gmail.com>
@danilgotvyansky danilgotvyansky requested a review from a team as a code owner December 29, 2025 21:09
@github-project-automation github-project-automation bot moved this to In review in Alerting Dec 29, 2025
@CLAassistant
Copy link

CLAassistant commented Dec 29, 2025

CLA assistant check
All committers have signed the CLA.

@fopina
Copy link

fopina commented Jan 4, 2026

I like the idea: always send none and let edit be the one to fail silently!

I can’t really comment on the code though couldn’t the change be restricted to telegram webhook only? So it would send and edit in the “send” method?
Avoiding impact in every webhook due to function signature changes

@danilgotvyansky
Copy link
Author

@fopina Yeah, I tried like that at first, but since the edit action requires response details, it would require either Telegram using an exclusive SendWebhook function or adding a response into an existing one, which requires all other receivers to expect a response and do nothing with it.

The second approach looked easier :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

Truncating Telegram HTML/Markdown alerts returns 400

3 participants