【问题标题】:Telegram Bot PHP Create Dynamic ButtonsTelegram Bot PHP 创建动态按钮
【发布时间】:2017-06-25 13:47:05
【问题描述】:

我正在创建一个经理机器人,其中列出了某个频道的所有管理员或机器人的所有经理或机器人有权访问他们的所有频道...

所以我创建了一个数据库并成功地从机器人连接到它

我要做的是:即我是机器人的经理,所以我为机器人发送“面板”,它向我显示频道、管理员、更多设置

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

即我在表中有 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]
                                    );

                }

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

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-04
      • 2018-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      • 2017-09-26
      • 1970-01-01
      相关资源
      最近更新 更多