-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtelegram.go
More file actions
165 lines (157 loc) · 4.13 KB
/
Copy pathtelegram.go
File metadata and controls
165 lines (157 loc) · 4.13 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package grest
import (
"bytes"
"encoding/json"
"io"
"mime/multipart"
"net/http"
"path/filepath"
)
// Telegram is an utility to send telegram message using the Telegram API.
type Telegram struct {
BaseURL string
BotToken string
ChatID string
ParseMode string
Text string
Photo *multipart.FileHeader
Audio *multipart.FileHeader
Video *multipart.FileHeader
Document *multipart.FileHeader
ReplyMarkup any
}
// AddMessage sets the text message to be sent.
func (t *Telegram) AddMessage(text string) {
t.Text = text
}
// AddAttachment adds an attachment to the message based on the file type.
func (t *Telegram) AddAttachment(file *multipart.FileHeader) {
switch filepath.Ext(file.Filename) {
case "jpg", "jpeg", "png", "gif":
t.Photo = file
case "mp3":
t.Audio = file
case "mp4":
t.Video = file
default:
t.Document = file
}
}
// Send sends the message with attachments to the specified chat.
func (t *Telegram) Send() error {
if t.BaseURL == "" {
t.BaseURL = "https://api.telegram.org"
}
if t.BotToken == "" {
return NewError(http.StatusInternalServerError, "BotToken is required")
}
if t.ChatID == "" {
return NewError(http.StatusInternalServerError, "ChatID is required")
}
if t.ParseMode == "" {
t.ParseMode = "MarkdownV2"
}
endPoint := "sendMessage"
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
writer.WriteField("chat_id", t.ChatID)
writer.WriteField("parse_mode", t.ParseMode)
writer.WriteField("text", t.Text)
if t.Photo != nil {
f, err := t.Photo.Open()
if err == nil {
key := "photo"
endPoint = "sendPhoto"
if t.Photo.Size > 10485760 {
key = "document"
endPoint = "sendDocument"
}
part, err := writer.CreateFormFile(key, t.Photo.Filename)
if err != nil {
return NewError(http.StatusInternalServerError, err.Error())
}
_, err = io.Copy(part, f)
if err != nil {
return NewError(http.StatusInternalServerError, err.Error())
}
}
} else if t.Audio != nil {
f, err := t.Audio.Open()
if err == nil {
key := "audio"
endPoint = "sendAudio"
if t.Audio.Size > 52428800 {
key = "document"
endPoint = "sendDocument"
}
part, err := writer.CreateFormFile(key, t.Audio.Filename)
if err != nil {
return NewError(http.StatusInternalServerError, err.Error())
}
_, err = io.Copy(part, f)
if err != nil {
return NewError(http.StatusInternalServerError, err.Error())
}
}
} else if t.Video != nil {
f, err := t.Video.Open()
if err == nil {
key := "video"
endPoint = "sendVideo"
if t.Video.Size > 52428800 {
key = "document"
endPoint = "sendDocument"
}
part, err := writer.CreateFormFile(key, t.Video.Filename)
if err != nil {
return NewError(http.StatusInternalServerError, err.Error())
}
_, err = io.Copy(part, f)
if err != nil {
return NewError(http.StatusInternalServerError, err.Error())
}
}
} else if t.Document != nil {
f, err := t.Document.Open()
if err == nil {
key := "Document"
endPoint = "sendDocument"
part, err := writer.CreateFormFile(key, t.Document.Filename)
if err != nil {
return NewError(http.StatusInternalServerError, err.Error())
}
_, err = io.Copy(part, f)
if err != nil {
return NewError(http.StatusInternalServerError, err.Error())
}
}
}
if t.ReplyMarkup != nil {
rm, err := json.Marshal(t.ReplyMarkup)
if err == nil {
writer.WriteField("reply_markup", string(rm))
}
}
err := writer.Close()
if err != nil {
return NewError(http.StatusInternalServerError, err.Error())
}
req, err := http.NewRequest("POST", t.BaseURL+"/"+t.BotToken+"/"+endPoint, body)
req.Header.Add("Content-Type", writer.FormDataContentType())
res, err := http.DefaultClient.Do(req)
if err != nil {
return NewError(http.StatusInternalServerError, err.Error())
}
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode >= 400 {
b, err := io.ReadAll(res.Body)
if err != nil {
return NewError(http.StatusInternalServerError, err.Error())
}
r := map[string]any{}
json.Unmarshal(b, &r)
msg, _ := r["description"].(string)
return NewError(http.StatusInternalServerError, msg, r)
}
return nil
}