Android App集成Telegram Bot登录:非App适配站点对接咨询
Hey fellow dev! Great that you already have a working DeepLink set up—let's walk through how to tie everything together with the Telegram Bot flow you described.
Step 1: Ensure the Site's Login Link Triggers Your DeepLink
First, double-check that the login link generated by the external site uses your app's DeepLink scheme/host exactly as configured in your AndroidManifest.xml. For example, if your DeepLink is yourapp://auth, the site should generate a link like yourapp://auth?user_id=123&username=JohnDoe to pass the user data.
In your manifest, confirm your intent-filter is properly set up to catch this:
<activity android:name=".DeepLinkHandlerActivity"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="yourapp" android:host="auth" /> </intent-filter> </activity>
Step 2: Parse DeepLink Parameters in Your App
In your DeepLinkHandlerActivity, extract the user data from the incoming intent:
Uri incomingUri = getIntent().getData(); if (incomingUri != null) { String userId = incomingUri.getQueryParameter("user_id"); String username = incomingUri.getQueryParameter("username"); // Store these values temporarily (e.g., in SharedPreferences or a ViewModel) }
Step 3: Load the External Site in a WebView
Since the site isn't built for mobile apps, a WebView is the best way to embed it. Create a WebViewActivity and pass the parsed user parameters to the site:
- Option 1: Pass parameters as URL query strings
String targetUrl = "https://external-site.com/login?user_id=" + userId + "&username=" + username; WebView webView = findViewById(R.id.webview); webView.loadUrl(targetUrl); - Option 2: Use POST request (if the site supports it)
If the site expects data via POST, usewebView.postUrl()instead:String postData = "user_id=" + userId + "&username=" + username; webView.postUrl("https://external-site.com/login", postData.getBytes());
Configure the WebView to keep navigation within your app:
webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { // Keep internal site navigation in the WebView view.loadUrl(request.getUrl().toString()); return true; } }); // Enable JavaScript if the site requires it WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true);
Step 4: Sync Login State (Optional)
To avoid forcing users to log in again in the WebView, sync cookies between the WebView and your app:
CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptCookie(true); // If you have auth tokens from your app, you can inject them into the WebView's cookies cookieManager.setCookie("https://external-site.com", "auth_token=your_app_token");
Step 5: Test the End-to-End Flow
- Have a user send
/startto your Telegram Bot, then tap the login link. Verify it opens your app instead of the browser. - Check that the user ID and username are correctly parsed and passed to the WebView-loaded site.
- Test edge cases: what if the link has missing parameters? Add error handling to show a friendly message to users.
Bonus: Security & UX Tips
- Validate DeepLink sources: Add checks to ensure the incoming link comes from your trusted external site (e.g., verify signed parameters if the site supports it).
- Handle WebView errors: Create a custom error page to show when the site fails to load, instead of the default WebView error screen.
- Request permissions: If the site needs access to camera, storage, etc., make sure your app has the necessary permissions, and configure
WebChromeClientto handle permission requests.
内容的提问来源于stack exchange,提问作者raza Amini




