13

I have an android app that should be able to open a chat in the telegram app by pressing a button.
I want to open an existing robot chat page DIRECTLY from my app. I have a valid token for my robot. How can this be achieved?

Thanks in advance.

robot name : @InfotechAvl_bot

robot token: 179284***********

   //-------------
    case ContentFragment.lMenuTelegram:
     Intent LaunchIntent=getPackageManager().getLaunchIntentForPackage("org.telegram.messenger");
     startActivity(LaunchIntent);
            break;
Mark Booth
  • 7,263
  • 2
  • 63
  • 90
BoshRa
  • 758
  • 1
  • 7
  • 15

3 Answers3

26

To solve this problem you have to open robot id with this :

Intent telegram = new Intent(Intent.ACTION_VIEW , Uri.parse("https://telegram.me/InfotechAvl_bot"));
startActivity(telegram);
BoshRa
  • 758
  • 1
  • 7
  • 15
5

If you prefer a little bit complex system I recommend you to use:

/** 
     * Intent to send a telegram message 
     * @param msg 
     */ 
    void intentMessageTelegram(String msg)
    { 
        final String appName = "org.telegram.messenger";
        final boolean isAppInstalled = isAppAvailable(mUIActivity.getApplicationContext(), appName);
        if (isAppInstalled) 
        { 
            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"));
        }  
        else  
        { 
            Toast.makeText(mUIActivity, "Telegram not Installed", Toast.LENGTH_SHORT).show();
        } 
    } 

And check if is installed with:

/** 
         * Indicates whether the specified app ins installed and can used as an intent. This 
         * method checks the package manager for installed packages that can 
         * respond to an intent with the specified app. If no suitable package is 
         * found, this method returns false. 
         * 
         * @param context The application's environment. 
         * @param appName The name of the package you want to check 
         * 
         * @return True if app is installed 
         */ 
        public static boolean isAppAvailable(Context context, String appName) 
        { 
            PackageManager pm = context.getPackageManager();
            try  
            { 
                pm.getPackageInfo(appName, PackageManager.GET_ACTIVITIES);
                return true; 
            }  
            catch (NameNotFoundException e) 
            { 
                return false; 
            } 
        } 

Hope this works for you.

Mayank Bhatnagar
  • 2,772
  • 1
  • 13
  • 19
  • copy paste but ok – Alireza Jamali Mar 04 '20 at 20:21
  • @Ali Thanks I didn't know this. – Mayank Bhatnagar Mar 06 '20 at 12:44
  • oh really? https://stackoverflow.com/questions/21627167/how-to-send-a-intent-with-telegram and you didn't know this, you just accidentally wrote every single word of this answer from TWO YEARS AGO, I'm not saying you did anything wrong, I said its OK, it may help someone who found this question instead of that, but don't play SMART "I DIDN"T KNOW THIS" yea yea~ – Alireza Jamali Mar 06 '20 at 12:55
  • 1
    @Ali thanks you can kindly downvote the answer in case you find it copied. Appreciate your kindness. – Mayank Bhatnagar Mar 06 '20 at 17:46
3

for Instagram and Telegram and WhatsApp you can use this methods

 void getTelegram(){

        try {
            Intent telegramIntent = new Intent(Intent.ACTION_VIEW);
            telegramIntent.setData(Uri.parse("https://telegram.me/diako099"));
            startActivity(telegramIntent);
        } catch (Exception e) {
            // show error message
        }
    }

    void getInstagram(){
        try {
            Intent instagramIntent = new Intent(Intent.ACTION_VIEW);
            instagramIntent.setData(Uri.parse("https://www.instagram.com/diako_hasani99/"));
            startActivity(instagramIntent);
        } catch (Exception e) {
            // show error message
        }

    }

    void getWhatsApp(){
        try{

            String trimToNumner = "+989180074693"; //10 digit number
            Intent intent = new Intent ( Intent.ACTION_VIEW );
            intent.setData ( Uri.parse ( "https://wa.me/" + trimToNumner + "/?text=" + "" ) );
            startActivity ( intent );

        }catch (Exception e){

        }
    }
Diako Hasani
  • 1,026
  • 8
  • 14