Search code examples
androidcalltelegram

Make telegram call with "user_id" with android programmatically


I want to make start a telegram call from my android app without opening telegram. Tried so:

public void callToTelegramContact(String user_id) {
    final String appName = "org.telegram.messenger";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse("tg://openmessage?" + user_id)); // open chan with user
    i.setPackage(appName);
    i.setType("vnd.android.cursor.item/vnd.org.telegram.messenger.android.call"); //mimeType
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
}

can anyone know how to do this? I know that this is possible, since it is implemented in the application "MacroDroid"

https://i.sstatic.net/c3Ylt.png


Solution

  • I figured out how to do it. If you need to implement this, then use URI contactID with mimeType which is "vnd.android.cursor.item/vnd.org.telegram.messenger.android.call.video". Looking first contactID by number phone:

    private String getContactIdByPhoneNumber(String phoneNumber) {
        ContentResolver contentResolver = getContentResolver();
        String contactId = null;
        Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
        String[] projection = new String[]{ContactsContract.PhoneLookup._ID};
        Cursor cursor = contentResolver.query(uri, projection, null, null, null);
    
        if (cursor != null) {
            while (cursor.moveToNext()) {
                contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
            }
            cursor.close();
        }
        return contactId;
    }
    

    then use this contactID and mimeType to find URI

    private Uri getUriFromPhoneNumber(String phoneNumber) {
        Uri uri = null;
        String contactId = getContactIdByPhoneNumber(phoneNumber);
        String mimeTypeTelegram = "vnd.android.cursor.item/vnd.org.telegram.messenger.android.call";
        Cursor cursorTelegram = getContentResolver().query(
                ContactsContract.Data.CONTENT_URI,
                new String[]{ContactsContract.Data._ID},
                ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?",
                new String[]{contactId, mimeTypeTelegram}, null);
        if (cursorTelegram != null) {
            while (cursorTelegram.moveToNext()) {
                String id = cursorTelegram.getString(cursorTelegram.getColumnIndexOrThrow(ContactsContract.Data._ID));
                if (!TextUtils.isEmpty(id)) {
                    uri = Uri.parse(ContactsContract.Data.CONTENT_URI + "/" + id);
                    break;
                }
            }
            cursorTelegram.close();
        }
        return uri;
    }
    

    after which use Intent.ACTION_VIEW

    public void сallToTelegramContact(String phoneNumber) {
        Uri uri = getUriFromPhoneNumber(phoneNumber);
        Log.d(TAG, uri + "");
        if (uri != null) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(uri);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }
    

    be sure to add a check if 'Telegram' is installed.