电报机器人PHP创建动态按钮
我正在创建一个管理者机器人,列出某个频道的所有管理员或机器人的所有管理员或机器人有权访问的所有频道 所以我创建了一个数据库,并成功地从bot连接到它 我要做的是:也就是说,我是机器人的经理,所以我为机器人发送“面板”,它会显示频道、管理员和更多设置 当我点击管理员时,我希望我的机器人在数据库中搜索我的管理员表,并为数据库中的每个用户名创建一个按钮 i、 我有5个管理员在桌子上。。我想机器人创建5个不同的按钮('text'=admin['username']),当我触摸一个按钮时,它只会显示管理员的信息 那么我如何才能创建按钮电报机器人PHP创建动态按钮,php,telegram-bot,php-telegram-bot,Php,Telegram Bot,Php Telegram Bot,我正在创建一个管理者机器人,列出某个频道的所有管理员或机器人的所有管理员或机器人有权访问的所有频道 所以我创建了一个数据库,并成功地从bot连接到它 我要做的是:也就是说,我是机器人的经理,所以我为机器人发送“面板”,它会显示频道、管理员和更多设置 当我点击管理员时,我希望我的机器人在数据库中搜索我的管理员表,并为数据库中的每个用户名创建一个按钮 i、 我有5个管理员在桌子上。。我想机器人创建5个不同的按钮('text'=admin['username']),当我触摸一个按钮时,它只会显示管理员
<?php
define ('API_KEY','MY_BOT_TOKEN');
//----######------
function makereq($method,$datas=[]){
$url = "https://api.telegram.org/bot".API_KEY."/".$method;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($datas));
$res = curl_exec($ch);
if(curl_error($ch)){
var_dump(curl_error($ch));
}else{
return json_decode($res);
}
}
//---------
$update = json_decode(file_get_contents('php://input'));
var_dump($update);
//=========
$chat_id = $update->message->chat->id;
$message_id = $update->message->message_id;
$from_id = $update->message->from->id;
$name = $update->message->from->first_name;
$contact = $update->message->contact;
$cnumber = $update->message->contact->phone_number;
$cname = $update->message->contact->first_name;
$photo = $update->message->photo;
$video = $update->message->video;
$sticker = $update->message->sticker;
$file = $update->message->document;
$music = $update->message->audio;
$voice = $update->message->voice;
$forward = $update->message->forward_from;
$conn = mysqli_connect("localhost","id2056593_root","123456","id2056593_botdb");
$username = $update->message->from->username;
$textmessage = isset($update->message->text)?$update->message->text:'';
$reply = $update->message->reply_to_message->forward_from->id;
$stickerid = $update->message->reply_to_message->sticker->file_id;
//-----------
if($textmessage == "panel" || $textmessage == "Panel"){
var_dump(makereq('sendMessage',[
'chat_id'=>$update->message->chat->id,
'text'=>"Welcome to the pannel",
'parse_mode'=>'MarkDown',
'reply_markup'=>json_encode([
'keyboard'=>[
[
['text'=>"Channels"],['text'=>"Admins"]
],
[
['text'=>"More Settings"]
]
],
'resize_keyboard'=>true
])
]));
}elseif($textmessage == "Channels"){
$sql = "SELECT * FROM channels";
$result = mysqli_query($conn,$sql);
$text = "channel id :\n";
while ($row = mysqli_fetch_assoc($result)) {
$keyboard =json_encode([
'keyboard'=>[
[
['text'=>"$row[username]"]
]]]);
}
makereq('sendMessage',[
'chat_id'=>$update->message->chat->id,
'text'=>"Pick up one channel from above",
'reply_markup'=>$keyboard]
);
}elseif($textmessage == "Admins"|| $textmessage =="admins"){
$sql = "SELECT * FROM admin";
$result = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_assoc($result)) {
$keyboard =json_encode([
'keyboard'=>[
[
['text'=>"$row[username]"]
]]]);
}
makereq('sendMessage',[
'chat_id'=>$update->message->chat->id,
'text'=>"Pick up one admin",
'reply_markup'=>$keyboard]
);
}
在while循环中,替换$keyboard变量。
尝试更改您的代码:
while ($row = mysqli_fetch_assoc($result)) {
$keyboard =json_encode([
'keyboard'=>[
[
['text'=> "$row[username]"]
]]]);
}
通过以下代码:
$keyboard = [];
while ($row = mysqli_fetch_assoc($result)) {
$keyboard[] = [
['text'=> $row[username]]
];
}
$reply_markup = json_encode(['keyboard'=> $keyboard]);
在while循环中的代码中,替换$keyboard变量。
尝试更改您的代码:
while ($row = mysqli_fetch_assoc($result)) {
$keyboard =json_encode([
'keyboard'=>[
[
['text'=> "$row[username]"]
]]]);
}
通过以下代码:
$keyboard = [];
while ($row = mysqli_fetch_assoc($result)) {
$keyboard[] = [
['text'=> $row[username]]
];
}
$reply_markup = json_encode(['keyboard'=> $keyboard]);