Skip to content

Commit

Permalink
Fix few bugs with notification and chatid migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Civil committed Oct 16, 2023
1 parent c3c889f commit cfb3200
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
37 changes: 26 additions & 11 deletions endpoints/telegram/telegram_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ package telegram

import (
"fmt"
"github.com/Civil/github2telegram/configs"
"github.com/Civil/github2telegram/db"
"github.com/Civil/github2telegram/endpoints"
"github.com/Civil/github2telegram/feeds"
"github.com/Civil/github2telegram/types"
"github.com/lomik/zapwriter"
"github.com/mymmrac/telego"
tu "github.com/mymmrac/telego/telegoutil"
"github.com/pkg/errors"
"go.uber.org/zap"
"net/http"
"regexp"
"strconv"
"strings"
"time"

"github.com/lomik/zapwriter"
"github.com/mymmrac/telego"
tu "github.com/mymmrac/telego/telegoutil"
"github.com/pkg/errors"
"go.uber.org/zap"

"github.com/Civil/github2telegram/configs"
"github.com/Civil/github2telegram/db"
"github.com/Civil/github2telegram/endpoints"
"github.com/Civil/github2telegram/feeds"
"github.com/Civil/github2telegram/types"
)

const (
Expand Down Expand Up @@ -287,7 +289,7 @@ func (e *TelegramEndpoint) Send(url, filter, message string) error {

func (e *TelegramEndpoint) checkAndChangeChatID(logger *zap.Logger, id int64, error string) int64 {
if strings.Contains(error, "migrate to chat ID:") {
re := regexp.MustCompile(`migrate\s+to\s+chat\s+ID:\s([-0-9]+).`)
re := regexp.MustCompile(`migrate\s+to\s+chat\s+ID:\s([-0-9]+)`)
matches := re.FindStringSubmatch(error)
if len(matches) != 2 {
logger.Error("failed to parse new chat id, either no matches or too many matches found",
Expand All @@ -305,6 +307,14 @@ func (e *TelegramEndpoint) checkAndChangeChatID(logger *zap.Logger, id int64, er
)
return id
}
if newID == 0 {
logger.Error("failed to parse new chat id",
zap.String("newIDStr", newIDStr),
zap.String("original_error", error),
zap.Any("matches", matches),
)
return id
}
err = e.db.UpdateChatID(id, newID)
if err != nil {
logger.Error("failed to update chat id",
Expand Down Expand Up @@ -546,6 +556,11 @@ func (e *TelegramEndpoint) handlerSubscribe(tokens []string, update *telego.Upda
}

chatID := update.Message.Chat.ID
if chatID == 0 {
logger.Error("chat id is 0, that shouldn't happen", zap.Any("update", update))
_ = e.sendMessage(update.Message.Chat.ID, update.Message.MessageID, "failed to subscribe as bot cannot determine chat_id, please try again later")
return errors.New("cannot detect chat_id, subscription failed")
}
err := e.db.AddSubscribtion(TelegramEndpointName, url, filterName, chatID)
if err != nil {
if errors.Is(err, db.ErrAlreadyExists) {
Expand Down
17 changes: 9 additions & 8 deletions feeds/process.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package feeds

import (
"github.com/Civil/github2telegram/types"
"math/rand"
"regexp"
"strings"
"time"

"github.com/Civil/github2telegram/configs"
"github.com/Civil/github2telegram/db"
"github.com/Civil/github2telegram/types"

"github.com/pkg/errors"

"math/rand"
"strings"
"github.com/Civil/github2telegram/configs"
"github.com/Civil/github2telegram/db"

"github.com/lomik/zapwriter"
"github.com/lunny/html2md"
Expand Down Expand Up @@ -193,17 +194,17 @@ func (f *Feed) processSingleItem(cfg *configs.FeedsConfig, url string, item *gof
logger.Debug("filter matched")
contentTruncated := false
var changeType UpdateType
var notification string

// check if last tag haven't changed
if item.Title == cfg.Filters[i].LastTag {
changeType = DescriptionChange
notification = types.MdReplacer.Replace(cfg.Repo) + " description changed: " + item.Title + "\nLink: " + types.MdReplacer.Replace(item.Link)

} else {
changeType = NewRelease
notification = types.MdReplacer.Replace(cfg.Repo) + " tagged: " + types.MdReplacer.Replace(item.Title) + "\nLink: " + types.MdReplacer.Replace(item.Link)
}

notification := types.MdReplacer.Replace(cfg.Repo) + changeType.String() + types.MdReplacer.Replace(item.Title) + "\nLink: " + types.MdReplacer.Replace(item.Link)

content := html2md.Convert(item.Content)
if len(content) > 250 {
content = content[:250] + "\\.\\.\\."
Expand Down
13 changes: 13 additions & 0 deletions feeds/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ const (
DescriptionChange
)

func (t UpdateType) String() string {
switch t {
case NewRelease:
return " tagged: "
case Retag:
return " re-tagged: "
case DescriptionChange:
return " description changed: "
default:
return " (unhandled update type): "
}
}

type Update struct {
Type UpdateType
Repo string
Expand Down

0 comments on commit cfb3200

Please sign in to comment.