Search code examples
phpsqlinlinetelegramstring-concatenation

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


I'm programming a telegram bot that has inline buttons. To print inline buttons I first need to setup a "keyboard".

The "keyboard" is made of buttons that will appear when I print it.

An Inline keyboard example is this:

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

this keyboard visualizes 2 buttons (Menu and Carrello) on the first row and 1 on the second row (prezzario)

In my case I need to create a keyboard dynamically taking datas from my database


/*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");

In the code you see below I select only the elements I need and add them to my keyboard. The code works and if I try to print the keyboard it gets printed in the correct format.

When I want to use that as a keyboard tho my bot just prints "1". I have no clue where is this "1" coming from.


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();
    }

Also this is the function I use to edit a previous keyboard


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);
    }

I'd like the bot to use the keyboard I create [$tastieraFreddo] as an actual keyboard and print it as inline buttons

Thanks for the help :)


Solution

  • I solved the problem... Well basically it's an easy solution :p

    I'll paste the correct code

    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();
    }