| 1 | package telegramscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/ |
| 2 | |
| 3 | import ( |
| 4 | "encoding/jsonscip-go gomod github.com/golang/go/src . `encoding/json`/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src . fmt/" |
| 6 | "net/httpscip-go gomod github.com/golang/go/src . `net/http`/" |
| 7 | |
| 8 | "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/" |
| 9 | "github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/" |
| 10 | ) |
| 11 | |
| 12 | // Client for Telegram API. |
| 13 | type Clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client# struct { |
| 14 | tokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#token. string |
| 15 | httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#httpClient. typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient# |
| 16 | } |
| 17 | |
| 18 | // GetBotInfo returns the bot User info. |
| 19 | func (c *Clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#) GetBotInfoscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#GetBotInfo().() (*Userscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/User#, error) { |
| 20 | response := &userResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/userResponse#{} |
| 21 | jc := jsonclientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/.NewWithHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/NewWithHTTPClient().(c.httpClientOrDefaultscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#httpClientOrDefault().()) |
| 22 | err := jc.Getscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/Client#Get.(c.apiURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#apiURL().("getMe"), response) |
| 23 | |
| 24 | if !response.OKscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/userResponse#OK. { |
| 25 | return nil, GetErrorResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/GetErrorResponse().(jsonclientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/.ErrorBodyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/ErrorBody().(err)) |
| 26 | } |
| 27 | |
| 28 | return &response.Resultscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/userResponse#Result., nil |
| 29 | } |
| 30 | |
| 31 | // GetUpdates retrieves the latest updates. |
| 32 | func (c *Clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#) GetUpdatesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#GetUpdates().( |
| 33 | offset int, |
| 34 | limit int, |
| 35 | timeout int, |
| 36 | allowedUpdates []string, |
| 37 | ) ([]Updatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Update#, error) { |
| 38 | request := &updatesRequestscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/updatesRequest#{ |
| 39 | Offsetscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/updatesRequest#Offset.: offset, |
| 40 | Limitscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/updatesRequest#Limit.: limit, |
| 41 | Timeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/updatesRequest#Timeout.: timeout, |
| 42 | AllowedUpdatesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/updatesRequest#AllowedUpdates.: allowedUpdates, |
| 43 | } |
| 44 | response := &updatesResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/updatesResponse#{} |
| 45 | jc := jsonclientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/.NewWithHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/NewWithHTTPClient().(c.httpClientOrDefaultscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#httpClientOrDefault().()) |
| 46 | err := jc.Postscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/Client#Post.(c.apiURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#apiURL().("getUpdates"), request, response) |
| 47 | |
| 48 | if !response.OKscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/updatesResponse#OK. { |
| 49 | return nil, GetErrorResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/GetErrorResponse().(jsonclientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/.ErrorBodyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/ErrorBody().(err)) |
| 50 | } |
| 51 | |
| 52 | return response.Resultscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/updatesResponse#Result., nil |
| 53 | } |
| 54 | |
| 55 | // SendMessage sends the specified Message. |
| 56 | func (c *Clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#) SendMessagescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#SendMessage().(message *SendMessagePayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/SendMessagePayload#) (*Messagescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Message#, error) { |
| 57 | response := &messageResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/messageResponse#{} |
| 58 | jc := jsonclientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/.NewWithHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/NewWithHTTPClient().(c.httpClientOrDefaultscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#httpClientOrDefault().()) |
| 59 | err := jc.Postscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/Client#Post.(c.apiURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#apiURL().("sendMessage"), message, response) |
| 60 | |
| 61 | if !response.OKscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/messageResponse#OK. { |
| 62 | return nil, GetErrorResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/GetErrorResponse().(jsonclientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/.ErrorBodyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient`/ErrorBody().(err)) |
| 63 | } |
| 64 | |
| 65 | return response.Resultscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/messageResponse#Result., nil |
| 66 | } |
| 67 | |
| 68 | func (c *Clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#) apiURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#apiURL().(endpoint string) string { |
| 69 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Sprintfscip-go gomod github.com/golang/go/src . fmt/Sprintf().(apiFormatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/apiFormat., c.tokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#token., endpoint) |
| 70 | } |
| 71 | |
| 72 | // httpClientOrDefault returns the injected client or a default Client. |
| 73 | func (c *Clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#) httpClientOrDefaultscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#httpClientOrDefault().() typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient# { |
| 74 | if c.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#httpClient. != nil { |
| 75 | return c.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Client#httpClient. |
| 76 | } |
| 77 | |
| 78 | return &httpscip-go gomod github.com/golang/go/src . `net/http`/.Clientscip-go gomod github.com/golang/go/src . `net/http`/Client#{Timeoutscip-go gomod github.com/golang/go/src . `net/http`/Client#Timeout.: defaultHTTPTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/defaultHTTPTimeout.} |
| 79 | } |
| 80 | |
| 81 | // GetErrorResponse retrieves the error message from a failed request. |
| 82 | func GetErrorResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/GetErrorResponse().(body string) error { |
| 83 | response := &responseErrorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/responseError#{} |
| 84 | if err := jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src . `encoding/json`/Unmarshal().([]byte(body), response); err == nil { |
| 85 | return response |
| 86 | } |
| 87 | |
| 88 | return nil |
| 89 | } |