diff options
Diffstat (limited to 'messenger.go')
-rw-r--r-- | messenger.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/messenger.go b/messenger.go new file mode 100644 index 0000000..127ebc2 --- /dev/null +++ b/messenger.go @@ -0,0 +1,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) +} |