Open telegram channel in android
Asked Answered
Z

7

23

In my app want to use Intent to open specific telegram channel or telegram group. i search in SF but i can't find anything.i try to implement but i only can open all messenger apps that user can choose but not telegram or specific telegram group or channel. if find this on sf but it's not answer to my question.

    Intent myIntent = new Intent(Intent.ACTION_SEND);
    myIntent.setType("text/plain");
    myIntent.setPackage(appName);
    myIntent.putExtra(Intent.EXTRA_TEXT, msg);//
    mUIActivity.startActivity(Intent.createChooser(myIntent, "Share with"));
Zoosperm answered 9/1, 2016 at 10:5 Comment(0)
T
45

Intent for opening a Telegram channel or user :

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tg://resolve?domain=partsilicon"));
startActivity(intent);
Thereinafter answered 29/3, 2016 at 10:1 Comment(3)
also we can use http://www.telegram.me/groupname in intent and when user click on button can choose telegramZoosperm
can we add message or text on this link?Eulalia
add try catch for above solution. Otherwise app will crash if telegram app not installedHolierthanthou
K
6

First check if any telegram client (telegram messenger or telegram x) is installed. If not open it in browser.

fun telegramIntent(context: Context): Intent {
    var intent: Intent? = null
    try {
        try {
           context.packageManager.getPackageInfo("org.telegram.messenger", 0)//Check for Telegram Messenger App
        } catch (e : Exception){
           context.packageManager.getPackageInfo("org.thunderdog.challegram", 0)//Check for Telegram X App
        }
       intent = Intent(Intent.ACTION_VIEW, Uri.parse("tg://resolve?domain=${TELEGRAM_PAGE_ID}"))
    }catch (e : Exception){ //App not found open in browser
       intent = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.telegram.me/$TELEGRAM_PAGE_ID"))
    }
   return intent!!
}
Kauri answered 6/10, 2019 at 12:4 Comment(3)
The ${TELEGRAM_PAGE_ID}"it could be a phone number or what it should be??Honky
${TELEGRAM_PAGE_ID} that is nothing but the group id/channel id which we are trying to open. To locate the channel id, open any telegram channel, click on the channel info. You will find a link e.g https://t.me/easyoffers the ${TELEGRAM_PAGE_ID} would be easyoffersKauri
And if you want to open a conversation, not a group, the ${TELEGRAM_PAGE_ID} will be the phone number??Honky
A
5

Answer @Saurabh Padwekar is good, but i want tell that you probably need add queries for Android SDK 30+ like this:

<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="tg" />
    </intent>
</queries>
Artur answered 17/7, 2021 at 20:35 Comment(0)
U
2

Java version of @Saurabh Padwekar 's answer!

  Intent getTelegramInt(Context context) {
        Intent intent;
        try {
            try { // check for telegram app
                context.getPackageManager().getPackageInfo("org.telegram.messenger", 0);
            } catch (PackageManager.NameNotFoundException e) {
                // check for telegram X app
                context.getPackageManager().getPackageInfo("org.thunderdog.challegram", 0);
            }
            // set app Uri
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tg://resolve?domain=${TELEGRAM_PAGE_ID}"));
        } catch (PackageManager.NameNotFoundException e) {
            // set browser URI
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.telegram.me/$TELEGRAM_PAGE_ID"));
        }
        return intent;
    }
Uund answered 26/12, 2020 at 16:41 Comment(0)
J
2
private void getTelegramInt(Context context) {

    Intent intent;

    try {
        try {
            context.getPackageManager().getPackageInfo("org.telegram.messenger", 0);
        } catch (PackageManager.NameNotFoundException e) {
            context.getPackageManager().getPackageInfo("org.thunderdog.challegram", 0);
        }
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tg://resolve?domain=dilshod_software"));
        startActivity(intent);

    } catch (PackageManager.NameNotFoundException e) {
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://t.me/dilshod_software"));
        startActivity(intent);
    }
}

My button click : telegram_image.setOnClickListener(view1 -> getTelegramInt(this));

Janssen answered 18/9, 2022 at 11:56 Comment(1)
Whats the logic behind returning the intent?Tieshatieup
R
1

This would be a more complete answer:

try {
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(telegramLink));
     PackageManager pm = getPackageManager();
     if (intent.resolveActivity(pm) != null) {
          startActivity(intent);
     } else {
        Toast.makeText(context, "Error message", Toast.LENGTH_LONG).show();
     }
    } catch (Exception ignored) {
    }
Rosy answered 14/1, 2022 at 9:37 Comment(0)
G
0

Try,

private void goTelegramLink(Context context,String tg) {

    Intent intent;
    try {
        try {
            context.getPackageManager().getPackageInfo("org.telegram.messenger", 0);
        } catch (PackageManager.NameNotFoundException e) {
            context.getPackageManager().getPackageInfo("org.thunderdog.challegram", 0);
        }
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tg://resolve?domain="+tg));
        startActivity(intent);

    } catch (PackageManager.NameNotFoundException e) {
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://t.me/"+tg));
        startActivity(intent);
    }
}

private String tgurl="telegramuser";//https://t.me/telegramuser
goTelegramLink(getContext(),tgurl);
Gassman answered 3/8 at 2:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.