skip to Main Content

getFullUser a method in Telegram API that returns extended user info by ID.

https://core.telegram.org/method/users.getFullUser

My question is how I can get the user id from telegram username to use with this method.
For example this is my username : telegram.me/androidsoftware .
Is a method exist that return userId from username ?

2

Answers


  1. Chosen as BEST ANSWER

    I have found this code in Telegram source code, but I don't know how write this code in C# to use with MTProto.

    if (username != null) {
            TLRPC.TL_contacts_resolveUsername req = new TLRPC.TL_contacts_resolveUsername();
            req.username = username;
            requestId = ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() {
                @Override
                public void run(final TLObject response, final TLRPC.TL_error error) {
                    AndroidUtilities.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            if (!LaunchActivity.this.isFinishing()) {
                                try {
                                    progressDialog.dismiss();
                                } catch (Exception e) {
                                    FileLog.e("tmessages", e);
                                }
                                if (error == null && actionBarLayout != null) {
                                    final TLRPC.TL_contacts_resolvedPeer res = (TLRPC.TL_contacts_resolvedPeer) response;
                                    MessagesController.getInstance().putUsers(res.users, false);
                                    MessagesController.getInstance().putChats(res.chats, false);
                                    MessagesStorage.getInstance().putUsersAndChats(res.users, res.chats, false, true);
    
                                    if (botChat != null) {
                                        final TLRPC.User user = !res.users.isEmpty() ? res.users.get(0) : null;
                                        if (user == null || (user.flags & TLRPC.USER_FLAG_BOT) != 0 && (user.flags & TLRPC.USER_FLAG_BOT_CANT_JOIN_GROUP) != 0) {
                                            try {
                                                Toast.makeText(LaunchActivity.this, LocaleController.getString("BotCantJoinGroups", R.string.BotCantJoinGroups), Toast.LENGTH_SHORT).show();
                                            } catch (Exception e) {
                                                FileLog.e("tmessages", e);
                                            }
                                            return;
                                        }
                                        Bundle args = new Bundle();
                                        args.putBoolean("onlySelect", true);
                                        args.putInt("dialogsType", 2);
                                        args.putString("addToGroupAlertString", LocaleController.formatString("AddToTheGroupTitle", R.string.AddToTheGroupTitle, UserObject.getUserName(user), "%1$s"));
                                        DialogsActivity fragment = new DialogsActivity(args);
                                        fragment.setDelegate(new DialogsActivity.MessagesActivityDelegate() {
                                            @Override
                                            public void didSelectDialog(DialogsActivity fragment, long did, boolean param) {
                                                NotificationCenter.getInstance().postNotificationName(NotificationCenter.closeChats);
                                                MessagesController.getInstance().addUserToChat(-(int) did, user, null, 0, botChat, null);
                                                Bundle args = new Bundle();
                                                args.putBoolean("scrollToTopOnResume", true);
                                                args.putInt("chat_id", -(int) did);
                                                actionBarLayout.presentFragment(new ChatActivity(args), true, false, true);
                                            }
                                        });
                                        presentFragment(fragment);
                                    } else {
                                        Bundle args = new Bundle();
                                        if (!res.chats.isEmpty()) {
                                            args.putInt("chat_id", res.chats.get(0).id);
                                        } else {
                                            args.putInt("user_id", res.users.get(0).id);
                                        }
                                        if (botUser != null) {
                                            args.putString("botUser", botUser);
                                        }
                                        ChatActivity fragment = new ChatActivity(args);
                                        NotificationCenter.getInstance().postNotificationName(NotificationCenter.closeChats);
                                        actionBarLayout.presentFragment(fragment, false, true, true);
                                    }
                                } else {
                                    try {
                                        Toast.makeText(LaunchActivity.this, LocaleController.getString("NoUsernameFound", R.string.NoUsernameFound), Toast.LENGTH_SHORT).show();
                                    } catch (Exception e) {
                                        FileLog.e("tmessages", e);
                                    }
                                }
                            }
                        }
                    });
                }
            });
        }
    

Please signup or login to give your own answer.
Back To Top
Search