diff options
author | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2021-12-04 21:10:54 +0300 |
---|---|---|
committer | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2021-12-04 21:10:54 +0300 |
commit | 105f5017c35f92a9e2f5398a06cfdd3f1da31bad (patch) | |
tree | c602c7eb3ede37718894e7dea7875490cbc8d728 /telegram/commands.go | |
parent | bc37cf0c4f0d58752f92238a013513be9aa3caff (diff) |
Migrate to TDlib 1.7.9
Diffstat (limited to 'telegram/commands.go')
-rw-r--r-- | telegram/commands.go | 86 |
1 files changed, 48 insertions, 38 deletions
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(), )) } |