Skip to content

Commit

Permalink
Migrate to telegram-bot-api/v5 and built-in slices function
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmz committed Dec 3, 2023
1 parent 7ea87cc commit 6f8de4a
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 45 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.2.1] - 2023-12-03

### Changed

- Migrate to telegram-bot-api/v5

## [1.2.0] - 2023-12-02

### Added
Expand Down
7 changes: 2 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/FreeFeed/freefeed-tg-client/types"
"github.com/bluele/gcache"
"github.com/davidmz/debug-log"
tg "github.com/davidmz/telegram-bot-api"
"github.com/enescakir/emoji"
tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

var internaErrorMsg = emoji.Parse(":stop_sign: An internal error occurred during your request. If it repeats, please contact support.")
Expand Down Expand Up @@ -77,10 +77,7 @@ func (a *App) Start() (err error) {
debugLogger: a.DebugLogger,
})

a.updChannel, err = a.TgAPI.GetUpdatesChan(tg.UpdateConfig{Offset: 0, Timeout: 60})
if err != nil {
return
}
a.updChannel = a.TgAPI.GetUpdatesChan(tg.UpdateConfig{Offset: 0, Timeout: 60})

a.waitGroup.Add(1)
a.DebugLogger.Println("▶️ Starting Telegram listener")
Expand Down
2 changes: 1 addition & 1 deletion chat/buttons.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"

"github.com/FreeFeed/freefeed-tg-client/frf"
tg "github.com/davidmz/telegram-bot-api"
"github.com/enescakir/emoji"
tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/gofrs/uuid"
"golang.org/x/text/message"
)
Expand Down
7 changes: 1 addition & 6 deletions chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/FreeFeed/freefeed-tg-client/frf"
"github.com/FreeFeed/freefeed-tg-client/store"
"github.com/davidmz/debug-log"
tg "github.com/davidmz/telegram-bot-api"
"github.com/enescakir/emoji"
tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

type Chat struct {
Expand Down Expand Up @@ -61,11 +61,6 @@ func (c *Chat) ShouldSendAndSave(msg tg.Chattable, rec store.SentMsgRec) (tg.Mes
return m.(tg.Message), err
}

func (c *Chat) ShouldAnswer(config tg.CallbackConfig) (tg.APIResponse, error) {
resp, err := c.Should(c.App.Tg().AnswerCallbackQuery(config))
return resp.(tg.APIResponse), err
}

func (c *Chat) newHTMLMessage(text string) *tg.MessageConfig {
return c.newRawHTMLMessage(c.App.Linkify(emoji.Parse(text)))
}
Expand Down
22 changes: 11 additions & 11 deletions chat/handle-callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/FreeFeed/freefeed-tg-client/store"
"github.com/FreeFeed/freefeed-tg-client/types"
"github.com/davidmz/go-try"
tg "github.com/davidmz/telegram-bot-api"
"github.com/enescakir/emoji"
tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/gofrs/uuid"
"golang.org/x/text/language"
"golang.org/x/text/message"
Expand Down Expand Up @@ -36,7 +36,7 @@ func (c *Chat) handleCallback(update tg.Update) {

p := message.NewPrinter(c.State.Language)

c.ShouldAnswer(tg.CallbackConfig{
c.ShouldSend(tg.CallbackConfig{
CallbackQueryID: cbQuery.ID,
Text: p.Sprintf("Language is %v now", c.State.Language),
})
Expand All @@ -55,7 +55,7 @@ func (c *Chat) handleCallback(update tg.Update) {
if errors.Is(err, store.ErrNotFound) {
text = emoji.Parse(p.Sprintf(":warning: Cannot load event data, probably this message is too old"))
}
c.ShouldAnswer(tg.CallbackConfig{
c.ShouldSend(tg.CallbackConfig{
CallbackQueryID: cbQuery.ID,
Text: text,
})
Expand All @@ -65,7 +65,7 @@ func (c *Chat) handleCallback(update tg.Update) {
event := eventRec.Event

if err := c.ShouldOK(event.LoadPost(c.frfAPI())); err != nil {
c.ShouldAnswer(tg.CallbackConfig{
c.ShouldSend(tg.CallbackConfig{
CallbackQueryID: cbQuery.ID,
Text: emoji.Parse(p.Sprintf(":warning: FreeFeed error: %v", err)),
})
Expand All @@ -84,7 +84,7 @@ func (c *Chat) handleCallback(update tg.Update) {
}
c.saveState()
c.App.PauseEvents(c.ID)
c.ShouldAnswer(tg.CallbackConfig{CallbackQueryID: cbQuery.ID})
c.ShouldSend(tg.CallbackConfig{CallbackQueryID: cbQuery.ID})
} else if cbData == doAcceptRequest || cbData == doRejectRequest {
var err error
if event.Group == nil {
Expand All @@ -101,7 +101,7 @@ func (c *Chat) handleCallback(update tg.Update) {
}
}
if err != nil {
c.ShouldAnswer(tg.CallbackConfig{
c.ShouldSend(tg.CallbackConfig{
CallbackQueryID: cbQuery.ID,
Text: emoji.Parse(p.Sprintf(":warning: FreeFeed error: %v", err)),
})
Expand Down Expand Up @@ -142,7 +142,7 @@ func (c *Chat) handleCallback(update tg.Update) {
})()
if err != nil {
c.errorLog().Print(err)
c.ShouldAnswer(tg.CallbackConfig{
c.ShouldSend(tg.CallbackConfig{
CallbackQueryID: cbQuery.ID,
Text: emoji.Parse(p.Sprintf(":warning: Error: %v", err)),
})
Expand All @@ -159,7 +159,7 @@ func (c *Chat) handleCallback(update tg.Update) {
err = c.frfAPI().UnlikeComment(event.CommentID)
}
if err != nil {
c.ShouldAnswer(tg.CallbackConfig{
c.ShouldSend(tg.CallbackConfig{
CallbackQueryID: cbQuery.ID,
Text: emoji.Parse(p.Sprintf(":warning: FreeFeed error: %v", err)),
})
Expand All @@ -172,7 +172,7 @@ func (c *Chat) handleCallback(update tg.Update) {
c.ShouldSend(msg)

} else {
c.ShouldAnswer(tg.CallbackConfig{
c.ShouldSend(tg.CallbackConfig{
CallbackQueryID: cbQuery.ID,
Text: emoji.Parse(p.Sprintf(":alien: Unknown command %v", cbQuery.Data)),
})
Expand All @@ -182,14 +182,14 @@ func (c *Chat) handleCallback(update tg.Update) {
c.State.ClearExpectations()
c.saveState()
c.App.ResumeEvents(c.ID)
c.ShouldAnswer(tg.CallbackConfig{
c.ShouldSend(tg.CallbackConfig{
CallbackQueryID: cbQuery.ID,
Text: p.Sprintf("Action is cancelled"),
})
} else {
// Unknown or irrelevant command
c.errorLog().Printf("Unknown callback data received: %v", cbQuery.Data)
c.ShouldAnswer(tg.CallbackConfig{
c.ShouldSend(tg.CallbackConfig{
CallbackQueryID: cbQuery.ID,
Text: emoji.Parse(p.Sprintf(":alien: Unknown command %v", cbQuery.Data)),
})
Expand Down
2 changes: 1 addition & 1 deletion chat/handle-command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package chat

import (
"github.com/FreeFeed/freefeed-tg-client/store"
tg "github.com/davidmz/telegram-bot-api"
tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"golang.org/x/text/message"
)

Expand Down
4 changes: 2 additions & 2 deletions chat/handle-message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package chat

import (
"github.com/FreeFeed/freefeed-tg-client/store"
tg "github.com/davidmz/telegram-bot-api"
tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/gofrs/uuid"
"github.com/golang-jwt/jwt/v4"
"golang.org/x/text/message"
Expand All @@ -26,7 +26,7 @@ func (c *Chat) handleMessage(update tg.Update) {
}

// Delete message with the token for safety
c.Should(c.App.Tg().DeleteMessage(tg.DeleteMessageConfig{
c.Should(c.App.Tg().Request(tg.DeleteMessageConfig{
ChatID: c.ID,
MessageID: msg.MessageID,
}))
Expand Down
2 changes: 1 addition & 1 deletion chat/handle-update.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package chat

import tg "github.com/davidmz/telegram-bot-api"
import tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"

func (c *Chat) HandleUpdate(update tg.Update) {
c.handleMessage(update)
Expand Down
2 changes: 1 addition & 1 deletion chat/print-expectation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package chat

import (
"github.com/FreeFeed/freefeed-tg-client/store"
tg "github.com/davidmz/telegram-bot-api"
"github.com/enescakir/emoji"
tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"golang.org/x/text/message"
)

Expand Down
2 changes: 1 addition & 1 deletion chat/process-event.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/FreeFeed/freefeed-tg-client/frf"
"github.com/FreeFeed/freefeed-tg-client/store"
tg "github.com/davidmz/telegram-bot-api"
tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/gofrs/uuid"
"golang.org/x/text/message"
)
Expand Down
2 changes: 1 addition & 1 deletion chat/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/FreeFeed/freefeed-tg-client/store"
"github.com/FreeFeed/freefeed-tg-client/types"
"github.com/davidmz/debug-log"
tg "github.com/davidmz/telegram-bot-api"
tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

type ID = types.TgChatID
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ require (
github.com/bluele/gcache v0.0.2
github.com/davidmz/debug-log v1.2.0
github.com/davidmz/go-try v0.1.5
github.com/davidmz/sliceutil v1.2.0
github.com/davidmz/telegram-bot-api v5.0.2-fork+incompatible
github.com/enescakir/emoji v1.0.0
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/gobwas/ws v1.3.1
github.com/gofrs/uuid v4.4.0+incompatible
github.com/golang-jwt/jwt/v4 v4.5.0
Expand All @@ -22,7 +21,6 @@ require (
github.com/gobwas/pool v0.2.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
golang.org/x/sys v0.15.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ github.com/davidmz/debug-log v1.2.0 h1:7dYMJ2oLGjLXTg/wD9UpG1Ae3BPlEHZrt4F+jlqqx
github.com/davidmz/debug-log v1.2.0/go.mod h1:dhNuGbwoCq6gVfwrt+/TlKs7YQMJdAyrdlmX/0YXwj4=
github.com/davidmz/go-try v0.1.5 h1:tylL42whKswcvmG9M4uM2DVRN0YJLAxP4nRylDtfN0M=
github.com/davidmz/go-try v0.1.5/go.mod h1:OOEBam4PN+vojywL/ZkHo0q4AoQsLeOtTgt7GDS0wYs=
github.com/davidmz/sliceutil v1.2.0 h1:rLTP0kJf7jxYkDB2qsblRM0AZzakTEALnkyQbit1dYs=
github.com/davidmz/sliceutil v1.2.0/go.mod h1:7UYHGgafAEi0muw1W0UGRzfG0To64rJAzXFHTMkM8zk=
github.com/davidmz/telegram-bot-api v5.0.2-fork+incompatible h1:ludjXgKEbl4JkaWPDsZ6zbQPYi3PJvpQa7yWdz+QpJ4=
github.com/davidmz/telegram-bot-api v5.0.2-fork+incompatible/go.mod h1:5jwq2lbK6nddxOsCSMaSkkTik4DTUT036GxIjgC+ySc=
github.com/enescakir/emoji v1.0.0 h1:W+HsNql8swfCQFtioDGDHCHri8nudlK1n5p2rHCJoog=
github.com/enescakir/emoji v1.0.0/go.mod h1:Bt1EKuLnKDTYpLALApstIkAjdDrS/8IAgTkKp+WKFD0=
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/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
Expand All @@ -40,8 +38,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/FreeFeed/freefeed-tg-client/store"
"github.com/davidmz/debug-log"
"github.com/davidmz/go-try"
tgbotapi "github.com/davidmz/telegram-bot-api"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

const (
Expand Down
13 changes: 8 additions & 5 deletions store/fs-tracked-posts.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package store

import (
"slices"

"github.com/FreeFeed/freefeed-tg-client/types"
"github.com/davidmz/sliceutil"
"github.com/gofrs/uuid"
)

Expand All @@ -26,8 +27,9 @@ func (s *fsStore) TrackPost(chatID types.TgChatID, postID uuid.UUID) error {
trackedPostsFile,
&tracked,
func() error {
tracked.PostIDs = append(tracked.PostIDs, postID)
sliceutil.Unique(&tracked.PostIDs)
if !slices.Contains(tracked.PostIDs, postID) {
tracked.PostIDs = append(tracked.PostIDs, postID)
}
return nil
},
)
Expand All @@ -40,8 +42,9 @@ func (s *fsStore) UntrackPost(chatID types.TgChatID, postID uuid.UUID) error {
trackedPostsFile,
&tracked,
func() error {
sliceutil.Filter(&tracked.PostIDs,
func(i int) bool { return tracked.PostIDs[i] != postID })
if idx := slices.Index(tracked.PostIDs, postID); idx >= 0 {
slices.Delete(tracked.PostIDs, idx, idx+1)
}
return nil
},
)
Expand Down

0 comments on commit 6f8de4a

Please sign in to comment.