diff --git a/backend/app/notify/telegram.go b/backend/app/notify/telegram.go index 6ae627b2..5139f485 100644 --- a/backend/app/notify/telegram.go +++ b/backend/app/notify/telegram.go @@ -40,6 +40,14 @@ type telegramMsg struct { ParseMode string `json:"parse_mode,omitempty"` } +// TelegramBotInfo structure contains information about telegram bot +type TelegramBotInfo struct { + ID uint64 `json:"id"` + IsBot bool `json:"is_bot"` + FirstName string `json:"first_name"` + Username string `json:"username"` +} + const telegramTimeOut = 5000 * time.Millisecond const telegramAPIPrefix = "https://api.telegram.org/bot" @@ -82,12 +90,7 @@ func NewTelegram(params TelegramParams) (*Telegram, error) { tgResp := struct { OK bool `json:"ok"` - Result struct { - FirstName string `json:"first_name"` - ID uint64 `json:"id"` - IsBot bool `json:"is_bot"` - UserName string `json:"username"` - } + Result TelegramBotInfo }{} if err = json.NewDecoder(resp.Body).Decode(&tgResp); err != nil { @@ -98,7 +101,7 @@ func NewTelegram(params TelegramParams) (*Telegram, error) { return errors.Errorf("unexpected telegram response %+v", tgResp) } - res.BotUsername = tgResp.Result.UserName + res.BotUsername = tgResp.Result.Username return nil }) diff --git a/backend/app/notify/telegram_test.go b/backend/app/notify/telegram_test.go index 115942b4..707b5ac9 100644 --- a/backend/app/notify/telegram_test.go +++ b/backend/app/notify/telegram_test.go @@ -35,7 +35,7 @@ func TestTelegram_New(t *testing.T) { Token: "bad-resp", apiPrefix: ts.URL + "/", }) - assert.EqualError(t, err, "unexpected telegram response {OK:false Result:{FirstName:comments_test ID:707381019 IsBot:false UserName:remark42_test_bot}}") + assert.EqualError(t, err, "unexpected telegram response {OK:false Result:{ID:707381019 IsBot:false FirstName:comments_test Username:remark42_test_bot}}") assert.True(t, time.Since(st) >= 250*5*time.Millisecond) _, err = NewTelegram(TelegramParams{