how open chat page of an specific telegram contact by intent
Asked Answered
F

3

6

I want open chat page of an specific telegram contact for example @userTest by android intent.

this is snippet of open telegram by intent:

Intent myIntent = new Intent(Intent.ACTION_SEND);
myIntent.setType("text/plain");
myIntent.setPackage("org.telegram.messenger");
activity.startActivity(myIntent);

but now how open chat page of an specific user?

Frontier answered 17/5, 2016 at 16:4 Comment(4)
maybe try to open the relevant telegram.me page, e.g. telegram.me/userTestMiltonmilty
ok. solved. thanks :)Frontier
@saeid How did you solve it? please shareTibbitts
Please share your solutionCicero
G
3

How it works:

  • It builds list of browsers to ignore them if telegram client is installed.

  • If there is one and only one client(goodresolvers == 1) then it's opened.

  • If there are no good clients (goodresolvers == 0) it fall backs to default intent handler.

  • You can improve this code further if you implement a dialog with custom chooser who only allows selecting "good" clients in case user have several Telegram clients installed.

        public static void openTelegram(Activity activity, String userName) {
            Intent general = new Intent(Intent.ACTION_VIEW, Uri.parse("https://t.com/" + userName));
            HashSet<String> generalResolvers = new HashSet<>();
            List<ResolveInfo> generalResolveInfo = activity.getPackageManager().queryIntentActivities(general, 0);
            for (ResolveInfo info : generalResolveInfo) {
                if (info.activityInfo.packageName != null) {
                    generalResolvers.add(info.activityInfo.packageName);
                }
            }
    
            Intent telegram = new Intent(Intent.ACTION_VIEW, Uri.parse("https://t.me/" + userName));
            int goodResolver = 0;
            // gets the list of intents that can be loaded.
            List<ResolveInfo> resInfo = activity.getPackageManager().queryIntentActivities(telegram, 0);
            if (!resInfo.isEmpty()) {
                for (ResolveInfo info : resInfo) {
                    if (info.activityInfo.packageName != null && !generalResolvers.contains(info.activityInfo.packageName)) {
                        goodResolver++;
                        telegram.setPackage(info.activityInfo.packageName);
                    }
                }
            }
            //TODO: if there are several good resolvers create custom chooser
            if (goodResolver != 1) {
                telegram.setPackage(null);
            }
            if (telegram.resolveActivity(activity.getPackageManager()) != null) {
                activity.startActivity(telegram);
            }
        }

usage: openTelegram(activity, "userTest");

Guntar answered 30/1, 2018 at 16:46 Comment(0)
W
3

This is a simple solution, but it works flawlessly.

try {
    Intent telegram = new Intent(Intent.ACTION_VIEW, Uri.parse("https://t.me/UsernameOrId"));
    telegram.setPackage("org.telegram.messenger");
    startActivity(telegram);
} catch (Exception e) {
    Toast.makeText(this, "Telegram app is not installed", Toast.LENGTH_LONG).show();
}
Wiretap answered 23/3, 2018 at 15:11 Comment(8)
yeah, it will work flawlessly for single telegram client amongst tens, and there are at least two official telegram clients.Guntar
What @BorisTreukhov is true! Remove the setPackage line, so all telegram apps which can handle uri will pop up.Anuska
@KirillStarostin it's not that simple, if you simply remove setPackage then the system browser will appear in android chooser as it can handle https:// scheme perfectlyGuntar
@BorisTreukhov I assume there could be requirements which would demand for the browser not to appear, but i personally don't think that it's a bad thing. If a user chooses web - so be it.Anuska
@KirillStarostin this will waste user's time because every Android device has a browser, but it's a rare situation if user uses many Telegram clients same timeGuntar
@BorisTreukhov how exactly will it waste user's time? "if user uses many Telegram clients" - the main benefit of the solution above is that it will work for different users who use different clients. Not one with many.Anuska
@KirillStarostin 99% of users will decide that there's a bug if a weird chooser will instead of Telegram client when the user taps on @Username, I don't think they will even understand what is going on. So the developer of the application will be to blame, application would seem raw because it violates the principle of least astonishment. Not all users will see the things from the programmer's perspective even if it fits some abstract nature of order, a model of choosers invented by Google engineers. So the user wants to send the message, he has the client installed, why distract him?Guntar
@BorisTreukhov On the general i agree with you, but I feel that a number of 99% is far fetched. Also i think that the problem in this case is not that large and would certainly not make user see the app to be "raw". If that is not a part of the core functionality, of course. In my case i have company's Telegram contact hidden in settings, so i deem it better to fit all the Telegram clients than to save a really momentary confusion.Anuska
U
0

This one worked for me:

try {
Intent telegram = new Intent(Intent.ACTION_VIEW, Uri.parse("https://t.me/USER_NAME"));
 telegram.setPackage("org.telegram.messenger");
 startActivity(telegram);
}catch (Exception e) 
{
   Toast.makeText(getContext(), "Telegram app is not installed", Toast.LENGTH_LONG).show();
 }

Tip: You can get USER_NAME by click on you telegram profile option you will get option of username in Account session --> if username is none create unique username and put here its work for me

Uprising answered 19/3, 2021 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.