承接 php-telegram-bot/fluent-keyboard 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

php-telegram-bot/fluent-keyboard

Composer 安装命令:

composer require php-telegram-bot/fluent-keyboard

包简介

Fluent Keyboard builder for ReplyKeyboardMarkup and InlineKeyboardMarkup.

README 文档

README

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'
]);

php-telegram-bot/fluent-keyboard 适用场景与选型建议

php-telegram-bot/fluent-keyboard 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.11k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2022 年 03 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「fluent」 「builder」 「bot」 「keyboard」 「telegram」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 php-telegram-bot/fluent-keyboard 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 php-telegram-bot/fluent-keyboard 我们能提供哪些服务?
定制开发 / 二次开发

基于 php-telegram-bot/fluent-keyboard 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.11k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 13
  • 点击次数: 25
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 13
  • Watchers: 5
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-22