Fluent Keyboard builder for php-telegram-bot.

Overview

Version PHP Version Bot API Tests

Table of Contents
  1. Installation
  2. Usage
    1. Defining a Keyboard
    2. Defining Buttons
    3. Bind Buttons to a Keyboard
      1. By Row
      2. By Button
      3. As Stack
    4. ForceReply and ReplyKeyboardRemove
    5. KeyboardButtonPollType

Installation

Install the package using composer:

composer require php-telegram-bot/fluent-keyboard 

(back to top)

Usage

If you need to create a keyboard you can use the classes provided by this package as a drop-in replacement.

This is best explained with an example:

Request::sendMessage([
    'chat_id'      => 12345,
    'text'         => 'Keyboard Example',
    'reply_markup' => ReplyKeyboardMarkup::make()
        ->oneTimeKeyboard()
        ->button(KeyboardButton::make('Cancel'))
        ->button(KeyboardButton::make('OK')),
]);

A ReplyKeyboardMarkup is created by calling the static make() method on ReplyKeyboardMarkup. After that every field, like one_time_keyboard, can be chained by calling it in camelCase. Buttons can be added by calling the button() method. We have a detailed look on that later.

The classes and fields are named after the corresponding types and fields of the Telegram Bot API.

(back to top)

Defining a Keyboard

You can create a keyboard by calling the static make() method on its class.

After that you can chain methods to set additional fields that are available in the Bot API. This is done by calling the field name in camelCase. So instead of input_field_placeholder, you need to call inputFieldPlaceholder().

ReplyKeyboardMarkup::make()
    ->inputFieldPlaceholder('Placeholder');

(back to top)

Defining Buttons

The Buttons are created in the same way:

KeyboardButton::make()
    ->text('Send my Contact')
    ->requestContact();

As a shortcut, you can pass the mandatory text field as an argument to the static method make() like this:

KeyboardButton::make('Send my Location')
    ->requestLocation();

This is done the same way for InlineKeyboardButton:

InlineKeyboardButton::make('Login')
    ->loginUrl(['url' => 'https://example.com']);

To find out which fields are available have a look at the Bot API documentation.

(back to top)

Bind Buttons to a Keyboard

The keyboard does not work without any buttons, so you need to pass the buttons to the keyboard. There are a few ways to do this.

By Row

ReplyKeyboardMarkup::make()
    ->row([
        KeyboardButton::make('Cancel'),
        KeyboardButton::make('OK')
    ]);

If you need more than one row, call row() multiple times:

InlineKeyboardMarkup::make()
    ->row([
        InlineKeyboardButton::make('1')->callbackData('page-1'),
        InlineKeyboardButton::make('2')->callbackData('page-2'),
        InlineKeyboardButton::make('3')->callbackData('page-3')
    ])
    ->row([
        InlineKeyboardButton::make('prev')->callbackData('page-prev'),
        InlineKeyboardButton::make('next')->callbackData('page-next')
    ]);

InlineKeyboard with multiple rows

By Button

ReplyKeyboardMarkup::make()
    ->button(KeyboardButton::make('First Button'))
    ->button(KeyboardButton::make('Second Button'));

If you need more than one row, just call the row method without arguments, and continue calling button():

InlineKeyboardMarkup::make()
    ->button(InlineKeyboardButton::make('A')->callbackData('answer-a'))
    ->button(InlineKeyboardButton::make('B')->callbackData('answer-b'))
    ->row()
    ->button(InlineKeyboardButton::make('C')->callbackData('answer-c'))
    ->button(InlineKeyboardButton::make('D')->callbackData('answer-d'));

InlineKeyboard with multiline buttons

It's up to you if you define your buttons inline like in these examples or if you'd like to generate a whole row beforehand and pass the variable to the row() method.

As Stack

If you want to add a bunch of buttons that have each a row for themselves you can use the stack() method.

InlineKeyboardMarkup::make()
    ->stack([
        InlineKeyboardButton::make('Login')->loginUrl('https://example.com/login'),
        InlineKeyboardButton::make('Visit Homepage')->url('https://example.com')
    ]);

InlineKeyboard with stack

You can mix and match the row(), stack() and button() methods as it fits your needs.

(back to top)

ForceReply and ReplyKeyboardRemove

ForceReply and ReplyKeyboardRemove can be used the same way as a normal keyboard, but they do not receive any buttons:

$this->replyToUser('Thank you', [
    'reply_markup' => ReplyKeyboardRemove::make()->selective(),
]);
$data['reply_markup'] = ForceReply::make()->inputFieldPlaceholder('Please type something...');

(back to top)

KeyboardButtonPollType

The request_poll field is a little special. You can specify which poll type the user can create by passing a KeyboardButtonPollType object.

KeyboardButton::make()->requestPoll(KeyboardButtonPollType::regular())

The KeyboardButtonPollType class has static methods for each possible type. But if there are new types in the future you don't have to wait until we release an update. You can either pass the array structure directly to the requestPoll() method or you pass the array structure to the constructor of KeyboardButtonPollType.

$pollButton = new KeyboardButtonPollType([
    'type' => 'quiz'
]);
You might also like...
A tool for sending fast and managed messages to Telegram bot users

👋🏻 HiToAll A tool for sending fast and managed messages to Telegram bot users About In some telegram bots programmed with php language, if there are

telegram bot for sell virtual number

Telefake Telegram bot for sell virtual number (fully automatic) Installation create bot in telegram upload script in website with php and ssl (https)

laravel package untuk memudahkan penggunaan MCA dengan Telegram Bot USDI di aplikasi Universitas Udayana.

MCA KubeMQ Laravel laravel package untuk memudahkan penggunaan MCA dengan Telegram Bot USDI di aplikasi Universitas Udayana. Motivasi Proyek ini berfu

 Telegram bot for CC Checker with hella lotta features.
Telegram bot for CC Checker with hella lotta features.

SDMN CHECKER BOT Telegram bot for CC Checker with hella lotta features. 📝 Table of Contents Dependencies / Limitations Future Scope Setting up a Hero

Create and manage advanced polls with this Telegram Bot which has many features available!
Create and manage advanced polls with this Telegram Bot which has many features available!

MasterPollBot Create and manage advanced polls with this Telegram Bot which has many features available! Requirements Local Telegram Bot API or a webh

Bin checker telegram bot
Bin checker telegram bot

BIN Checker Bot Test Bot: Bin Checker Bot Env API_TOKEN Bot token. START_MSG Custom start message. Deploy Heroku Railway ⚠ Warn: Still not Tested on R

This app remembers todo list in every specified time (e. every minute, hour, day etc) through telegram bot

remember_todo_list This app remembers todo list in every specified time (e. every minute, hour, day etc) through telegram bot This project includes tw

The Telegram bot framework that doesn't drive you nuts.
The Telegram bot framework that doesn't drive you nuts.

Nutgram The Telegram bot framework that doesn't drive you nuts This framework takes advantage of the latest PHP 8 features, and tries to make the spee

Gateway-pay-bot - Telegram robot for donating or paying + authentication

gateway-pay-bot این ربات جهت دونیت شدن یا پرداخت های شخصی نوشته شده است دارای قابلیت احراز هویت است درگاه های متصل به ربات : زرین پال نکست پی ایدی پی

Releases(v1.2.0)
  • v1.2.0(Apr 20, 2022)

  • v1.1.0(Mar 24, 2022)

    Full Changelog: https://github.com/php-telegram-bot/fluent-keyboard/compare/v1.0.1...v1.1.0

    Changelog

    • Bumped Minimum PHP Version to PHP 8.0
    • Added an intermediate Button class for better type-hinting
    • Added convenience behavior to InlineKeyboardButton::loginUrl() method
      • it's possible to pass the loginUrl directly as a string and it gets converted to the mandatory array structure automatically.
    • Added better type-hinting made possible with PHP 8
    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(Mar 24, 2022)

    First release

    Since there were no previous releases there is no need for a changelog. But you can read all about the current feature set by visiting the README in the project repo.

    Source code(tar.gz)
    Source code(zip)
Owner
PHP Telegram Bot
PHP Telegram Bot framework/library
PHP Telegram Bot
PHP Telegram Bot based on the official Telegram Bot API

PHP Telegram Bot based on the official Telegram Bot API

null 4 Dec 8, 2021
🤖 Telegram Bot API PHP SDK. Lets you build Telegram Bots easily! Supports Laravel out of the box.

Telegram Bot API - PHP SDK Telegram Bot PHP SDK lets you develop Telegram Bots in PHP easily! Supports Laravel out of the box. Telegram Bot API is an

Irfaq Syed 2.5k Jan 6, 2023
A telegram bot to check credit cards. written in php & py

MRBANKER BOT A telegram bot to check credit cards. written in php & py. You can find me on telegram STEP1: goto botfather create a bot copy the token

Nitin1818 48 Dec 30, 2022
NovaGram - An elegant, Object-Oriented, reliable PHP Telegram Bot Library

An elegant, Object-Oriented, reliable PHP Telegram Bot Library Full Documentation • Public support group Examples • Features • Installation ?

Gaetano 165 Jan 6, 2023
Integrate Your PHP Code With Telegram Bot API for Beginner

Documentation[https://core.telegram.org/bots/api] Resource[https://github.com/bachors/KBBI.sql] Integrate Your PHP Code With Telegram Bot API for Begi

Nova Andre Saputra 1 Oct 19, 2021
Simple telegram auto reminder BOT with PHP

Telebot Script Telegram BOT Auto Reminder Dibuat pake PHP OOP Sebenernya bukan buat dipublish sih, ini cuma buat praktek PHP OOP sama praktek Commit &

its Galih 4 Nov 7, 2022
Simple php telegram bot.

PHPTGBot Description Simple php telegram bot. Just for fun ?? Work In Progress ?? Maybe found error, since im not tested yet! Still on working, so be

Yoga Pranata 2 Nov 24, 2021
A simple php telegram bot to check if the bin is valid or not!

Bin-Checker A simple php telegram bot to check if the bin is valid or not! Deploy FORK the Repo by clicking HERE Add your bot token in LINE 12 THEN DE

ʀᴇxɪɴᴀᴢᴏʀ 1 Nov 23, 2021
PHP Source - Telegram Bot - Standart Method

Telegram Bot- yaratish uchun mo'ljallangan standart metod asosida ishlovchi, PHP tilida yozilgan kod Dasturchilarning eng yaxshi ko'rgan odati c/p bo'

Jabborov Abduroziq 4 Mar 30, 2022
A Php Checker Bot For Telegram.

A Card Checker Bot For Telegram Based On Telegram.

Roldex Stark 16 Dec 29, 2022