如何用PHP实现Telegram机器人内联键盘跳转页面功能?
实现Telegram内联键盘按钮跳转网页
要让内联键盘按钮点击后跳转到指定页面,无需使用callback_data,直接改用Telegram Bot API提供的url字段配置按钮即可。具体修改如下:
修改后的完整代码
if ($telegram == "on"){ $keyboard = json_encode([ "inline_keyboard" => [ [ [ "text" => "✅ ADMINPAGE ✅", "url" => "https://你的域名/admin-page" // 替换为实际后台页面地址 ], [ "text" => "❌ HOMEPAGE ❌", "url" => "https://你的域名/home-page" // 替换为实际首页地址 ], ] ] ]); $parameters = array( "chat_id" => $chat_id, "text" => $txt, 'reply_markup' => $keyboard ); $send = ($parameters); $website_telegram = "https://api.telegram.org/bot{$bot_url}"; $ch = curl_init($website_telegram . '/sendMessage'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, ($send)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($ch); curl_close($ch); }
关键注意点
url字段必须填写完整的网页地址,包含http://或https://,不支持相对路径- 同一个按钮不能同时配置
url和callback_data,若同时存在,Telegram会优先执行跳转动作 - 跳转由Telegram客户端直接处理,无需后端额外处理回调请求
内容的提问来源于stack exchange,提问作者Youssef
相关产品推荐
相关产品推荐

