smoqadam/php-telegram-bot
Composer 安装命令:
composer require smoqadam/php-telegram-bot
包简介
wrapper class for tg bot api
README 文档
README
php-telegram-bot
A wrapper class for Telegram Bot API
Install
create composer.json :
{
"require": {
"smoqadam/php-telegram-bot": "v1.0"
}
}
$ composer install
How to use:
Smoqadam\Telegram is a wrapper around Telegram bot API. After you instantiate Telegram object, you can register callbacks on the updates you receive and then respond accordingly.
Register callbacks: Use following functions:
cmd(), all normal messagesinlineQuery()
API Methods: The available methods are almost same of official Telegram API (for now the wrapper does not natively handles games and messages update):
sendMessage(),getMe(),forwardMessage(),sendPhoto(),sendVideo(),sendSticker(),sendLocation(),sendDocument(),sendAudio(),sendChatAction(),getUserProfilePhotos(),answerInlineQuery()
Getting current update:
The current update is stored in the property Telegram::result (that is an object).
Inline Result helper:
To facilitate the creation of Inline Bot there are some helper classes under namespace InlineQuery\Result:
Article
Keyboards helper:
Also the same with keyboards; namespace is Keyboard:
Standard, classic keyboardRemove, remove custom keyboard and show letter-keyboardInline, inline keyboard
Use Keyboard:
Use the keyboard is pretty simple with helpers, after you instantiate keyboard (Standard or Inline) you use:
addButton()addRow()
Note: These methods are chainable.
Example
Using long polling:
<?php require 'vendor/autoload.php'; use Smoqadam\Telegram; $tg = new Telegram('API_TOKEN'); $tg->cmd('\/name:<<[a-zA-Z]{0,}>>', function ($args) use ($tg){ $tg->sendMessage("my username is @".$args , $tg->getChatId()); }); $tg->cmd('\/number: <<:num>>' , function($args) use($tg){ $tg->sendMessage("your number is : ".$args , $tg->getChatId()); }); $tg->cmd('Hello',function () use ($tg){ $tg->sendChatAction(Telegram::ACTION_TYPING); $image = 'urltoqrcode.png'; $tg->sendPhoto($image); }); $tg->run();
Using webHooks:
If you want to use webhooks you have to first call setWebhook method or open the following URL with your own data:
https://api.telegram.org/API_TOKEN/setWebhook?url=https://yourdomain.com/index.php
please change API_TOKEN and url parameter
<?php require 'vendor/autoload.php'; $message = file_get_contents('php://input'); use Smoqadam\Telegram; $tg = new Telegram('API_TOKEN'); $tg->cmd('\/name:<<[a-zA-Z]{0,}>>', function ($args) use ($tg){ $tg->sendMessage("my username is @".$args , $tg->getChatId()); }); $tg->cmd('\/number: <<:num>>' , function($args) use($tg){ $tg->sendMessage("your number is : ".$args , $tg->getChatId()); }); $tg->cmd('Hello',function () use ($tg){ $tg->sendChatAction(Telegram::ACTION_TYPING); $image = 'urltoqrcode.png'; $tg->sendPhoto($image); }); $tg->process(json_decode($message, true));
Now when you send /number 123 in telegram bot page , bot will answer to you your number is 123
You can set argument in regex between << and >>
Running bot
$ php index.php
smoqadam/php-telegram-bot 适用场景与选型建议
smoqadam/php-telegram-bot 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 184 次下载、GitHub Stars 达 21, 最近一次更新时间为 2015 年 07 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 smoqadam/php-telegram-bot 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 smoqadam/php-telegram-bot 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 184
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 21
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-07-01