1

I'm trying to share a text to a precise chat on telegram. I'm using Android Studio.

If use only .setData (giving the URI to open the right chat) I manage to open the right chat without sharing anything:

Uri uri = Uri.parse("https://telegram.me/ChadID");
final String appName = "org.telegram.messenger";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_VIEW);
sharingIntent.setData(uri);
sharingIntent.setPackage(appName);
startActivity(sharingIntent);

This code doesn't return errors.

If I put .setType, I can share a text on Telegram, but then I've to choose the chat:

final String appName = "org.telegram.messenger";
String shareBody = "Here is the share content body";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_VIEW);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
sharingIntent.setPackage(appName);
startActivity(sharingIntent);

This code doesn't return errors.

I then try to merge the 2 solutions with .setDataAndType

Uri uri = Uri.parse("https://telegram.me/UserID");
final String appName = "org.telegram.messenger";
String shareBody = "Here is the share content body";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_VIEW);
sharingIntent.setDataAndType(uri,"text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
sharingIntent.setPackage(appName);
startActivity(sharingIntent);

This just crashes, I think setDataAndType does not what I suppose. But If I do

sharingIntent.setData
sharingIntent.setType

I get a warning telling me that the second command will erease the first one, and suggest to use .setDataAndType

Until now the code just contains only this rows I copied here. Nothing else.

What I saw before trying this:

Telegram API (NOT bot api, I don't need a bot).... even though I've made java/python/c#/php telegram bot, I just can't manage to use Telegram API, I'm not new to programming but I feel stupid because I just don't understand anything about Telegram API. Feel free to suggest it if you have some examples.

Aim: My purpose is to actually send a message to a precise telegram chat from my smartphone telegram app, and the closer result I had is with sharing. I dunno if there is a better way to do it, I'm opened to suggestion about this over the code error I'm experiencing.

EDIT: Error message - ACTION_SEND to ACTION_VIEW

The error:

Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=https://telegram.me/... typ=text/plain pkg=org.telegram.messenger (has extras) }
Raikoug
  • 347
  • 3
  • 16

0 Answers0