【发布时间】:2018-07-01 08:03:22
【问题描述】:
我在 larvel 5.5 中使用php-telegram-bot/core 来制作电报机器人。
我遵循了所有安装并添加了here 中描述的自定义命令。
首先在web.php 路由文件中我添加了这个:
Route::get('/setwebhook', 'BotController@setWebhook');
Route::post('/webhook', ['as' => 'webhook', 'uses' => 'BotController@Webhook']);
这是BotController 结构:
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Log;
use Longman\TelegramBot\Request;
class BotController extends Controller
{
public $bot_api_key;
public $bot_username;
public $hook_url;
public $commands_paths;
function __construct ()
{
$this->bot_api_key = 'my_api_key';
$this->bot_username = 'my_username';
$this->hook_url = 'https://e18ed4a5.ngrok.io/webhook';
$this->commands_paths = [
__DIR__ . '\app\Commands',
];
}
public function setWebhook ()
{
try {
// Create Telegram API object
$telegram = new \Longman\TelegramBot\Telegram($this->bot_api_key, $this->bot_username);
// Set webhook
$result = $telegram->setWebhook($this->hook_url);
if ($result->isOk()) {
echo $result->getDescription();
}
} catch (\Longman\TelegramBot\Exception\TelegramException $e) {
// log telegram errors
echo $e->getMessage();
}
}
public function Webhook ()
{
try {
// Create Telegram API object
$telegram = new \Longman\TelegramBot\Telegram($this->bot_api_key, $this->bot_username);
$telegram->addCommandsPaths($this->commands_paths);
Request::setClient(new \GuzzleHttp\Client([
'base_uri' => 'https://api.telegram.org',
'verify' => false
]));
// Handle telegram webhook request
$telegram->handle();
} catch (\Longman\TelegramBot\Exception\TelegramException $e) {
// Silence is golden!
// log telegram errors
echo $e->getMessage();
}
}
}
这是StartCommand:
namespace Longman\TelegramBot\Commands\UserCommands;
use Illuminate\Support\Facades\Log;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Request;
class StartCommand extends UserCommand
{
protected $name = 'start';
protected $description = 'Start command';
protected $usage = '/start';
protected $version = '1.1.0';
public function execute ()
{
Log::info('how are you');
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$text = 'Welcome to my first bot';
$data = [
'chat_id' => $chat_id,
'text' => $text,
];
return Request::sendMessage($data);
}
}
如您所见,我完成了运行简单机器人的所有要求。但是每次我向我的机器人发送\start 命令时,我都没有收到任何响应。似乎从电报发送到我的主机的新更新,但我的脚本无法识别。
我不知道真的是什么问题
【问题讨论】:
标签: php laravel telegram laravel-5.5 telegram-bot