From 105f5017c35f92a9e2f5398a06cfdd3f1da31bad Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Sat, 4 Dec 2021 13:10:54 -0500 Subject: [PATCH] Migrate to TDlib 1.7.9 --- README.md | 7 +-- go.mod | 1 + go.sum | 2 + telegram/cache/cache.go | 12 +++--- telegram/client.go | 2 +- telegram/commands.go | 86 ++++++++++++++++++++----------------- telegram/handlers.go | 46 ++++++++++---------- telegram/utils.go | 95 ++++++++++++++++++++++++----------------- 8 files changed, 139 insertions(+), 112 deletions(-) diff --git a/README.md b/README.md index fa09904..4a73798 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,7 @@ The configuration file is compatible with Zhabogram 2.0, so you can easily copy Prerequisites: Go links the binary against the `glibc` version present in the system where the build process happens, so it should be less or equal to the version of `glibc` in the system where Telegabber will run. -* Build TDLib according to [TDLib build instructions](https://tdlib.github.io/td/build.html). The 1.5.0 version is needed currently; for switching to it, run this after `cd td`: - -``` -git fetch --all --tags --prune -git checkout tags/v1.5.0 -``` +* Build TDLib according to [TDLib build instructions](https://tdlib.github.io/td/build.html). Roll back to commit 8d7bda00a535d1eda684c3c8802e85d69c89a14a if the compatibility got broken. * Install Go (tested with 1.13, but may work with earlier versions too). * Open the source dir in a new shell (to make sure that `$GOPATH` works) and run `make`. Dependencies will be installed automatically. diff --git a/go.mod b/go.mod index 7f0bbc9..386d904 100644 --- a/go.mod +++ b/go.mod @@ -14,3 +14,4 @@ require ( ) replace gosrc.io/xmpp => github.com/bodqhrohro/go-xmpp v0.1.4-0.20191106203535-f3b463f3b26c +replace github.com/zelenin/go-tdlib => github.com/godcong/go-tdlib v0.4.4-0.20211203152853-64d22ab8d4ac diff --git a/go.sum b/go.sum index a6dda34..3e17ff1 100644 --- a/go.sum +++ b/go.sum @@ -28,6 +28,8 @@ github.com/go-interpreter/wagon v0.6.0/go.mod h1:5+b/MBYkclRZngKF5s6qrgWxSLgE9F5 github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM= +github.com/godcong/go-tdlib v0.4.4-0.20211203152853-64d22ab8d4ac h1:5FQGW4yHSkbwm+4i/8ef7FvkIFt4NOM4HexSbvPduRo= +github.com/godcong/go-tdlib v0.4.4-0.20211203152853-64d22ab8d4ac/go.mod h1:Xs8fXbk5n7VaPyrSs9DP7QYoBScWYsjX+lUcWmx1DIU= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= diff --git a/telegram/cache/cache.go b/telegram/cache/cache.go index b799709..3d9608d 100644 --- a/telegram/cache/cache.go +++ b/telegram/cache/cache.go @@ -17,7 +17,7 @@ type Status struct { // a thread-safe manner type Cache struct { chats map[int64]*client.Chat - users map[int32]*client.User + users map[int64]*client.User statuses map[int64]*Status chatsLock sync.Mutex usersLock sync.Mutex @@ -28,7 +28,7 @@ type Cache struct { func NewCache() *Cache { return &Cache{ chats: map[int64]*client.Chat{}, - users: map[int32]*client.User{}, + users: map[int64]*client.User{}, statuses: map[int64]*Status{}, } } @@ -48,11 +48,11 @@ func (cache *Cache) ChatsKeys() []int64 { // UsersKeys grabs user ids synchronously to avoid lockups // while they are used -func (cache *Cache) UsersKeys() []int32 { +func (cache *Cache) UsersKeys() []int64 { cache.usersLock.Lock() defer cache.usersLock.Unlock() - var keys []int32 + var keys []int64 for id := range cache.users { keys = append(keys, id) } @@ -89,7 +89,7 @@ func (cache *Cache) GetChat(id int64) (*client.Chat, bool) { } // GetUser retrieves user by id if it's present in the cache -func (cache *Cache) GetUser(id int32) (*client.User, bool) { +func (cache *Cache) GetUser(id int64) (*client.User, bool) { cache.usersLock.Lock() defer cache.usersLock.Unlock() @@ -115,7 +115,7 @@ func (cache *Cache) SetChat(id int64, chat *client.Chat) { } // SetUser stores a user in the cache -func (cache *Cache) SetUser(id int32, user *client.User) { +func (cache *Cache) SetUser(id int64, user *client.User) { cache.usersLock.Lock() defer cache.usersLock.Unlock() diff --git a/telegram/client.go b/telegram/client.go index 2b122f8..07603b5 100644 --- a/telegram/client.go +++ b/telegram/client.go @@ -87,7 +87,7 @@ func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component UseMessageDatabase: true, UseSecretChats: conf.Tdlib.Client.UseSecretChats, - ApiId: int32(apiID), + ApiID: int64(apiID), ApiHash: conf.Tdlib.Client.APIHash, SystemLanguageCode: "en", diff --git a/telegram/commands.go b/telegram/commands.go index 558652d..58c0e17 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -3,7 +3,6 @@ package telegram import ( "fmt" "github.com/pkg/errors" - "math" "regexp" "strconv" "strings" @@ -134,8 +133,8 @@ func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message) { } } -func (c *Client) usernameOrIDToID(username string) (int32, error) { - userID, err := strconv.ParseInt(username, 10, 32) +func (c *Client) usernameOrIDToID(username string) (int64, error) { + userID, err := strconv.ParseInt(username, 10, 64) // couldn't parse the id, try to lookup as a username if err != nil { chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{ @@ -145,13 +144,13 @@ func (c *Client) usernameOrIDToID(username string) (int32, error) { return 0, err } - userID = chat.Id - if userID <= 0 || userID > math.MaxInt32 { + userID = chat.ID + if userID <= 0 { return 0, errors.New("Not a user") } } - return int32(userID), nil + return userID, nil } // ProcessTransportCommand executes a command sent directly to the component @@ -345,10 +344,10 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) } messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{ - ChatId: chatID, - Limit: limit, - SenderUserId: c.me.Id, - Filter: &client.SearchMessagesFilterEmpty{}, + ChatID: chatID, + Limit: limit, + Sender: &client.MessageSenderUser{UserID: c.me.ID}, + Filter: &client.SearchMessagesFilterEmpty{}, }) if err != nil { return err.Error(), true @@ -358,13 +357,13 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) var messageIds []int64 for _, message := range messages.Messages { if message != nil { - messageIds = append(messageIds, message.Id) + messageIds = append(messageIds, message.ID) } } _, err = c.client.DeleteMessages(&client.DeleteMessagesRequest{ - ChatId: chatID, - MessageIds: messageIds, + ChatID: chatID, + MessageIDs: messageIds, Revoke: true, }) if err != nil { @@ -384,10 +383,10 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) } messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{ - ChatId: chatID, - Limit: 1, - SenderUserId: c.me.Id, - Filter: &client.SearchMessagesFilterEmpty{}, + ChatID: chatID, + Limit: 1, + Sender: &client.MessageSenderUser{UserID: c.me.ID}, + Filter: &client.SearchMessagesFilterEmpty{}, }) if err != nil { return err.Error(), true @@ -407,7 +406,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) } text := regex.ReplaceAllString(messageText.Text.Text, strings.Join(args[1:], " ")) - c.ProcessOutgoingMessage(chatID, text, message.Id, "") + c.ProcessOutgoingMessage(chatID, text, message.ID, "") // add @contact case "add": if len(args) < 1 { @@ -427,7 +426,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) gateway.SendPresence( c.xmpp, c.jid, - gateway.SPFrom(strconv.FormatInt(chat.Id, 10)), + gateway.SPFrom(strconv.FormatInt(chat.ID, 10)), gateway.SPType("subscribe"), ) // join https://t.me/publichat @@ -477,7 +476,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) } _, err = c.client.CreateNewSecretChat(&client.CreateNewSecretChatRequest{ - UserId: int32(chatID), + UserID: chatID, }) if err != nil { return err.Error(), true @@ -490,7 +489,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) if chatID > 0 { _, err := c.client.CreateNewBasicGroupChat(&client.CreateNewBasicGroupChatRequest{ - UserIds: []int32{int32(chatID)}, + UserIDs: []int64{chatID}, Title: args[0], }) if err != nil { @@ -500,8 +499,9 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) // blacklists current user case "block": if chatID > 0 { - _, err := c.client.BlockUser(&client.BlockUserRequest{ - UserId: int32(chatID), + _, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{ + Sender: &client.MessageSenderUser{UserID: chatID}, + IsBlocked: true, }) if err != nil { return err.Error(), true @@ -510,8 +510,9 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) // unblacklists current user case "unblock": if chatID > 0 { - _, err := c.client.UnblockUser(&client.UnblockUserRequest{ - UserId: int32(chatID), + _, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{ + Sender: &client.MessageSenderUser{UserID: chatID}, + IsBlocked: false, }) if err != nil { return err.Error(), true @@ -530,8 +531,8 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) } _, err = c.client.AddChatMember(&client.AddChatMemberRequest{ - ChatId: chatID, - UserId: userID, + ChatID: chatID, + UserID: userID, }) if err != nil { return err.Error(), true @@ -550,8 +551,8 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) } _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ - ChatId: chatID, - UserId: userID, + ChatID: chatID, + UserID: userID, Status: &client.ChatMemberStatusLeft{}, }) if err != nil { @@ -579,8 +580,8 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) } _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ - ChatId: chatID, - UserId: userID, + ChatID: chatID, + UserID: userID, Status: &client.ChatMemberStatusBanned{ BannedUntilDate: until, }, @@ -599,7 +600,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) chatType := chat.Type.ChatTypeType() if chatType == client.TypeChatTypeBasicGroup || chatType == client.TypeChatTypeSupergroup { _, err = c.client.LeaveChat(&client.LeaveChatRequest{ - ChatId: chatID, + ChatID: chatID, }) if err != nil { return err.Error(), true @@ -618,7 +619,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) if chatType == client.TypeChatTypeSecret { chatTypeSecret, _ := chat.Type.(*client.ChatTypeSecret) _, err = c.client.CloseSecretChat(&client.CloseSecretChatRequest{ - SecretChatId: chatTypeSecret.SecretChatId, + SecretChatID: chatTypeSecret.SecretChatID, }) if err != nil { return err.Error(), true @@ -629,7 +630,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) // delete current chat case "delete": _, err := c.client.DeleteChatHistory(&client.DeleteChatHistoryRequest{ - ChatId: chatID, + ChatID: chatID, RemoveFromChatList: true, }) if err != nil { @@ -653,7 +654,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) } messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{ - ChatId: chatID, + ChatID: chatID, Query: query, Limit: limit, Filter: &client.SearchMessagesFilterEmpty{}, @@ -674,7 +675,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) } messages, err := c.client.GetChatHistory(&client.GetChatHistoryRequest{ - ChatId: chatID, + ChatID: chatID, Limit: limit, }) if err != nil { @@ -690,7 +691,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) } members, err := c.client.SearchChatMembers(&client.SearchChatMembersRequest{ - ChatId: chatID, + ChatID: chatID, Limit: 9999, Query: query, Filter: &client.ChatMembersFilterMembers{}, @@ -701,9 +702,18 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) var entries []string for _, member := range members.Members { + var senderId int64 + switch member.MemberID.MessageSenderType() { + case client.TypeMessageSenderUser: + memberUser, _ := member.MemberID.(*client.MessageSenderUser) + senderId = memberUser.UserID + case client.TypeMessageSenderChat: + memberChat, _ := member.MemberID.(*client.MessageSenderChat) + senderId = memberChat.ChatID + } entries = append(entries, fmt.Sprintf( "%v | role: %v", - c.formatContact(int64(member.UserId)), + c.formatContact(senderId), member.Status.ChatMemberStatusType(), )) } diff --git a/telegram/handlers.go b/telegram/handlers.go index e768f04..f8e6751 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -119,15 +119,15 @@ func (c *Client) updateHandler() { // new user discovered func (c *Client) updateUser(update *client.UpdateUser) { - c.cache.SetUser(update.User.Id, update.User) + c.cache.SetUser(update.User.ID, update.User) show, status := c.userStatusToText(update.User.Status) - go c.ProcessStatusUpdate(int64(update.User.Id), status, show) + go c.ProcessStatusUpdate(update.User.ID, status, show) } // user status changed func (c *Client) updateUserStatus(update *client.UpdateUserStatus) { show, status := c.userStatusToText(update.Status) - go c.ProcessStatusUpdate(int64(update.UserId), status, show, gateway.SPImmed(false)) + go c.ProcessStatusUpdate(update.UserID, status, show, gateway.SPImmed(false)) } // new chat discovered @@ -135,7 +135,7 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) { go func() { if update.Chat != nil && update.Chat.Photo != nil && update.Chat.Photo.Small != nil { _, err := c.client.DownloadFile(&client.DownloadFileRequest{ - FileId: update.Chat.Photo.Small.Id, + FileID: update.Chat.Photo.Small.ID, Priority: 32, Synchronous: true, }) @@ -145,7 +145,7 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) { } } - c.cache.SetChat(update.Chat.Id, update.Chat) + c.cache.SetChat(update.Chat.ID, update.Chat) var isChannel = false if update.Chat.Type.ChatTypeType() == client.TypeChatTypeSupergroup { @@ -157,21 +157,21 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) { } // don't subscribe to channel posters - if !((isChannel && update.Chat.LastReadInboxMessageId == 0) || + if !((isChannel && update.Chat.LastReadInboxMessageID == 0) || // don't subscribe to chats with no conversation // (manual adding will trigger a subscribe anyway) - (update.Chat.LastReadInboxMessageId == 0 && update.Chat.LastReadOutboxMessageId == 0)) { + (update.Chat.LastReadInboxMessageID == 0 && update.Chat.LastReadOutboxMessageID == 0)) { gateway.SendPresence( c.xmpp, c.jid, - gateway.SPFrom(strconv.FormatInt(update.Chat.Id, 10)), + gateway.SPFrom(strconv.FormatInt(update.Chat.ID, 10)), gateway.SPType("subscribe"), gateway.SPNickname(update.Chat.Title), ) } - if update.Chat.Id < 0 { - c.ProcessStatusUpdate(update.Chat.Id, update.Chat.Title, "chat") + if update.Chat.ID < 0 { + c.ProcessStatusUpdate(update.Chat.ID, update.Chat.Title, "chat") } }() } @@ -180,7 +180,7 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) { func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { go func() { // guarantee sequential message delivering per chat - lock := c.getChatMessageLock(update.Message.ChatId) + lock := c.getChatMessageLock(update.Message.ChatID) lock.Lock() defer lock.Unlock() @@ -192,7 +192,7 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { } log.WithFields(log.Fields{ - "chat_id": update.Message.ChatId, + "chat_id": update.Message.ChatID, }).Warn("New message from chat") text := c.messageToText(update.Message) @@ -201,7 +201,7 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { // download file(s) if file != nil && !file.Local.IsDownloadingCompleted { c.client.DownloadFile(&client.DownloadFileRequest{ - FileId: file.Id, + FileID: file.ID, Priority: 32, Synchronous: true, }) @@ -212,9 +212,9 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { prefix.WriteString(c.messageToPrefix(update.Message, c.formatContent(file, filename))) if text != "" { // \n if it is groupchat and message is not empty - if update.Message.ChatId < 0 { + if update.Message.ChatID < 0 { prefix.WriteString("\n") - } else if update.Message.ChatId > 0 { + } else if update.Message.ChatID > 0 { prefix.WriteString(" | ") } @@ -226,12 +226,12 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { // mark message as read c.client.ViewMessages(&client.ViewMessagesRequest{ - ChatId: update.Message.ChatId, - MessageIds: []int64{update.Message.Id}, + ChatID: update.Message.ChatID, + MessageIDs: []int64{update.Message.ID}, ForceRead: true, }) // forward message to XMPP - gateway.SendMessage(c.jid, strconv.FormatInt(update.Message.ChatId, 10), text, c.xmpp) + gateway.SendMessage(c.jid, strconv.FormatInt(update.Message.ChatID, 10), text, c.xmpp) }() } @@ -240,20 +240,20 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) { markupFunction := formatter.EntityToMarkdown if update.NewContent.MessageContentType() == client.TypeMessageText { textContent := update.NewContent.(*client.MessageText) - text := fmt.Sprintf("✎ %v | %s", update.MessageId, formatter.Format( + text := fmt.Sprintf("✎ %v | %s", update.MessageID, formatter.Format( textContent.Text.Text, formatter.SortEntities(textContent.Text.Entities), markupFunction, )) - gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp) + gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatID, 10), text, c.xmpp) } } // message(s) deleted func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) { if update.IsPermanent { - text := "✗ " + strings.Join(int64SliceToStringSlice(update.MessageIds), ",") - gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp) + text := "✗ " + strings.Join(int64SliceToStringSlice(update.MessageIDs), ",") + gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatID, 10), text, c.xmpp) } } @@ -269,7 +269,7 @@ func (c *Client) updateFile(update *client.UpdateFile) { fmt.Sprintf( "%s/%s%s", c.content.Path, - fmt.Sprintf("%x", sha256.Sum256([]byte(update.File.Remote.Id))), + fmt.Sprintf("%x", sha256.Sum256([]byte(update.File.Remote.ID))), filepath.Ext(update.File.Local.Path), ), ) diff --git a/telegram/utils.go b/telegram/utils.go index f7e7a28..3082b5f 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -6,7 +6,6 @@ import ( "fmt" "github.com/pkg/errors" "io" - "math" "os" "path/filepath" "regexp" @@ -44,7 +43,7 @@ func (c *Client) GetContactByUsername(username string) (*client.Chat, *client.Us return nil, nil, err } - return c.GetContactByID(chat.Id, chat) + return c.GetContactByID(chat.ID, chat) } // GetContactByID gets user and chat information from cache (or tries to retrieve it, if missing) @@ -58,18 +57,13 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli var ok bool var err error - if id <= math.MaxInt32 && id >= math.MinInt32 { - userID := int32(id) - user, ok = c.cache.GetUser(userID) - if !ok && userID > 0 { - user, err = c.client.GetUser(&client.GetUserRequest{ - UserId: userID, - }) - if err != nil { - return nil, nil, err - } - - c.cache.SetUser(userID, user) + user, ok = c.cache.GetUser(id) + if !ok && id > 0 { + user, err = c.client.GetUser(&client.GetUserRequest{ + UserID: id, + }) + if err == nil { + c.cache.SetUser(id, user) } } @@ -77,7 +71,7 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli if !ok { if chat == nil { cacheChat, err = c.client.GetChat(&client.GetChatRequest{ - ChatId: id, + ChatID: id, }) if err != nil { // error is irrelevant if the user was found successfully @@ -200,11 +194,11 @@ func (c *Client) formatContact(chatID int64) string { var str string if chat != nil { - str = fmt.Sprintf("%s (%v)", chat.Title, chat.Id) + str = fmt.Sprintf("%s (%v)", chat.Title, chat.ID) } else if user != nil { username := user.Username if username == "" { - username = strconv.FormatInt(int64(user.Id), 10) + username = strconv.FormatInt(user.ID, 10) } str = fmt.Sprintf("%s %s (%v)", user.FirstName, user.LastName, username) @@ -221,8 +215,8 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess var err error if message == nil { message, err = c.client.GetMessage(&client.GetMessageRequest{ - ChatId: chatID, - MessageId: messageID, + ChatID: chatID, + MessageID: messageID, }) if err != nil { return fmt.Sprintf("", err.Error()) @@ -234,7 +228,16 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess } var str strings.Builder - str.WriteString(fmt.Sprintf("%v | %s | ", message.Id, c.formatContact(int64(message.SenderUserId)))) + var senderId int64 + switch message.Sender.MessageSenderType() { + case client.TypeMessageSenderUser: + senderUser, _ := message.Sender.(*client.MessageSenderUser) + senderId = senderUser.UserID + case client.TypeMessageSenderChat: + senderChat, _ := message.Sender.(*client.MessageSenderChat) + senderId = senderChat.ChatID + } + str.WriteString(fmt.Sprintf("%v | %s | ", message.ID, c.formatContact(senderId))) if !preview { str.WriteString( time.Unix(int64(message.Date), 0). @@ -276,7 +279,7 @@ func (c *Client) formatContent(file *client.File, filename string) string { filename, file.Size/1024, c.content.Link, - fmt.Sprintf("%x", sha256.Sum256([]byte(file.Remote.Id))), + fmt.Sprintf("%x", sha256.Sum256([]byte(file.Remote.ID))), filepath.Ext(filename), ) } @@ -295,17 +298,17 @@ func (c *Client) messageToText(message *client.Message) string { addMembers, _ := message.Content.(*client.MessageChatAddMembers) text := "invited " - if len(addMembers.MemberUserIds) > 0 { - text += c.formatContact(int64(addMembers.MemberUserIds[0])) + if len(addMembers.MemberUserIDs) > 0 { + text += c.formatContact(addMembers.MemberUserIDs[0]) } return text case client.TypeMessageChatDeleteMember: deleteMember, _ := message.Content.(*client.MessageChatDeleteMember) - return "kicked " + c.formatContact(int64(deleteMember.UserId)) + return "kicked " + c.formatContact(deleteMember.UserID) case client.TypeMessagePinMessage: pinMessage, _ := message.Content.(*client.MessagePinMessage) - return "pinned message: " + c.formatMessage(message.ChatId, pinMessage.MessageId, false, nil) + return "pinned message: " + c.formatMessage(message.ChatID, pinMessage.MessageID, false, nil) case client.TypeMessageChatChangeTitle: changeTitle, _ := message.Content.(*client.MessageChatChangeTitle) return "chat title set to: " + changeTitle.Title @@ -393,7 +396,7 @@ func (c *Client) contentToFilename(content client.MessageContent) (*client.File, sizes := photo.Photo.Sizes if len(sizes) > 1 { file := sizes[len(sizes)-1].Photo - return file, strconv.FormatInt(int64(file.Id), 10) + ".jpg" + return file, strconv.FormatInt(file.ID, 10) + ".jpg" } return nil, "" case client.TypeMessageAudio: @@ -419,16 +422,32 @@ func (c *Client) messageToPrefix(message *client.Message, fileString string) str } else { directionChar = "⬅ " } - prefix = append(prefix, directionChar+strconv.FormatInt(message.Id, 10)) + prefix = append(prefix, directionChar+strconv.FormatInt(message.ID, 10)) // show sender in group chats - if message.ChatId < 0 && message.SenderUserId != 0 { - prefix = append(prefix, c.formatContact(int64(message.SenderUserId))) + if message.ChatID < 0 && message.Sender != nil { + var senderId int64 + switch message.Sender.MessageSenderType() { + case client.TypeMessageSenderUser: + senderUser, _ := message.Sender.(*client.MessageSenderUser) + senderId = senderUser.UserID + case client.TypeMessageSenderChat: + senderChat, _ := message.Sender.(*client.MessageSenderChat) + senderId = senderChat.ChatID + } + prefix = append(prefix, c.formatContact(senderId)) } if message.ForwardInfo != nil { switch message.ForwardInfo.Origin.MessageForwardOriginType() { case client.TypeMessageForwardOriginUser: originUser := message.ForwardInfo.Origin.(*client.MessageForwardOriginUser) - prefix = append(prefix, "fwd: "+c.formatContact(int64(originUser.SenderUserId))) + prefix = append(prefix, "fwd: "+c.formatContact(originUser.SenderUserID)) + case client.TypeMessageForwardOriginChat: + originChat := message.ForwardInfo.Origin.(*client.MessageForwardOriginChat) + var signature string + if originChat.AuthorSignature != "" { + signature = fmt.Sprintf(" (%s)", originChat.AuthorSignature) + } + prefix = append(prefix, "fwd: "+c.formatContact(originChat.SenderChatID)+signature) case client.TypeMessageForwardOriginHiddenUser: originUser := message.ForwardInfo.Origin.(*client.MessageForwardOriginHiddenUser) prefix = append(prefix, fmt.Sprintf("fwd: anonymous (%s)", originUser.SenderName)) @@ -438,12 +457,12 @@ func (c *Client) messageToPrefix(message *client.Message, fileString string) str if channel.AuthorSignature != "" { signature = fmt.Sprintf(" (%s)", channel.AuthorSignature) } - prefix = append(prefix, "fwd: "+c.formatContact(channel.ChatId)+signature) + prefix = append(prefix, "fwd: "+c.formatContact(channel.ChatID)+signature) } } // reply to - if message.ReplyToMessageId != 0 { - prefix = append(prefix, "reply: "+c.formatMessage(message.ChatId, message.ReplyToMessageId, true, nil)) + if message.ReplyToMessageID != 0 { + prefix = append(prefix, "reply: "+c.formatMessage(message.ChatID, message.ReplyToMessageID, true, nil)) } if fileString != "" { prefix = append(prefix, "file: "+fileString) @@ -484,8 +503,8 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int } c.client.EditMessageText(&client.EditMessageTextRequest{ - ChatId: chatID, - MessageId: messageID, + ChatID: chatID, + MessageID: messageID, InputMessageContent: message, }) } else { @@ -500,7 +519,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int var file *client.InputFileRemote if c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) { file = &client.InputFileRemote{ - Id: text, + ID: text, } } @@ -531,8 +550,8 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int } _, err := c.client.SendMessage(&client.SendMessageRequest{ - ChatId: chatID, - ReplyToMessageId: reply, + ChatID: chatID, + ReplyToMessageID: reply, InputMessageContent: message, }) if err != nil {