java Android - 向特定号码发送电报消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30055201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - Send Telegram message to a specific number
提问by fergaral
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:
我正在尝试从我的 Android 应用程序中向特定号码发送电报消息。现在我的代码启动 Telegram 应用程序,然后用户必须选择目的地。我想要做的是将消息发送到指定的号码,而无需用户选择联系人。我的代码如下:
/**
* 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();
}
}
采纳答案by Sekongur
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.
Telegram Android 应用程序无法直接向电报用户发送消息,因此如果您使用共享意图,您将获得电报/任何其他应用程序想要对共享的消息执行的操作。在这种情况下,打开联系人列表将此消息发送给他。
If you want to send messages directly to Telegram users you should use the Telegram API https://core.telegram.org/api#getting-started
如果您想直接向 Telegram 用户发送消息,您应该使用 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
一旦您在应用程序中配置了 API 密钥,您就可以使用这些方法发送消息、阅读它们甚至获取电报联系人
回答by fergaral
You can't send to special number, But You can do this by USERID
你不能发送到特殊号码,但你可以通过 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!
此代码将向用户显示一个警报,提示用户选择支持电报 uri 的应用程序,例如 Telegram 本身和 Mobogram!
Tip:don't set package name. some people install telegram alternatives like mobogram.
提示:不要设置包名。有些人安装电报替代品,如 mobogram。
回答by MSaudi
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);