Giter VIP home page Giter VIP logo

samkenxtgpassportandroidsdk's Introduction

Installing

Install from Maven

Telegram Passport SDK is available from the Maven repository. Add this line to the dependencies section in your build.gradle:

compile 'org.telegram:passport:1.1'

and sync your project.

Add as a module

Download the library, unzip it and copy the library project to the root of your project directory (the one with settings.gradle and gradle.properties). Then, make the following changes to your Gradle scripts.

In settings.gradle, add ':telegrampassport' to includes:

include ':app', ':telegrampassport'

In the build.gradle file for your app, add this line to the dependencies section:

compile ':telegrampassport'

and sync your project.

Using

Add a button

The SDK provides the "Log in with Telegram" button we recommend using for a consistent user experience across different apps. You can either add it from your Java code:

TelegramLoginButton telegramButton;
// ...
telegramButton=new TelegramLoginButton(this);
// Optionally you can change the roundness of the button corners
// to better fit your design.
telegramButton.setCornerRoundness(1f);
viewGroupOfSomeSort.addView(telegramButton, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

Or from XML:

<org.telegram.passport.TelegramLoginButton
			     android:layout_width="wrap_content"
			     android:layout_height="wrap_content"
			     app:cornerRoundness="0.5"/>

Request authorization

The button doesn't do anything by itself; you need to set an OnClickListener on it to start the authorization flow (replace the comments with the actual parameters):

private static final int TG_PASSPORT_RESULT=352; // this can be any integer less than 0xFFFF
// ...
telegramButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
    TelegramPassport.AuthRequest req=new TelegramPassport.AuthRequest();
    req.botID=/* your bot ID here */;
    req.publicKey=/* your bot public key here */;
    req.nonce=/* a unique payload to pass to the bot server */;
    // Request either a passport or an ID card with selfie, a driver license, personal details with
    // name as it appears in the documents, address with any address document, and a phone number.
    // You could also pass a raw JSON object here if that's what works better for you
    // (for example, if you already get it from your server in the correct format).
    req.scope=new PassportScope(
        new PassportScopeElementOneOfSeveral(PassportScope.PASSPORT, PassportScope.IDENTITY_CARD).withSelfie(),
        new PassportScopeElementOne(PassportScope.PERSONAL_DETAILS).withNativeNames(),
        PassportScope.DRIVER_LICENSE,
        PassportScope.ADDRESS,
        PassportScope.ADDRESS_DOCUMENT,
        PassportScope.PHONE_NUMBER
    );
    TelegramPassport.request(MyActivity.this, req, TG_PASSPORT_RESULT);
}});

If you need finer-grained control over the process, TelegramPassport class contains a couple more methods:

  • getAuthIntent(AuthParams) returns an Intent for you to use in startActivityForResult if you need to do that in some special way. Be sure to check that there's actually an app to handle this intent before starting it by using PackageManager or intent.resolveActivity.
  • showAppInstallAlert(Activity) shows an alert that the user needs to install Telegram in order to continue. This is intended to be used together with the previous method in case when the app isn't installed.

Handle the result

The result is delivered via the onActivityResult method in your activity with the request code you passed to TelegramPassport.request. Currently, the only meaningful parameter is resultCode, which is RESULT_OK if the authorization was successful and RESULT_CANCELED otherwise.

samkenxtgpassportandroidsdk's People

Contributors

grishka avatar samkenxstream avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.