-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrestart_delivery.go
More file actions
86 lines (75 loc) · 2.04 KB
/
Copy pathrestart_delivery.go
File metadata and controls
86 lines (75 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package telegram
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/Suren878/matrixclaw/internal/core"
)
type RestartDeliveryCodec struct{}
func (RestartDeliveryCodec) ClientName() string {
return ClientName
}
func (RestartDeliveryCodec) NormalizeRestartDeliveryAddress(raw json.RawMessage) (json.RawMessage, error) {
address, err := decodeRestartDeliveryAddress(raw)
if err != nil {
return nil, err
}
return encodeDeliveryAddress(address), nil
}
type RestartDeliverySenderConfig struct {
BotToken string
BaseURL string
HTTPClient HTTPDoer
API BotAPI
}
type RestartDeliverySender struct {
api BotAPI
}
func NewRestartDeliverySender(cfg RestartDeliverySenderConfig) (*RestartDeliverySender, error) {
api := cfg.API
if api == nil {
client, err := NewClient(ClientConfig{
Token: cfg.BotToken,
BaseURL: cfg.BaseURL,
HTTPClient: cfg.HTTPClient,
})
if err != nil {
return nil, err
}
api = client
}
return &RestartDeliverySender{api: api}, nil
}
func (s *RestartDeliverySender) ClientName() string {
return ClientName
}
func (s *RestartDeliverySender) DeliverRestartNotification(ctx context.Context, delivery core.ClientDelivery, fallback string) error {
if s == nil || s.api == nil {
return fmt.Errorf("telegram: restart delivery sender is not configured")
}
address, err := decodeRestartDeliveryAddress(delivery.Address)
if err != nil {
return err
}
text := fallback
if summary := strings.TrimSpace(delivery.Summary); summary != "" {
text = summary
}
_, err = s.api.EditMessageText(ctx, EditMessageTextRequest{
ChatID: address.ChatID,
MessageID: address.MessageID,
Text: telegramPersonaText(text),
})
return err
}
func decodeRestartDeliveryAddress(raw json.RawMessage) (DeliveryAddress, error) {
address, err := decodeDeliveryAddress(raw)
if err != nil {
return DeliveryAddress{}, fmt.Errorf("telegram: restart %w", err)
}
if address.MessageID == 0 {
return DeliveryAddress{}, fmt.Errorf("telegram: restart delivery address missing message_id")
}
return address, nil
}