从Telegram用户名中查找用户ID

时间:2015-11-12 15:18:12

标签: c# android api telegram

getFullUser Telegram API中的一种方法,它按ID返回扩展的用户信息。

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

我的问题是如何从电报用户名中获取用户ID以使用此方法。 例如,这是我的用户名:telegram.me/androidsoftware。 是否存在从用户名返回userId的方法?

2 个答案:

答案 0 :(得分:1)

我在Telegram源代码中找到了这段代码, 但我不知道如何在C#中编写此代码以与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);
                                }
                            }
                        }
                    }
                });
            }
        });
    }

答案 1 :(得分:0)

相关问题