我需要动态创建一个字符串以将其打印为电报上的内联键盘

I need to create a string dynamically to print it as an Inline keyboard on telegram

我正在编写一个具有内联按钮的电报机器人。 要打印内联按钮,我首先需要设置一个 "keyboard".

"keyboard" 由打印时会出现的按钮组成。

内联键盘示例是这样的:

$tastieraStart='[{"text":"Menu","callback_data":"StampaMenu"},{"text":"Carrello","callback_data":"VisualizzaCarrello"}],[{"text":"Prezzario","callback_data":"Prezzario"}]';

此键盘在第一行可视化 2 个按钮(菜单和 Carrello) 第二行 1 个 (prezzario)

在我的例子中,我需要创建一个动态键盘,从我的数据库中获取数据


/*THIS IS HOW I USUALLY PRINT A BOT INLINE KEYBOARD*/

$tastieraStart='[{"text":"Menu\n'.$menu.'","callback_data":"StampaMenu"},{"text":"Carrello\n'.$carrello.'","callback_data":"VisualizzaCarrello"}],[{"text":"Prezzario\n'.$prezzario.'","callback_data":"Prezzario"}]';
editMessageText($queryUserId,$querymsgid,"Benvenuto ".$name.", da oggi sarò il tuo barista personale! \xF0\x9F\x98\x89 \nCome posso servirti?",$tastieraStart,"inline");

在下面的代码中,我 select 只添加了我需要的元素并将它们添加到我的键盘。 代码有效,如果我尝试打印键盘,它会以正确的格式打印。

当我想将其用作键盘时,我的机器人只打印“1”。 我不知道这个“1”是从哪里来的。


if($querydata=="Freddo")
    {

        $CONT="SELECT COUNT(*) AS totale FROM ListinoProdotti WHERE categoria='freddo'";
        $resultCONT=$conn->query($CONT);
        $row = $resultCONT->fetch_assoc();
        $COUNT=$row['totale'];
        editMessageText($queryUserId,$querymsgid,$COUNT);


        $QueryFreddo="SELECT * FROM ListinoProdotti WHERE categoria='freddo'";
        $resultFreddo=$conn->query($QueryFreddo);  
        $row = $resultFreddo->fetch_assoc();
        $tastieraTemp="'";
        for($i=0;$i<$COUNT;$i++)
        {

            $prezzoTemp=$row['prezzo'];

            $prodottoTemp=$row['prodotto'];

            $tastieraTemp=$tastieraTemp."[{'text':\"".$prodottoTemp.$prezzoTemp."\",'callback_data':\"POSVER\"}]";

            if($i<=$resultCONT)
            {
                $tastieraTemp=$tastieraTemp."'";
            }
            else
            {
                $tastieraTemp=$tastieraTemp.",";
            }

        }
        $tastieraFreddo=$tastieraTemp;

        editMessageText($queryUserId,$querymsgid,"Seleziona ciò che desideri ordinare:freddo",$tastieraFreddo,"inline");
        exit();
    }

这也是我用来编辑以前键盘的功能


function editMessageText($chatId,$message_id,$newText,$tastiera,$tipo)
    {
    if(isset($tastiera))
      {
        if($tipo=="fisica")
        {
            $tastierino='&reply_markup={"keyboard":['.$tastiera.'],"resize_keyboard":true}';
        }
        else
        {
            $tastierino='&reply_markup={"inline_keyboard":['.$tastiera.'],"resize_keyboard":true}';
        }
      }
        $url = $GLOBALS[website]."/editMessageText?chat_id=".$chatId."&message_id=".$message_id."&text=".urlencode($newText).$tastierino;
      file_get_contents($url);
    }

我希望机器人使用我创建的键盘 [$tastieraFreddo] 作为实际键盘并将其打印为内联按钮

感谢您的帮助:)

我解决了问题... 好吧,基本上这是一个简单的解决方案:p

我会粘贴正确的代码

if($querydata=="Freddo")
{

    $CONT="SELECT COUNT(*) AS totale FROM ListinoProdotti WHERE categoria='freddo'";
    $resultCONT=$conn->query($CONT);
    $row = $resultCONT->fetch_assoc();
    $COUNT=$row['totale'];
    editMessageText($queryUserId,$querymsgid,$COUNT);


    $QueryFreddo="SELECT * FROM ListinoProdotti WHERE categoria='freddo'";
    $resultFreddo=$conn->query($QueryFreddo);  
    $row = $resultFreddo->fetch_assoc();
    //$tastieraTemp="'";
    for($i=0;$i<$COUNT;$i++)
    {

        $prezzoTemp=$row['prezzo'];

        $prodottoTemp=$row['prodotto'];

        $tastieraTemp=$tastieraTemp."[{\"text\":\"".$prodottoTemp.$prezzoTemp."\",\"callback_data\":\"POSVER\"}]";

        if($i<=$resultCONT)
        {
            $tastieraTemp=$tastieraTemp."";
        }
        else
        {
            $tastieraTemp=$tastieraTemp.",";
        }

    }
    $tastieraFreddo=$tastieraTemp;

    editMessageText($queryUserId,$querymsgid,"Seleziona ciò che desideri ordinare:freddo",$tastieraFreddo,"inline");
    exit();
}