You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ss-panel-v3-mod_Uim2/app/Utils/Telegram/Process.php

69 lines
2.1 KiB
PHTML

7 years ago
<?php
namespace App\Utils\Telegram;
use App\Models\User;
use App\Services\Config;
use Telegram\Bot\Api;
use Exception;
class Process
{
public static function index()
{
try {
$bot = new Api(Config::get('new_telegram_token'));
$bot->addCommands(
[
Commands\HelpCommand::class,
Commands\StartCommand::class,
7 years ago
Commands\PingCommand::class,
Commands\CheckinCommand::class,
7 years ago
Commands\MyCommand::class,
7 years ago
Commands\UnbindCommand::class,
7 years ago
]
);
$update = $bot->commandsHandler(true);
if ($update->getCallbackQuery() !== null) {
$id = $update->getCallbackQuery()->getFrom()->getId();
7 years ago
$user = self::getUser($id);
7 years ago
Callback::CallbackQueryMethod($user, $bot, $update->getCallbackQuery());
}
7 years ago
if ($update->getMessage() !== null) {
$id = $update->getMessage()->getFrom()->getId();
$user = self::getUser($id);
Message::MessageMethod($user, $bot, $update->getMessage());
}
7 years ago
} catch (Exception $e) {
$e->getMessage();
}
}
7 years ago
public static function getUser($telegram_id)
{
return User::where('telegram_id', $telegram_id)->first();
}
7 years ago
/**
* Sends a POST request to Telegram Bot API.
* 伪异步,无结果返回.
*
* @param array $params
*
* @return string
*/
public static function SendPost($Method, $Params)
{
$URL = 'https://api.telegram.org/bot' . Config::get('new_telegram_token') . '/' . $Method;
$POSTData = json_encode($Params);
$C = curl_init();
curl_setopt($C, CURLOPT_URL, $URL);
curl_setopt($C, CURLOPT_POST, 1);
curl_setopt($C, CURLOPT_HTTPHEADER, ['Content-Type:application/json; charset=utf-8']);
curl_setopt($C, CURLOPT_POSTFIELDS, $POSTData);
curl_setopt($C, CURLOPT_TIMEOUT, 1);
curl_exec($C);
curl_close($C);
}
7 years ago
}