Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create inline buttons in PHP Bot Telegram

I must program in php due to company needs... but I am working with php for first time... and it's the first time I am working with telegram bot :'( In some way, before, when i ran the command /start and doWork everything worked...

but now I must modify the bot, in a way that all commands are "hidden" behind some telegram button... Here how I edited my php page:

if(strpos($text, "/start") === 0)
{
    $response = "Ciao $firstname, benvenuto!";

    $keyboard = [
        'inline_keyboard' => [
            [
                ['text' => 'forward me to groups']
            ]
        ]
    ];

    $encodedKeyboard = json_encode($keyboard);
    $parameters = 
        array(
            'chat_id' => $chatId, 
            'text' => $response, 
            'reply_markup' => $encodedKeyboard
        );
    $parameters["method"] = "sendMessage";
    echo json_encode($parameters);

}

With BotFather I ran the command /setinline too...

So I think I am working how I parameters array.. can anyone help me please?

Ps.: (if can anyone suggest me also an IDE I work with please... i am using notepad++ now)

Thank you all

like image 768
Ciccio Avatar asked Aug 26 '17 10:08

Ciccio


2 Answers

First of all you don't need to use /setinline command in botFather. this command is for "inline mode" while you are using an inline_keyboard which is a custom keyboard in normal chat mode.

also you need to provide a callback_data in your keyboard array for each button:

$keyboard = [
    'inline_keyboard' => [
        [
            ['text' => 'forward me to groups', 'callback_data' => 'someString']
        ]
    ]
];
$encodedKeyboard = json_encode($keyboard);
$parameters = 
    array(
        'chat_id' => $chatId, 
        'text' => $response, 
        'reply_markup' => $encodedKeyboard
    );

send('sendMessage', $parameters); // function description Below

At last you need to send it via curl. here is a function i use in my codes:

function send($method, $data)
{
    $url = "https://api.telegram.org/bot<Bot-Token>". "/" . $method;

    if (!$curld = curl_init()) {
        exit;
    }
    curl_setopt($curld, CURLOPT_POST, true);
    curl_setopt($curld, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curld, CURLOPT_URL, $url);
    curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($curld);
    curl_close($curld);
    return $output;
}

P.S. I personally use PhpStorm, its nice ;)

like image 65
Majeed Askari Avatar answered Oct 29 '22 02:10

Majeed Askari


I have managed some function with https://github.com/irazasyed/telegram-bot-sdk if you don't want to manage row on inline keyword you can skip foreach and use $inline_keyboard[] = $keyword_data; to show keyword

check Image for $keyword_data array

$keyword_data array

 public function inlineKeyword($chat_id, $keyword_data, $msg = '') {
        if (empty($msg)) {
            $msg = "Select";
        }
        $inline_keyboard = array();
        $row = 0;
        $prev_value = '';
        foreach ($keyword_data as $value) {
            if (isset($prev_value['text']) && strlen($prev_value['text']) < 10 && strlen($value['text']) < 10) {
                $inline_keyboard[$row - 1][] = $value;
            } else {
                $inline_keyboard[$row][] = $value;
            }
            $prev_value = $value;
            $row++;
        }
//    $inline_keyboard[] = $keyword_data;
        $reply_markup = $this->telegram->replyKeyboardMarkup([
            'inline_keyboard' => $inline_keyboard,
            'resize_keyboard' => true
        ]);
        $response = $this->telegram->sendMessage([
            'text' => $msg,
            'reply_markup' => $reply_markup,
            'chat_id' => $chat_id
        ]);
    }

Function responce on telegram

like image 42
Bot Digit Avatar answered Oct 29 '22 02:10

Bot Digit