mirror of
https://github.com/v2board/v2board.git
synced 2025-02-25 14:50:26 +08:00
update telegram
This commit is contained in:
parent
ca650dd067
commit
ce19ebc97f
@ -24,16 +24,12 @@ class ConfigController extends Controller
|
|||||||
public function setTelegramWebhook(Request $request)
|
public function setTelegramWebhook(Request $request)
|
||||||
{
|
{
|
||||||
$telegramService = new TelegramService($request->input('telegram_bot_token'));
|
$telegramService = new TelegramService($request->input('telegram_bot_token'));
|
||||||
if (!$telegramService->getMe()) {
|
$telegramService->getMe();
|
||||||
abort(500, '机器人Token有误');
|
$telegramService->setWebhook(
|
||||||
}
|
|
||||||
if (!$telegramService->setWebhook(
|
|
||||||
config('v2board.app_url')
|
config('v2board.app_url')
|
||||||
. '/api/v1/guest/telegram/webhook?access_token='
|
. '/api/v1/guest/telegram/webhook?access_token='
|
||||||
. md5(config('v2board.telegram_bot_token', $request->input('telegram_bot_token')))
|
. md5(config('v2board.telegram_bot_token', $request->input('telegram_bot_token')))
|
||||||
)) {
|
);
|
||||||
abort(500, 'Webhook设置失败');
|
|
||||||
}
|
|
||||||
return response([
|
return response([
|
||||||
'data' => true
|
'data' => true
|
||||||
]);
|
]);
|
||||||
|
20
app/Http/Controllers/User/TelegramController.php
Normal file
20
app/Http/Controllers/User/TelegramController.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\User;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Services\TelegramService;
|
||||||
|
|
||||||
|
class TelegramController extends Controller
|
||||||
|
{
|
||||||
|
public function getBotInfo()
|
||||||
|
{
|
||||||
|
$telegramService = new TelegramService();
|
||||||
|
$response = $telegramService->getMe();
|
||||||
|
return response([
|
||||||
|
'data' => [
|
||||||
|
'username' => $response->result->username
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
@ -62,7 +62,8 @@ class UserController extends Controller
|
|||||||
'commission_balance',
|
'commission_balance',
|
||||||
'plan_id',
|
'plan_id',
|
||||||
'discount',
|
'discount',
|
||||||
'commission_rate'
|
'commission_rate',
|
||||||
|
'telegram_id'
|
||||||
])
|
])
|
||||||
->first();
|
->first();
|
||||||
$user['avatar_url'] = 'https://cdn.v2ex.com/gravatar/' . md5($user->email) . '?s=64&d=identicon';
|
$user['avatar_url'] = 'https://cdn.v2ex.com/gravatar/' . md5($user->email) . '?s=64&d=identicon';
|
||||||
|
@ -51,6 +51,8 @@ class UserRoute
|
|||||||
$router->get ('/server/log/fetch', 'User\\ServerController@logFetch');
|
$router->get ('/server/log/fetch', 'User\\ServerController@logFetch');
|
||||||
// Coupon
|
// Coupon
|
||||||
$router->post('/coupon/check', 'User\\CouponController@check');
|
$router->post('/coupon/check', 'User\\CouponController@check');
|
||||||
|
// Telegram
|
||||||
|
$router->get ('/telegram/getBotInfo', 'User\\TelegramController@getBotInfo');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,9 @@ use \Curl\Curl;
|
|||||||
class TelegramService {
|
class TelegramService {
|
||||||
protected $api;
|
protected $api;
|
||||||
|
|
||||||
public function __construct(string $token = '')
|
public function __construct($token = '')
|
||||||
{
|
{
|
||||||
$this->api = 'https://api.telegram.org/bot' . config('v2board.telegram_bot_token', $token) . '/';
|
$this->api = 'http://dev.v2board.com/bot' . config('v2board.telegram_bot_token', $token) . '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function sendMessage(int $chatId, string $text, string $parseMode = '')
|
public function sendMessage(int $chatId, string $text, string $parseMode = '')
|
||||||
@ -22,29 +22,25 @@ class TelegramService {
|
|||||||
|
|
||||||
public function getMe()
|
public function getMe()
|
||||||
{
|
{
|
||||||
$response = $this->request('getMe');
|
return $this->request('getMe');
|
||||||
if (!$response->ok) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return $response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setWebhook(string $url)
|
public function setWebhook(string $url)
|
||||||
{
|
{
|
||||||
$response = $this->request('setWebhook', [
|
return $this->request('setWebhook', [
|
||||||
'url' => $url
|
'url' => $url
|
||||||
]);
|
]);
|
||||||
if (!$response->ok) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return $response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function request(string $method, array $params = [])
|
private function request(string $method, array $params = [])
|
||||||
{
|
{
|
||||||
$curl = new Curl();
|
$curl = new Curl();
|
||||||
$curl->get($this->api . $method, http_build_query($params));
|
$curl->get($this->api . $method . '?' . http_build_query($params));
|
||||||
|
$response = $curl->response;
|
||||||
$curl->close();
|
$curl->close();
|
||||||
return $curl->response;
|
if (!$response->ok) {
|
||||||
|
abort(500, '来自TG的错误:' . $response->description);
|
||||||
|
}
|
||||||
|
return $response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user