Android - Send Telegram message to a specific number

时光总嘲笑我的痴心妄想 提交于 2019-11-27 17:55:01

问题


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

回答1:


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




回答2:


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.




回答3:


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);


来源:https://stackoverflow.com/questions/30055201/android-send-telegram-message-to-a-specific-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!