PHP开发Telegram机器人:如何以Web App打开URL而非外部链接
实现Telegram机器人按钮在内部打开Web App(小程序)
要让按钮在Telegram内部以Web App形式打开,不能用普通的url按钮,必须使用Telegram Bot API的Web App按钮类型,同时要完成两个关键步骤:BotFather域名白名单配置,以及代码中按钮结构的修改。
1. 先在BotFather配置Web App域名白名单
- 给BotFather发送
/mybots,选择你的目标机器人 - 进入
Bot Settings->Web Apps(部分版本路径为Menu Button下的Configure Web App) - 将你的商店域名(比如
https://example.com)添加到允许的域名列表中,只有白名单内的域名才能在Telegram内部加载Web App
2. 修改PHP代码中的按钮配置
核心是把sendStore函数里的url字段替换成web_app字段,具体修改如下:
修改后的sendStore函数
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', 'web_app' => [ '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); }
完整修改后的代码
<?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 .= " Number 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', 'web_app' => [ '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); } ?>
3. 额外注意事项
你的商店页面需要引入Telegram官方的Web App JS库,才能正常和Telegram客户端交互(比如获取用户信息、适配页面尺寸等),在页面的<head>标签内添加:
<script src="https://telegram.org/js/telegram-web-app.js"></script>
内容的提问来源于stack exchange,提问作者Ahmad Karimi
相关产品推荐
相关产品推荐

