|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + |
| 11 | + "github.com/base-org/pessimism/internal/core" |
| 12 | + "github.com/base-org/pessimism/internal/logging" |
| 13 | +) |
| 14 | + |
| 15 | +type TelegramClient interface { |
| 16 | + AlertClient |
| 17 | +} |
| 18 | + |
| 19 | +type TelegramConfig struct { |
| 20 | + Token string |
| 21 | + ChatID string |
| 22 | +} |
| 23 | + |
| 24 | +type telegramClient struct { |
| 25 | + token string |
| 26 | + chatID string |
| 27 | + client *http.Client |
| 28 | +} |
| 29 | + |
| 30 | +func NewTelegramClient(cfg *TelegramConfig) TelegramClient { |
| 31 | + if cfg.Token == "" { |
| 32 | + logging.NoContext().Warn("No Telegram token provided") |
| 33 | + } |
| 34 | + |
| 35 | + return &telegramClient{ |
| 36 | + token: cfg.Token, |
| 37 | + chatID: cfg.ChatID, |
| 38 | + client: &http.Client{}, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +type TelegramPayload struct { |
| 43 | + ChatID string `json:"chat_id"` |
| 44 | + Text string `json:"text"` |
| 45 | +} |
| 46 | + |
| 47 | +type TelegramAPIResponse struct { |
| 48 | + Ok bool `json:"ok"` |
| 49 | + Result json.RawMessage `json:"result"` // Might not be needed for basic response handling |
| 50 | + Error string `json:"description"` |
| 51 | +} |
| 52 | + |
| 53 | +func (tr *TelegramAPIResponse) ToAlertResponse() *AlertAPIResponse { |
| 54 | + if tr.Ok { |
| 55 | + return &AlertAPIResponse{ |
| 56 | + Status: core.SuccessStatus, |
| 57 | + Message: "Message sent successfully", |
| 58 | + } |
| 59 | + } |
| 60 | + return &AlertAPIResponse{ |
| 61 | + Status: core.FailureStatus, |
| 62 | + Message: tr.Error, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func (tc *telegramClient) PostEvent(ctx context.Context, data *AlertEventTrigger) (*AlertAPIResponse, error) { |
| 67 | + payload := TelegramPayload{ |
| 68 | + ChatID: tc.chatID, |
| 69 | + Text: data.Message, |
| 70 | + } |
| 71 | + |
| 72 | + payloadBytes, err := json.Marshal(payload) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", tc.token) |
| 78 | + req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(payloadBytes)) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + req.Header.Set("Content-Type", "application/json") |
| 83 | + |
| 84 | + resp, err := tc.client.Do(req) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + defer resp.Body.Close() |
| 89 | + |
| 90 | + respBytes, err := io.ReadAll(resp.Body) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + |
| 95 | + var apiResp TelegramAPIResponse |
| 96 | + if err = json.Unmarshal(respBytes, &apiResp); err != nil { |
| 97 | + return nil, fmt.Errorf("could not unmarshal telegram response: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + return apiResp.ToAlertResponse(), nil |
| 101 | +} |
| 102 | + |
| 103 | +func (tc *telegramClient) GetName() string { |
| 104 | + return "TelegramClient" |
| 105 | +} |
0 commit comments