blob: 127ebc2c4288b07d19c68c4a59eb92d2c7c86db8 (
plain) (
blame)
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
|
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
type messenger struct {
Token string
ChatID string
}
func (m *messenger) Send(f flat) error {
text := fmt.Sprintf("%v\n%v", f.Price, f.URL())
data := url.Values{"chat_id": {m.ChatID}, "text": {text}}
resp, err := http.PostForm(m.makeURL(), data)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
type errorResponse struct {
ErrorCode int `json:"error_code"`
Description string `json:"description"`
}
errorResp := errorResponse{}
if err := json.Unmarshal(body, &errorResp); err != nil {
return err
}
return fmt.Errorf("Error code: %v, description: %v",
errorResp.ErrorCode, errorResp.Description)
}
return nil
}
func (m *messenger) makeURL() string {
return fmt.Sprintf(
"https://api.telegram.org/bot%v/sendMessage",
m.Token)
}
|