Android - Send Telegram message to a specific number
Asked Answered
S

6

17

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();
    }
}
Swinford answered 5/5, 2015 at 14:2 Comment(3)
I did something like that by using an eMail intent (it opens a chooser - Telegram is listed), which I can make the default, eventually. I also send optional attachments (0, 1 or 2 - depending on user choices). I use the recipient's eMail address.Hermineherminia
I'm not sure you can do that since Telegram uses your phone contact list and it doesn't have a Type a new number to chat with function by itselfChorea
hi check my answer in this question.Stationer
E
6

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

Ethel answered 5/5, 2015 at 14:41 Comment(0)
P
11

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.

Pole answered 29/5, 2017 at 10:48 Comment(1)
How can i get username of user?Maggee
E
6

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

Ethel answered 5/5, 2015 at 14:41 Comment(0)
W
2

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.

Wince answered 19/3, 2021 at 10:44 Comment(0)
T
0

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);
Transalpine answered 3/4, 2017 at 12:33 Comment(0)
E
0

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

Explosive answered 4/6, 2022 at 1:15 Comment(0)
K
0

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

Kingpin answered 16/11, 2022 at 19:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.