Telegram Bot PHP创建动态按钮

时间:2017-06-25 13:47:05

标签: php telegram-bot php-telegram-bot

我正在创建一个管理器机器人,列出某个渠道的所有管理员或机器人的所有管理员或机器人可以访问它们的所有渠道......

所以我创建了一个数据库,并从bot

成功连接到它

我要做的是:即我是机器人的经理,所以我发送" panel"对于机器人,它向我显示频道,管理员,更多设置

当我点击管理员时,我希望我的机器人在数据库中搜索我的管理员表,并在db中搜索每个用户名,创建一个按钮

即我在桌子上有5位管理员..我希望机器人创建5个不同的按钮('文本' = admin ['用户名'])当我触摸按钮时,它只是向我显示管理员的信息.....

所以我怎么能创建按钮?

<?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]
                                    );

                }

注意:我的代码只显示一个管理员或一个渠道

P.S:我不使用Telegram-bot-php

1 个答案:

答案 0 :(得分:0)

在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]);