User's username is nearly always empty
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
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?
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?