TLP Masih
TLP Masih

Reputation: 556

PHP / Telegram Bot: How to open a URL as Web App not an external Link?

I am creating a Telegram bot for my store with PHP.

Currently I have created the bot, used webhook URL to introduce my bot's PHP file to Telegram. My code almost works and can respond commands I defined, but I have a button/command "Open the store". When we click this button, a message will send to user with this content "Click the button below to open the store:" and a button bellow the message with name "Open store".

In my code I set this button's link $storeURL = 'https://example.com/?username='.$username.'&uid='.$userID; and it works good and could get username and used ID.

The problem is this button will show a pupop with message "Do you want the link? https://example.com/?username=xxxxxxx&uid=1234678", And it will open this link in external browser.

But I need to open this button like a Web App (Mini-App) inside of Telegram not as an external link.

In BotFather I can set a custom URL and Custom Text for menu, this URL open like a webview/internal mini-app. And exactly I need to open the "Open store" button like a mini-app.

How is this possible? I just saw some PHP bots can do it. My website is coded with PHP so for this reason I want to use PHP for my bot. This is my PHP code for bot file in my website:

<?php

$botToken = '6877041912:AAE-LsUBqjuurlBSFA6l6jU_RcbdFO_t000';
$telegramApi = 'https://api.telegram.org/bot'.$botToken;

$servername = 'localhost';
$username = 'epfnwgng_farmms';
$password = 'BappJ5CstjY6';
$dbname = 'epfnwgng_farmdbn';

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die('Connection failed: '.$conn->connect_error);
}

$update = json_decode(file_get_contents('php://input'), true);

if (isset($update['message'])) {
    
$chatID = $update['message']['chat']['id'];
$messageText = $update['message']['text'];
$userID = $update['message']['from']['id'];
$username = $update['message']['from']['username'];

switch ($messageText) {
    case '/start':
        sendMessage($chatID, 'Welcome to the Telegram mini app bot! Choose an option below:');
        sendKeyboard($chatID);
        break;
    case 'Open store':
    $storeURL = 'https://example.com/?username='.$username.'&uid='.$userID;
    sendStore($chatID, $storeURL);
    break;
    case 'Referred members':
        $referredMembersCount = getReferredMembersCount($chatID);
        sendMessage($chatID, 'Number of referred members: '.$referredMembersCount);
        break;
    case 'Refer and point':
        $referLink = 'https://t.me/xxxxxxxx_bot?startuid='.$chatID;
        $message = 'Your referral link is '.$referLink.'. Share it and earn points for each user.';
        $referredPlayersCount = getReferredPlayersCount($chatID);
        $message .= "\n\nNumber of players with your UID in the game: ".$referredPlayersCount;
        sendMessage($chatID, $message);
        break;
    default:
        sendMessage($chatID, 'Invalid command. Please choose an option from the keyboard.');
        sendKeyboard($chatID);
}
}

function insertPlayerData($username, $userID) {
    global $conn;
    
    $check_sql = "SELECT * FROM players WHERE username = '$username'";
    $check_result = $conn->query($check_sql);
    
    if ($check_result->num_rows == 0) {
        $sql = "INSERT INTO players (username, UID) VALUES ('$username', '$userID')";
        $conn->query($sql);
    }
}

function getReferredMembersCount($chatID) {
    global $conn;
    $sql = "SELECT COUNT(*) AS referred_count FROM players WHERE referreduid = $chatID";
    $result = $conn->query($sql);
    $row = $result->fetch_assoc();
    return $row['referred_count'];
}

function getReferredPlayersCount($chatID) {
    global $conn;
    $sql = "SELECT COUNT(*) AS referred_players_count FROM players WHERE referreduid = $chatID";
    $result = $conn->query($sql);
    $row = $result->fetch_assoc();
    return $row['referred_players_count'];
}

function sendMessage($chatID, $message) {
    global $telegramApi;
    
    $data = [
        'chat_id' => $chatID,
        'text' => $message
    ];

    $ch = curl_init($telegramApi.'/sendMessage');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_exec($ch);
    curl_close($ch);
}

function sendStore($chatID, $storeURL) {
    global $telegramApi;
    $data = [
        'chat_id' => $chatID,
        'text' => 'Click the button below to open the store:',
        'reply_markup' => json_encode([
            'inline_keyboard' => [
                [
                    [
                        'text' => 'Open store',
                        'url' => $storeURL
                    ]
                ]
            ]
        ])
    ];
    $ch = curl_init($telegramApi . '/sendMessage');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_exec($ch);
    curl_close($ch);
}


function sendKeyboard($chatID) {
    global $telegramApi;
    $keyboard = [
        ['Open the Store'],
        ['Help', 'Support'],
        ['Referred members', 'Refer and point']
    ];
    $replyMarkup = [
        'keyboard' => $keyboard,
        'resize_keyboard' => true,
        'one_time_keyboard' => false
    ];
    $data = [
        'chat_id' => $chatID,
        'text' => 'Choose an option:',
        'reply_markup' => json_encode($replyMarkup)
    ];

    $ch = curl_init($telegramApi.'/sendMessage');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_exec($ch);
    curl_close($ch);
}

?>

Upvotes: 1

Views: 1535

Answers (0)

Related Questions