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"));
Community
  • 1
  • 1
MBehtemam
  • 7,865
  • 15
  • 66
  • 108

6 Answers6

44

Intent for opening a Telegram channel or user :

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tg://resolve?domain=partsilicon"));
startActivity(intent);
Sadegh Ghanbari
  • 1,271
  • 15
  • 29
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!!
}
Saurabh Padwekar
  • 3,888
  • 1
  • 31
  • 37
  • 1
    The ```${TELEGRAM_PAGE_ID}"```it could be a phone number or what it should be?? – Sergio May 19 '20 at 16:07
  • ${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 `easyoffers` – Saurabh Padwekar May 19 '20 at 17:38
  • And if you want to open a conversation, not a group, the ${TELEGRAM_PAGE_ID} will be the phone number?? – Sergio May 20 '20 at 06:49
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>
kot3n0t
  • 51
  • 1
  • 3
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;
    }
Teekam Suthar
  • 529
  • 1
  • 9
  • 20
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));

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) {
    }
Ashish Bahl
  • 33
  • 1
  • 5