Skip to content

Commit

Permalink
chore(deps): Change Telegram bot library
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Nov 16, 2024
1 parent 3658733 commit ae936b2
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 34 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
gabe565.com/utils v0.0.0-20241114234101-e128cd3269b5
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/dmarkham/enumer v1.5.10
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/go-telegram/bot v1.10.1
github.com/gotify/server/v2 v2.6.0
github.com/likexian/whois v1.15.5
github.com/likexian/whois-parser v1.24.20
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dmarkham/enumer v1.5.10 h1:ygL0L6quiTiH1jpp68DyvsWaea6MaZLZrTTkIS++R0M=
github.com/dmarkham/enumer v1.5.10/go.mod h1:e4VILe2b1nYK3JKJpRmNdl5xbDQvELc6tQ8b+GsGk6E=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/go-telegram/bot v1.10.1 h1:zwbEjz6ZlqBsyT5EqNqZDYX1ogHIiln1lwYpcK3E8XA=
github.com/go-telegram/bot v1.10.1/go.mod h1:i2TRs7fXWIeaceF3z7KzsMt/he0TwkVC680mvdTFYeM=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gotify/server/v2 v2.6.0 h1:FhWXgJPlY9sNwMOHCVgiR21Af/8iXIl6Lzb3cWBxfgo=
Expand Down
20 changes: 9 additions & 11 deletions internal/domain/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"gabe565.com/domain-watch/internal/config"
"gabe565.com/domain-watch/internal/integration"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/go-telegram/bot"
whoisparser "github.com/likexian/whois-parser"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -71,19 +71,18 @@ func TestDomain_NotifyThreshold(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
telegram := integration.TelegramTestSetup(t)
gotNotify := false
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/bot/sendMessage", r.URL.Path)
assert.Equal(t, "/bot123/sendMessage", r.URL.Path)
gotNotify = true
resp := tgbotapi.APIResponse{
Ok: true,
resp := integration.APIResponse{
OK: true,
Result: json.RawMessage("{}"),
}
assert.NoError(t, json.NewEncoder(w).Encode(&resp))
}))
t.Cleanup(server.Close)
telegram.Bot.SetAPIEndpoint(server.URL + "/bot%s/%s")
telegram := integration.TelegramTestSetup(t, bot.WithServerURL(server.URL))

d := &Domain{
conf: &config.Config{Threshold: []int{1, 7}},
Expand Down Expand Up @@ -145,19 +144,18 @@ func TestDomain_NotifyStatusChange(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
telegram := integration.TelegramTestSetup(t)
gotNotify := false
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/bot/sendMessage", r.URL.Path)
assert.Equal(t, "/bot123/sendMessage", r.URL.Path)
gotNotify = true
resp := tgbotapi.APIResponse{
Ok: true,
resp := integration.APIResponse{
OK: true,
Result: json.RawMessage("{}"),
}
assert.NoError(t, json.NewEncoder(w).Encode(resp))
}))
t.Cleanup(server.Close)
telegram.Bot.SetAPIEndpoint(server.URL + "/bot%s/%s")
telegram := integration.TelegramTestSetup(t, bot.WithServerURL(server.URL))

d := &Domain{
Name: tt.fields.Name,
Expand Down
30 changes: 18 additions & 12 deletions internal/integration/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,51 @@ import (

"gabe565.com/domain-watch/internal/config"
"gabe565.com/domain-watch/internal/util"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/go-telegram/bot"
)

type Telegram struct {
ChatID int64
Bot *tgbotapi.BotAPI
Bot *bot.Bot
}

func (t *Telegram) Setup(_ context.Context, conf *config.Config) error {
func (t *Telegram) Setup(ctx context.Context, conf *config.Config) error {
if t.ChatID = conf.TelegramChat; t.ChatID == 0 {
return fmt.Errorf("telegram %w: chat ID", util.ErrNotConfigured)
}

return t.Login(conf.TelegramToken)
return t.Login(ctx, conf.TelegramToken)
}

func (t *Telegram) Login(token string) error {
func (t *Telegram) Login(ctx context.Context, token string) error {
if token == "" {
return fmt.Errorf("telegram %w: token", util.ErrNotConfigured)
}

var err error
t.Bot, err = tgbotapi.NewBotAPI(token)
t.Bot, err = bot.New(token, bot.WithSkipGetMe())
if err != nil {
return err
}

slog.Info("Connected to Telegram", "username", t.Bot.Self.UserName)
user, err := t.Bot.GetMe(ctx)
if err != nil {
return err
}

slog.Info("Connected to Telegram", "username", user.Username)
return nil
}

func (t *Telegram) Send(_ context.Context, message string) error {
func (t *Telegram) Send(ctx context.Context, message string) error {
if t.Bot == nil {
return nil
}

payload := tgbotapi.NewMessage(t.ChatID, message)
payload.ParseMode = tgbotapi.ModeMarkdown

_, err := t.Bot.Send(payload)
_, err := t.Bot.SendMessage(ctx, &bot.SendMessageParams{
ChatID: t.ChatID,
Text: message,
ParseMode: "markdown",
})
return err
}
34 changes: 26 additions & 8 deletions internal/integration/telegram_test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,53 @@ import (
"net/http/httptest"
"testing"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TelegramTestSetup(t *testing.T) *Telegram {
type APIResponse struct {
OK bool `json:"ok"`
Result json.RawMessage `json:"result,omitempty"`
Description string `json:"description,omitempty"`
ErrorCode int `json:"error_code,omitempty"`
Parameters struct {
RetryAfter int `json:"retry_after,omitempty"`
MigrateToChatID int `json:"migrate_to_chat_id,omitempty"`
} `json:"parameters,omitempty"`
}

func TelegramTestSetup(t *testing.T, opts ...bot.Option) *Telegram {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/bot/getMe" {
if r.URL.Path == "/bot123/getMe" {
var buf bytes.Buffer
u := tgbotapi.User{
u := models.User{
IsBot: true,
FirstName: "Bot",
UserName: "Bot",
Username: "Bot",
CanJoinGroups: true,
}
assert.NoError(t, json.NewEncoder(&buf).Encode(u))

resp := tgbotapi.APIResponse{
Ok: true,
resp := APIResponse{
OK: true,
Result: json.RawMessage(buf.Bytes()),
}
assert.NoError(t, json.NewEncoder(w).Encode(resp))
return
}
w.WriteHeader(http.StatusNotFound)
}))
t.Cleanup(server.Close)

telegram := &Telegram{}
var err error
telegram.Bot, err = tgbotapi.NewBotAPIWithAPIEndpoint("", server.URL+"/bot%s/%s")
telegram.Bot, err = bot.New("123", bot.WithServerURL(server.URL))
require.NoError(t, err)

for _, opt := range opts {
opt(telegram.Bot)
}
return telegram
}

0 comments on commit ae936b2

Please sign in to comment.