-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram.go
More file actions
35 lines (28 loc) · 913 Bytes
/
telegram.go
File metadata and controls
35 lines (28 loc) · 913 Bytes
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
package web
import (
"bytes"
"fmt"
"net/http"
)
// SendTelegramMessage sends the given message to the Telegram bot which is bound to the given token and chatID.
func SendTelegramMessage(token string, chatID string, message string) error {
data := fmt.Sprintf("{\"chat_id\": \"%s\", \"text\": \"%s\"}", chatID, message)
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.telegram.org/bot%v/sendMessage", token), bytes.NewBufferString(data))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("could not make Telegram request: %v", err)
}
defer resp.Body.Close()
if resp == nil {
return fmt.Errorf("no response from Telegram request")
}
if resp.StatusCode >= 400 {
return fmt.Errorf("telegram responded with status code %v", resp.StatusCode)
}
return nil
}