composer require irazasyed/telegram-bot-sdk ^2.0
'providers' => [
Telegram\Bot\Laravel\TelegramServiceProvider::class
]
'aliases' => [
'Telegram' => Telegram\Bot\Laravel\Facades\Telegram::class
]
php artisan vendor:publish --provider="Telegram\Bot\Laravel\TelegramServiceProvider"
TELEGRAM_BOT_TOKEN=API TOKEN
TELEGRAM_ASYNC_REQUESTS=false
以Laravel-admin自定义行操作为例
<?php
namespace App\Admin\Actions\Article;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Telegram\Bot\Laravel\Facades\Telegram;
class SendToTGChannel extends RowAction
{
public $name = '发送文章到TG频道';
public $chat_id = '@xxxxx'; //@channelusername
public function handle(Model $model)
{
$title = "<a href={https://domin/article/$model->id}>{$model->title}</a>";
if ($model->cover) { //有封面 发图片
$response = Telegram::sendPhoto([
'chat_id' => $this->chat_id,
'photo' => Storage::disk(config('admin.upload.disk'))->path($model->cover),
'caption' => $title,
'parse_model' => 'html', //支持html和markdown
]);
} else {
$response = Telegram::sendMessage([
'chat_id' => $this->chat_id,
'text' => $title,
'parse_model' => 'html',
]);
}
$messageId = $response->getMessageId();
if ($messageId) {
return $this->response()->success('发送成功')->refresh();
} else {
return $this->response()->error('发送失败')->refresh();
}
}
public function dialog()
{
$this->confirm('确定发送文章到TG频道?');
}
}
Route::post('/API TOKEN/webhook', function () {
Telegram\Bot\Laravel\Facades\Telegram::commandsHandler(true);
});
protected $except = [
'/API TOKEN/webhook',
];
//dingo api
$api->post('v1/API TOKEN/webhook', function () {
Telegram\Bot\Laravel\Facades\Telegram::commandsHandler(true);
});
curl https://api.telegram.org/botAPI TOKEN/setWebhook --data url=https://domain/API TOKEN/webhook
curl https://api.telegram.org/botAPI TOKEN/getWebhookInfo
'commands' => [
//Telegram\Bot\Commands\HelpCommand::class,
App\Console\Telegram\Start::class,
App\Console\Telegram\Help::class,
App\Console\Telegram\Article::class,
],
<?php
namespace App\Console\Telegram;
use Telegram\Bot\Actions;
use Telegram\Bot\Commands\Command;
class Start extends Command
{
/**
* @var string Command Name
*/
protected $name = 'start';
/**
* @var string Command Description
*/
protected $description = '开始';
/**
* {@inheritdoc}
*/
public function handle($arguments)
{
//回复消息
$this->replyWithMessage(['text' => '你好!欢迎使用机器人,以下是我们可以用的指令:']);
//切换到输入状态
$this->replyWithChatAction(['action' => Actions::TYPING]);
//触发帮助指令
$this->triggerCommand('help');
$this->replyWithMessage(['text' => '以下是热门文章:']);
$this->replyWithChatAction(['action' => Actions::TYPING]);
$this->triggerCommand('article');
}
}
<?php
namespace App\Console\Telegram;
use Telegram\Bot\Commands\Command;
class Help extends Command
{
/**
* @var string Command Name
*/
protected $name = 'help';
/**
* @var string Command Description
*/
protected $description = '帮助';
/**
* {@inheritdoc}
*/
public function handle($arguments)
{
$commands = $this->telegram->getCommands();
$text = '';
foreach ($commands as $name => $handler) {
$text .= sprintf('/%s - %s'.PHP_EOL, $name, $handler->getDescription());
}
$this->replyWithMessage(compact('text'));
}
}
<?php
namespace App\Console\Telegram;
use App\Models\ArticleModel;
use Telegram\Bot\Commands\Command;
class Article extends Command
{
/**
* @var string Command Name
*/
protected $name = 'Article';
/**
* @var string Command Description
*/
protected $description = '查看或搜索文章';
/**
* {@inheritdoc}
*/
public function handle($arguments)
{
if ($arguments) {//搜索 /article 新冠肺炎 回复标题包含“新冠肺炎”的10篇文章
$article = ArticleModel::where('title', 'LIKE', "%{$arguments}%")->take(10)->get();
} else {
$article = ArticleModel::orderBy('created_at', 'DESC')->take(10)->get();
}
$return = [
'text' => '',
'parse_mode' => 'html' //支持html和markdown
];
foreach ($article as $item) {
$return['text'] .= "
<a href={https://domin/article/$item->id}>{$item->title}</a>
";
}
$this->replyWithMessage($return);
}
}