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

问题描述 投票:0回答:1

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

“键盘”由我打印时出现的按钮组成。

内联键盘示例如下:

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

这个键盘可以看到第一行的2个按钮(Menu和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");

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

当我想用它作为键盘时,我的机器人只打印“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]作为实际键盘并将其打印为内联按钮

谢谢您的帮助 :)

php sql inline telegram string-concatenation
1个回答
0
投票

我解决了这个问题......基本上这是一个简单的解决方案: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();
}
© www.soinside.com 2019 - 2024. All rights reserved.