fergaral
fergaral

Reputation: 2097

Android - Send Telegram message to a specific number

I'm trying to send a Telegram message to a specific number from within my Android app. Right now my code launches Telegram app, and then the user has to select the destinatary. What I want to do is to send the message to the specified number, without having the user select the contact. My code is as follows:

/**
 * 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();
    }
}

Upvotes: 17

Views: 27569

Answers (6)

samanba81
samanba81

Reputation: 1

I'm using this for send message on background :
this cod is api telegram for use send message by url token bot

1 - use WebView on xml like this :

    <WebView
    android:id="@+id/webView_sendToTelegram"
    android:layout_width="0dp"
    android:layout_height="0dp"/>

2 - on java use this cod:

public static void SendMessageToBotTelegram(String chatID, String text, String botToken,WebView webView) {
    webView.loadUrl(MessageFormat.format("https://api.telegram.org/bot{0}/sendMessage?chat_id={1}&text={2}", botToken, chatID, text));
}

chatID : your telegram id @RawDataBot for get id
text : text your message
botToken : your bot for get message if you don't have bot using @BotFather for crate bot
webView : your id WebView on xml

Upvotes: 0

orz12
orz12

Reputation: 1

try the intent like this tg://resolve?domain=YOUR_USER_ID it's more direct then https://t.me

Upvotes: 0

Aditya Nandardhane
Aditya Nandardhane

Reputation: 1393

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.

Upvotes: 2

user6558741
user6558741

Reputation:

You can't send to special number, But You can do this by USERID

try {

    Intent telegramIntent = new Intent(Intent.ACTION_VIEW);
    telegramIntent.setData(Uri.parse("http://telegram.me/USERID"));
    startActivity(telegramIntent);

} catch (Exception e) {
        // show error message
}

This code will show user an alert for choosing applications that support telegram uri's like Telegram itself and Mobogram!

Tip: don't set package name. some people install telegram alternatives like mobogram.

Upvotes: 11

MSaudi
MSaudi

Reputation: 4652

This one worked for me:

//check if application is installed first before running this code.

 Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse("http://telegram.me/+UT_USER_ID_HERE"));
            final String appName = "org.telegram.messenger";
                    i.setPackage(appName);
            this.startActivity(i);

Upvotes: 0

Sekongur
Sekongur

Reputation: 84

The Telegram Android App does not have a way to send messages directly to telegram users, so if you use the share intent, you'll get what telegram / any other app wants to do with the message shared. In this case, open the contact list to send this message to him.

If you want to send messages directly to Telegram users you should use the Telegram API https://core.telegram.org/api#getting-started

once you have configured your API key in your app, you could send messages, read them or even get the telegram contacts with these methods

https://core.telegram.org/methods

Upvotes: 6

Related Questions