Skip to content

Commit cfb3200

Browse files
author
Vladimir Smirnov
committed
Fix few bugs with notification and chatid migrations
1 parent c3c889f commit cfb3200

3 files changed

Lines changed: 48 additions & 19 deletions

File tree

endpoints/telegram/telegram_endpoint.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@ package telegram
22

33
import (
44
"fmt"
5-
"github.com/Civil/github2telegram/configs"
6-
"github.com/Civil/github2telegram/db"
7-
"github.com/Civil/github2telegram/endpoints"
8-
"github.com/Civil/github2telegram/feeds"
9-
"github.com/Civil/github2telegram/types"
10-
"github.com/lomik/zapwriter"
11-
"github.com/mymmrac/telego"
12-
tu "github.com/mymmrac/telego/telegoutil"
13-
"github.com/pkg/errors"
14-
"go.uber.org/zap"
155
"net/http"
166
"regexp"
177
"strconv"
188
"strings"
199
"time"
10+
11+
"github.com/lomik/zapwriter"
12+
"github.com/mymmrac/telego"
13+
tu "github.com/mymmrac/telego/telegoutil"
14+
"github.com/pkg/errors"
15+
"go.uber.org/zap"
16+
17+
"github.com/Civil/github2telegram/configs"
18+
"github.com/Civil/github2telegram/db"
19+
"github.com/Civil/github2telegram/endpoints"
20+
"github.com/Civil/github2telegram/feeds"
21+
"github.com/Civil/github2telegram/types"
2022
)
2123

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

288290
func (e *TelegramEndpoint) checkAndChangeChatID(logger *zap.Logger, id int64, error string) int64 {
289291
if strings.Contains(error, "migrate to chat ID:") {
290-
re := regexp.MustCompile(`migrate\s+to\s+chat\s+ID:\s([-0-9]+).`)
292+
re := regexp.MustCompile(`migrate\s+to\s+chat\s+ID:\s([-0-9]+)`)
291293
matches := re.FindStringSubmatch(error)
292294
if len(matches) != 2 {
293295
logger.Error("failed to parse new chat id, either no matches or too many matches found",
@@ -305,6 +307,14 @@ func (e *TelegramEndpoint) checkAndChangeChatID(logger *zap.Logger, id int64, er
305307
)
306308
return id
307309
}
310+
if newID == 0 {
311+
logger.Error("failed to parse new chat id",
312+
zap.String("newIDStr", newIDStr),
313+
zap.String("original_error", error),
314+
zap.Any("matches", matches),
315+
)
316+
return id
317+
}
308318
err = e.db.UpdateChatID(id, newID)
309319
if err != nil {
310320
logger.Error("failed to update chat id",
@@ -546,6 +556,11 @@ func (e *TelegramEndpoint) handlerSubscribe(tokens []string, update *telego.Upda
546556
}
547557

548558
chatID := update.Message.Chat.ID
559+
if chatID == 0 {
560+
logger.Error("chat id is 0, that shouldn't happen", zap.Any("update", update))
561+
_ = e.sendMessage(update.Message.Chat.ID, update.Message.MessageID, "failed to subscribe as bot cannot determine chat_id, please try again later")
562+
return errors.New("cannot detect chat_id, subscription failed")
563+
}
549564
err := e.db.AddSubscribtion(TelegramEndpointName, url, filterName, chatID)
550565
if err != nil {
551566
if errors.Is(err, db.ErrAlreadyExists) {

feeds/process.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package feeds
22

33
import (
4-
"github.com/Civil/github2telegram/types"
4+
"math/rand"
55
"regexp"
6+
"strings"
67
"time"
78

8-
"github.com/Civil/github2telegram/configs"
9-
"github.com/Civil/github2telegram/db"
9+
"github.com/Civil/github2telegram/types"
10+
1011
"github.com/pkg/errors"
1112

12-
"math/rand"
13-
"strings"
13+
"github.com/Civil/github2telegram/configs"
14+
"github.com/Civil/github2telegram/db"
1415

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

198198
// check if last tag haven't changed
199199
if item.Title == cfg.Filters[i].LastTag {
200200
changeType = DescriptionChange
201-
notification = types.MdReplacer.Replace(cfg.Repo) + " description changed: " + item.Title + "\nLink: " + types.MdReplacer.Replace(item.Link)
201+
202202
} else {
203203
changeType = NewRelease
204-
notification = types.MdReplacer.Replace(cfg.Repo) + " tagged: " + types.MdReplacer.Replace(item.Title) + "\nLink: " + types.MdReplacer.Replace(item.Link)
205204
}
206205

206+
notification := types.MdReplacer.Replace(cfg.Repo) + changeType.String() + types.MdReplacer.Replace(item.Title) + "\nLink: " + types.MdReplacer.Replace(item.Link)
207+
207208
content := html2md.Convert(item.Content)
208209
if len(content) > 250 {
209210
content = content[:250] + "\\.\\.\\."

feeds/types.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ const (
88
DescriptionChange
99
)
1010

11+
func (t UpdateType) String() string {
12+
switch t {
13+
case NewRelease:
14+
return " tagged: "
15+
case Retag:
16+
return " re-tagged: "
17+
case DescriptionChange:
18+
return " description changed: "
19+
default:
20+
return " (unhandled update type): "
21+
}
22+
}
23+
1124
type Update struct {
1225
Type UpdateType
1326
Repo string

0 commit comments

Comments
 (0)