extract TelegramBotInfo structure
This commit is contained in:
committed by
Umputun
parent
ca7cbedea4
commit
8fc9141f19
@@ -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
|
||||
})
|
||||
|
||||
|
||||
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user