cjongseok / mtproto

Telegram MTProto and its proxy (over gRPC) in Go (golang). API Layer: 71
Apache License 2.0
150 stars 20 forks source link

Sending Message to User #13

Closed Skyforces closed 6 years ago

Skyforces commented 6 years ago

Hey! I like to get the full information about an user (from my contacts or not) if he writes me a message. So on PredUpdateShortMessage, i try to call the UsersGetUsers but i need an AccessHash i didnt have. how do i get it? Here is my code: `

case *mtproto.PredUpdateShortMessage:

    {
        caller := mtproto.RPCaller{RPC: s.mconn}

        inputUser := &mtproto.TypeInputUser{
            Value: &mtproto.TypeInputUser_InputUser{
                InputUser: &mtproto.PredInputUser{
                    UserId: u.(*mtproto.PredUpdateShortMessage).Id,
                    AccessHash: ???,
                },
            },
        }

        idArray := []*mtproto.TypeInputUser{
            inputUser,
        }

        _, err := caller.UsersGetUsers(context.Background(), &mtproto.ReqUsersGetUsers{
            Id: idArray,
        })

        fmt.Println(err.Error())
    }
cjongseok commented 6 years ago

@Skyforces Thanks for your interest in my code. You can get user hashes using ContactsGetContacts. It also needs a hash, but you can just assign 0 to it as below.

caller.ContactsGetContacts(context.Background(), &mtproto.ReqContactsGetContacts{})

I added this example to examples/simpleshell as 'contacts' command.

Skyforces commented 6 years ago

Thanks for your quick reply. With your example i get all my contacts and the access hashs. It works fine but im missing Bots and foreign users, which aren't in my contact list. Maybe i understand the getUser function wrong?

cjongseok commented 6 years ago

@Skyforces I think

caller.MessagesGetAllChats(context.Background(), &mtproto.ReqMessagesGetAllChats{})

would work for that. It returns id and hash list of all the chats and channels.

Skyforces commented 6 years ago

It works quite good so far. Still have some question, sorry i didn't clarified them. What is the function of MessageGetChats and UsersGetUsers? I thought you can get a specific User/Chat/Channel with them based on ID. Like this:

ids := []int32{u.(*mtproto.PredUpdateShortMessage).UserId} chats, _ := caller.MessagesGetChats(context.Background(), &mtproto.ReqMessagesGetChats{ Id: ids, })

Second is about the implementation of the InputUser. There are too different types, inputUserContact and inputUserForeign. One with access hash and one without. How is this implemented in your api?

cjongseok commented 6 years ago

@Skyforces MessagesGetChats() needs a chat or channel ID, not an user ID. MessagesGetAllChats() returns TypeMessagesChats, which would be one of PredMessagesChats and PredMessagesChatsSlice. The both have an array of TypeChat. TypeChat is one of PredChat, PredChannel, ... . PredChat and PredChannel have an id, and you can use it for MessagesGetChats().

Now InputUser has no types of inputUserContact and inputUserForeign. Telegram's mtproto document is too old, so you should refer to the protocol definition in this protobuf file; Telegram's document version is layer 23, the latest one is layer 81, and mine is 71.

Skyforces commented 6 years ago

Okay, I undestand this now. You pushed me to the right point. Thank you! Keep on your good work!