laravel集成TG Bot 机器人
1 github 下载 https://github.com/irazasyed/TG-bot-sdk

或者 composer 用2.0版本 3.0都没文档
composer require irazasyed/TG-bot-sdk ^2.0
![]()
2 laravel配置
- config/app.php
providers数组 添加
TG\Bot\Laravel\TGServiceProvider::class,
aliases数组添加
'TG' => TG\Bot\Laravel\Facades\TG::class
- 执行命令 生成 config/TG.php配置文件
php artisan vendor:publish --provider="TG\Bot\Laravel\TGServiceProvider"

主要修改 bot_token(随意找到TG任何人 然后输入 https://TG.me/botfather 点击就可以了添加botfather 然后输入 /newbot 他会提示你给bot命名 ps:结尾为_bot或者Bot,下面打码的就是token)

3 laravel中使用
引入 TG(use TG)
//获取bot信息
$response = TG::getMe();
$botId = $response->getId();
$firstName = $response->getFirstName();
$username = $response->getUsername();

//发送信息 发送图片 chat_id怎么获取呢 参考TG回调json里面的 chat:{id}
TG::sendMessage(['chat_id' => 'xxx',
'text' => '更多请点击 https://bbs.918dxs.com/']);
TG::setAsyncRequest(true)
->sendPhoto(['chat_id' => 'xxx',
'photo' => 图片url,
'caption' => '图片描述更多请点击 https://bbs.918dxs.com/'
]);
4 配置TG回调地址(同微信公众号一样道理 所有信息都会推送到该地址)

返回的结果右下 更多方法参考https://TG-bot-sdk.readme.io/reference#getupdates


浙公网安备 33010602011771号