S
S
sham632019-12-10 16:14:07
API
sham63, 2019-12-10 16:14:07

InlineKeyboardMarkup Telegram. What could I be missing?

Wrote a simple button generator. At the input we give $tg->GenKeyboard2("#string1# #string1# ? #string2# #string2#");
(the code)

public function GenKeyboard2($data)
    {
        $data = trim($data);
        $mas = explode("#", $data);
        $i=0;$j=0;$j_=0;
         foreach ($mas as  $key => $value) {
            if(!empty(trim($value)))
            {

                if(strpos($value, "?")===FALSE)
                {  
                    $arInfo["inline_keyboard"][$i][$j]["text"] = $value;  
                    $j++; 
                    
                }
                else { $i++; $j=0; }
                
            }
          }
        return $arInfo;
    }

At the output we get
array(1) {
  ["inline_keyboard"]=>
  array(2) {
    [0]=>
    array(2) {
      [0]=>
      array(1) {
        ["text"]=>
        string(13) "строка1"
      }
      [1]=>
      array(1) {
        ["text"]=>
        string(13) "строка1"
      }
    }
    [1]=>
    array(2) {
      [0]=>
      array(1) {
        ["text"]=>
        string(13) "строка2"
      }
      [1]=>
      array(1) {
        ["text"]=>
        string(13) "строка2"
      }
    }
  }
}

Next, I try to send this case to telegram as follows:
public function send($id, $message,$keyboard='') 
    {   
    
    if($keyboard == "DEL"){		
      $keyboard = array(
        'remove_keyboard' => true
      );
    }
    if($keyboard){
      $encodedMarkup = json_encode($keyboard);
      
      $data = array(
        'chat_id'      => $id,
        'text'     => $message,
        'reply_markup' => $encodedMarkup
      );
    }else{
      $data = array(
        'chat_id'      => $id,
        'text'     => $message
      );
    }
       
        $out = $this->request('sendMessage', $data);       
        return $out;
    }

With simple buttons (ReplyKeyboardMarkup) everything works fine, but it doesn't want to with InlineKeyboardMarkup...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Fobius, 2019-12-11
@phobious

can help, I personally wrote this
via NuGet connected Telegram.Bot
and the script itself:
switch (message.Text)
{
case "/btn":
var inlineKeyboard = new InlineKeyboardMarkup(new[]
{
new[]
{
InlineKeyboardButton.WithUrl("link ", "text"),
},
new[]
{
InlineKeyboardButton.WithCallbackData("button2"),
}
});
await Bot.SendTextMessageAsync(message.From.Id, "available buttons",
replyMarkup: inlineKeyboard);
break;
default:
break;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question