@@ -2,21 +2,23 @@ package telegram
22
33import (
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
2224const (
@@ -287,7 +289,7 @@ func (e *TelegramEndpoint) Send(url, filter, message string) error {
287289
288290func (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 ) {
0 commit comments