duandao1931 2019-04-23 22:05
浏览 82

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

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
'.$menu.'","callback_data":"StampaMenu"},{"text":"Carrello
'.$carrello.'","callback_data":"VisualizzaCarrello"}],[{"text":"Prezzario
'.$prezzario.'","callback_data":"Prezzario"}]';
editMessageText($queryUserId,$querymsgid,"Benvenuto ".$name.", da oggi sarò il tuo barista personale! \xF0\x9F\x98\x89 
Come 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 :)

  • 写回答

1条回答 默认 最新

  • doushan9415 2019-04-23 23:12
    关注

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

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)