Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User's username is nearly always empty #85

Open
DayJun opened this issue Jul 15, 2024 · 2 comments
Open

User's username is nearly always empty #85

DayJun opened this issue Jul 15, 2024 · 2 comments

Comments

@DayJun
Copy link

DayJun commented Jul 15, 2024

Using u.EffectiveUser() to get a user, but username is nearly always empty

user := u.EffectiveUser()
	if user != nil {
		if user.Bot {
			return nil
		}
		username = user.Username
		nickname = user.FirstName + " " + user.LastName
	}

I tried to use client.API().UsersGetFullUser to get user, but it failed with rpcDoRequest: rpc error code 400: USER_ID_INVALID

if len(username) == 0 && user != nil {
		users, err := client.API().UsersGetFullUser(ctx, &tg.InputUser{
			UserID:     user.ID,
			AccessHash: user.AccessHash,
		})
		if err == nil {
			for _, user := range users.Users {
				peekedUser := user.(*tg.User)
				username = peekedUser.Username
				nickname = peekedUser.FirstName + " " + peekedUser.LastName
			}
		} else {
			fmt.Println(err)
		}
	}

How to get user's username in channel or group?

This confused me many days

@DayJun DayJun changed the title user's username is nearly always empty User's username is nearly always empty Jul 15, 2024
@celestix
Copy link
Owner

Hi @DayJun, it seems like the issue was: ctx.EffectiveUser() was referring to the logged in account sometimes, and maybe your logged in account didn't have a username. Can you check if it is fixed now?

@DayJun
Copy link
Author

DayJun commented Jul 17, 2024

I found that the channel id i got from update can't be used to call UsersGetUsers or UsersGetFullUser, so I called ChannelsGetChannels to get a real channel peer, and I found that the access hash of channel is different from the one which i got from PeerStorage.

p, e := client.API().ChannelsGetChannels(ctx, []tg.InputChannelClass{
			&tg.InputChannel{
				ChannelID:  channelId,
				AccessHash: channelAccessHash,
			},
		})
		if e == nil {
			chats := p.GetChats()
			if len(chats) > 0 {
				channelPeer := chats[0].(*tg.Channel).AsInputPeer()
				users, err := client.API().UsersGetUsers(ctx, []tg.InputUserClass{
					&tg.InputUserFromMessage{
						UserID: user.ID,
						MsgID:  msg.ID,
						Peer:   channelPeer,
					},
				},
				)
				if err == nil {
					for _, user := range users {
						peekedUser := user.(*tg.User)
						username = peekedUser.Username
						nickname = peekedUser.FirstName + " " + peekedUser.LastName
						ctx.PeerStorage.AddPeer(peekedUser.ID, peekedUser.AccessHash, storage.TypeUser, username)
					}
				}
			}
		}

And then, I got a new problem. Sometimes when I call UsersGetUsers with InputUserFromMessage filled with the real channel peer, the error value is "MESSAGE_ID_INVALID".

So, is it normal that Username field is empty in user which is get from update? And is it normal that the channel's access hash is not the real one and the message id may be wrong?

saiaapiz14 added a commit to saiaapiz14/gotgproto that referenced this issue Oct 7, 2024
Referencing issue [celestix#85](celestix#85).

The problem arises because Go maps are unordered, and `getDifferent` is called before `fillUserIdFromMessage`. This sequence can lead to an incorrect `userId` being assigned to `Update.userId` due to inconsistent entity processing. Ensuring proper order of operations will resolve this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants