从Telegram用户名中查找用户id
本文关键字:用户 查找 id Telegram | 更新日期: 2023-09-27 18:21:44
getFullUser
Telegram API中的一种方法,通过ID返回扩展的用户信息。
https://core.telegram.org/method/users.getFullUser
我的问题是如何从telegram用户名中获取用户id以用于此方法。例如,这是我的用户名:telegram.me/androidsoftware。是否存在从username返回userId
的方法?
我在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);
}
}
}
}
});
}
});
}
您应该使用未记录的方法ContactsResolveUsername
,检查客户端SDK 中是否存在此方法
Java中的示例:https://github.com/rubenlagus/TelegramApi/blob/master/src/main/java/org/telegram/api/functions/contacts/TLRequestContactsResolveUsername.java
说明:http://aseman.co/documents/libqtelegram-ae/functions/methods/resolveusername.html
使用plus messenger,如果你搜索用户