Download the PHP package goletter/hyperf-telegram-bot without Composer

On this page you can find all versions of the php package goletter/hyperf-telegram-bot. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package hyperf-telegram-bot

Hyperf Telegram Bot

Hyperf 协程友好的 Telegram Bot API 客户端,支持多 Bot、动态 Token、Webhook 校验与常用消息能力。

心智模型

角色 类 作用
工厂 Goletter\Telegram\Factory\BotFactory 唯一推荐入口:按 Token / 名称拿到 Bot
客户端 Goletter\Telegram\Bot 调用 Telegram API(发消息、设 Webhook 等)
解析 Helper\Webhook + Update\Update 接收推送、校验 secret、读取命令 / chat_id
中间件 Middleware\VerifyTelegramWebhookMiddleware 只做 secret 校验;动态多 Bot 需先 attach

请注入 BotFactory。包默认不绑定 BotInterface(未配置静态 Token 时注入会失败)。


安装

本仓库(已通过 PSR-4 引入)

composer.json:

config/autoload/annotations.php 扫描路径需包含:

发布配置(若尚未有 config/autoload/telegram.php):

独立项目


配置

config/autoload/telegram.php 主要只配 HTTP。多机器人、Token 存在数据库时,不必写死 bots:

开启后每次 Bot API 调用会打类似日志:

静态单 Bot 时可这样写:

Token 格式必须是 {bot_id}:{secret},例如 7123456789:AAHxxxx。不要只填 secret 段,也不要把整段 API URL 塞进来。


场景 A:动态多 Bot(推荐)

Token 来自数据库 / 业务配置,可随时变更。同名 Bot 在 Token 变化时会自动重建缓存。

BotFactory 方法怎么选

方法 何时用
token($token) 临时用,缓存名默认 bot:{bot_id}
token($token, $bizId, $options) 业务侧推荐,名称稳定、便于轮换
resolve($name, $token, $options) 显式按名称绑定;Token 变了会重建
get('default') 仅当配置文件里有静态 bots.xxx.token
make(...) 只创建、不进缓存(一次性调用)
forget($name) 主动清缓存;forget() 清空全部

$options 目前支持:webhook_secret(设置 Webhook / 校验推送时用)。


场景 B:静态单 Bot

在 telegram.bots.default.token 配好后:

如仍希望注入 BotInterface,在项目 config/autoload/dependencies.php 自行绑定:


场景 C:Webhook(完整可跑)

1. 路由建议

POST /telegram/webhook/{id},用业务 ID 区分机器人。

2. 控制器(推荐写法)

在控制器里用 BotFactory 拿到 Bot,再交给 Webhook::parseRequest(会校验 X-Telegram-Bot-Api-Secret-Token)。

3. 设置 Webhook

每个机器人各自 URL。若创建 Bot 时带了 webhook_secret,setWebhook 会自动附带 secret_token:

4. 可选:中间件校验

VerifyTelegramWebhookMiddleware 不会自己从数据库查 Token。动态多 Bot 时,必须先在业务中间件里 attach:

控制器里再解析:

静态配置且路由带 {bot} 参数时,中间件可直接按配置名 get($bot),无需 attach。


场景 D:Long Polling

Webhook 与 Long Polling 不要同时用于同一 Bot。


常用能力

发送消息 / 文件

任意未封装的 API 可用:

输入框旁菜单按钮(Menu Button)

可设为命令列表、Mini App(web_app)或恢复默认。与系统「+」附件菜单无关。

Telegram 没有独立的 create / update / delete:设置即覆盖,删除即恢复 default。

操作 方法
添加 / 修改 setWebAppMenuButton($text, $url, $chatId?)
查询 getWebAppMenuButton($chatId?) → ['text'=>..., 'url'=>...] 或 null
删除 removeWebAppMenuButton($chatId?)(恢复 default)

Menu Button 打开的 Mini App 一般自行调业务 API。若使用 ReplyKeyboard 上的 web_app 按钮回传,可用 $update->getWebAppData()。

获取群用户(重要限制)

Telegram Bot API 不能一次拉全群成员,只能:

  1. 查人数 / 管理员
  2. 按已知 user_id 查单个或批量
  3. 监听 chat_member 进退群事件自行落库后再查

监听成员变动并落库(Webhook 需允许 chat_member):

按 Bot 获取已加入的群(需自行落库)

Telegram 没有「列出该 Bot 所在全部群」的 API。本包提供:

组件 作用
BotChatTracker 从 Update 同步,再按 Bot 查询群列表
BotChatRepositoryInterface 仓储契约;默认内存实现,生产请换数据库
getMyChatMember() / isMyChatMemberUpdate() 识别 Bot 进退群事件
getGroupInfo / getGroupsInfo 已知 chat_id 时拉实时资料

Webhook 控制器里同步:

按 Bot 取群:

生产环境请绑定自己的仓储(内存实现重启/多 Worker 会丢):

设置 Webhook 时务必包含 my_chat_member,否则 Bot 被拉进/踢出群时收不到事件。

拉群 / 邀请链接

Bot 不能直接把用户拉进群,需具备管理员的 can_invite_users,通过邀请链接或审批加群申请完成。


已封装 API 一览

未列出的方法仍可用 $bot->call('methodName', $params)。

类别 方法
文本/编辑 sendMessage、editMessageText / Caption / Media / ReplyMarkup、deleteMessage(s)
转发复制 forwardMessage(s)、copyMessage(s)
媒体 sendPhoto / Video / Document / Animation / Audio / Voice / VideoNote / Sticker、sendMediaGroup、sendPhotos / sendVideos
其它消息 sendLocation / Venue / Contact / Poll / Dice、stopPoll、sendChatAction、setMessageReaction
回调/Inline/WebApp answerCallbackQuery、answerInlineQuery、answerWebAppQuery
菜单/命令/资料 Menu Button 系列、set/get/deleteMyCommands、set/getMyName、Description、ShortDescription、默认管理员权限
群管理 邀请链接、进群审批、ban/unban/restrict/promote、权限与头衔、置顶、群资料、leaveChat
论坛话题 create/edit/close/reopen/deleteForumTopic、unpinAllForumTopicMessages、getForumTopicIconStickers
更新与文件 getUpdates、Webhook、getFile、getMe、getUserProfilePhotos、群查询便捷方法

Update 常用方法

方法 说明
getUpdateId() update_id
getMessage() message / edited_message / channel_post 等
getCallbackQuery() 回调查询
getWebAppData() 键盘 Web App 回传(message.web_app_data)
getInlineQuery() Inline 查询
getChosenInlineResult() 用户选择的 Inline 结果
getShippingQuery() / getPreCheckoutQuery() 支付相关查询
getPoll() / getPollAnswer() 投票更新
getMessageReaction() / getMessageReactionCount() 表情回应
getChatJoinRequest() 加群申请
getChatMemberUpdate() chat_member / my_chat_member
getMyChatMember() 仅 my_chat_member(Bot 进退群)
isMyChatMemberUpdate() 是否为 Bot 自身成员变更
getChatId() / getUserId() 会话与用户 ID
getText() 消息文本、callback data 或 web_app_data.data
isCommand('start') 是否为 /start(兼容 /start@BotName)
toArray() 原始数组

异常

API 失败抛出 Goletter\Telegram\Exceptions\TelegramApiException:

Webhook secret 校验失败同样抛该异常(HTTP 语义上对应 403)。


All versions of hyperf-telegram-bot with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
ext-json Version *
guzzlehttp/guzzle Version ^7.0
hyperf/contract Version ~3.1.0
hyperf/di Version ~3.1.0
hyperf/guzzle Version ~3.1.0
hyperf/http-server Version ~3.1.0
psr/container Version ^1.0 || ^2.0
psr/http-message Version ^1.0 || ^2.0
psr/log Version ^1.0 || ^2.0 || ^3.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package goletter/hyperf-telegram-bot contains the following files

Loading the files please wait ...