Skip to content

Instantly share code, notes, and snippets.

@adityaruplaha
Last active August 22, 2023 11:57
Show Gist options
  • Save adityaruplaha/d60c8c32062889ca49524e24788e873e to your computer and use it in GitHub Desktop.
Save adityaruplaha/d60c8c32062889ca49524e24788e873e to your computer and use it in GitHub Desktop.
Telegram Login Widget not working on certain browsers. - Investgation and workarounds.

Telegram Login Widget not working on certain browsers.

First reported on 22 October 2020 IST (https://t.me/BotTalk/357429) by @adityaruplaha (myself :P)

Issue

(haven't got around to filling this yet, please check the messages in the linked chat)

Workaround

This code example demonstates how to use LoginUrl in a UserCommand to work-around Telegram Login Widget not working on certain browsers. It uses PHP with the official Telegram PHP Bot API, but other languages should work similarly.

The user gets a prompt if they want to login, then the callback URL opens up with the required parameters, if the user accepts. Otherwise, it gets opened with no parameters. You can then handle it accordingly. Check https://core.telegram.org/bots/api#loginurl for details.

<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\InlineKeyboard;
use Longman\TelegramBot\Entities\InlineKeyboardButton;
use Longman\TelegramBot\Entities\LoginUrl;
use Longman\TelegramBot\Request;
/**
* User "/login" command
*/
class LoginCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'login';
/**
* @var string
*/
protected $description = 'Login to website.';
/**
* @var string
*/
protected $usage = '/login';
/**
* @var string
*/
protected $version = '1.0.0';
/**
* @var bool
*/
protected $private_only = true;
/**
* Command execute method
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function execute()
{
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$id = $message->getFrom()->getId();
// See https://core.telegram.org/bots/api#loginurl for details.
$login = new LoginUrl([
'url' => 'https://example.com/login.php',
'request_write_access' => true
]);
// See https://core.telegram.org/bots/api#inlinekeyboardbutton for details.
$login_button = new InlineKeyboardButton([
'text' => 'Login',
'login_url' => $login
]);
$data = [
'chat_id' => $chat_id,
'text' => "Press the button below to login.",
'reply_to_message_id' => $message->getMessageId(),
'reply_markup' => new InlineKeyboard([
'inline_keyboard' => array(array($login_button))
])
];
return Request::sendMessage($data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment