Telegram Bot

dev
GeekQu 7 years ago
parent e775d9a329
commit b2d78c0d63
No known key found for this signature in database
GPG Key ID: F0EEABFCD04A5891

@ -1697,16 +1697,14 @@ class UserController extends BaseController
return $response->getBody()->write(json_encode($res));
}
if (!$this->user->isAbleToCheckin()) {
$checkin = $this->user->checkin();
if ($checkin['ok'] === false) {
$res['ret'] = 0;
$res['msg'] = '您似乎已经签到过了...';
return $response->getBody()->write(json_encode($res));
$res['msg'] = $checkin['msg'];
return $this->echoJson($response, $res);
}
$traffic = random_int(Config::get('checkinMin'), Config::get('checkinMax'));
$this->user->transfer_enable += Tools::toMB($traffic);
$this->user->last_check_in_time = time();
$this->user->save();
$res['msg'] = sprintf('获得了 %d MB流量.', $traffic);
$res['msg'] = $checkin['msg'];
$res['unflowtraffic'] = $this->user->transfer_enable;
$res['traffic'] = Tools::flowAutoShow($this->user->transfer_enable);
$res['trafficInfo'] = array(

@ -465,4 +465,27 @@ class User extends Model
Tools::delDirAndFile($user_path);
}
}
/**
* 签到
*/
public function checkin()
{
$return = [
'ok' = true,
'msg' = ''
];
if (!$this->isAbleToCheckin()) {
$return['ok'] = false;
$return['msg'] = '您似乎已经签到过了...';
} else {
$traffic = random_int((int) Config::get('checkinMin'), (int) Config::get('checkinMax'));
$this->transfer_enable += Tools::toMB($traffic);
$this->last_check_in_time = time();
$this->save();
$return['msg'] = '获得了 ' . $traffic . 'MB 流量.';
}
return $return;
}
}

@ -2,10 +2,6 @@
namespace App\Utils\Telegram;
use App\Models\{Bought, InviteCode, InviteQueue, User};
use App\Services\Config;
use App\Utils\Tools;
class Callback
{
public static function CallbackQueryMethod($user, $bot, $Callback)

@ -0,0 +1,84 @@
<?php
namespace App\Utils\Telegram\Commands;
use App\Models\User;
use App\Services\Config;
use Telegram\Bot\Actions;
use Telegram\Bot\Commands\Command;
/**
* Class CheckinCommand.
*/
class CheckinCommand extends Command
{
/**
* @var string Command Name
*/
protected $name = 'checkin';
/**
* @var string Command Description
*/
protected $description = '';
/**
* {@inheritdoc}
*/
public function handle($arguments)
{
$Update = $this->getUpdate();
$Message = $Update->getMessage();
// 消息会话 ID
$ChatID = $Message->getChat()->getId();
if ($ChatID < 0) {
// 群组
if (Config::get('new_telegram_group_quiet') === true) {
// 群组中不回应
return;
}
if ($ChatID != Config::get('new_telegram_group_chatid')) {
// 非我方群组
return;
}
}
// 发送 '输入中' 会话状态
$this->replyWithChatAction(['action' => Actions::TYPING]);
// 触发用户
$SendUser = [
'id' => $Message->getFrom()->getId(),
'name' => $Message->getFrom()->getFirstName() . ' ' . $Message->getFrom()->getLastName(),
'username' => $Message->getFrom()->getUsername(),
];
$User = User::where('id', $SendUser['id'])->first();
if ($User == null) {
// 回送信息
$this->replyWithMessage(
[
'text' => '您未绑定本站账号,您可以进入网站的 **资料编辑**,在右下方绑定您的账号.',
'parse_mode' => 'Markdown',
]
);
return;
}
$checkin = $User->checkin();
$text = [
$checkin['msg']
];
// 回送信息
$this->replyWithMessage(
[
'text' => implode(PHP_EOL, $text),
'parse_mode' => 'Markdown',
]
);
}
}

@ -30,7 +30,10 @@ class HelpCommand extends Command
$Update = $this->getUpdate();
$Message = $Update->getMessage();
if ($Message->getChat()->getId() > 0) {
// 消息会话 ID
$ChatID = $Message->getChat()->getId();
if ($ChatID > 0) {
// 私人会话
// 发送 '输入中' 会话状态

@ -0,0 +1,84 @@
<?php
namespace App\Utils\Telegram\Commands;
use App\Services\Config;
use Telegram\Bot\Actions;
use Telegram\Bot\Commands\Command;
/**
* Class PingCommand.
*/
class PingCommand extends Command
{
/**
* @var string Command Name
*/
protected $name = 'ping';
/**
* @var string Command Description
*/
protected $description = '';
/**
* {@inheritdoc}
*/
public function handle($arguments)
{
$Update = $this->getUpdate();
$Message = $Update->getMessage();
// 消息会话 ID
$ChatID = $Message->getChat()->getId();
if ($ChatID > 0) {
// 私人会话
// 发送 '输入中' 会话状态
$this->replyWithChatAction(['action' => Actions::TYPING]);
// 触发用户
$SendUser = [
'id' => $Message->getFrom()->getId(),
'name' => $Message->getFrom()->getFirstName() . ' ' . $Message->getFrom()->getLastName(),
'username' => $Message->getFrom()->getUsername(),
];
$text = [
'Pong!',
'这个群组的 ID 是 ' . $ChatID . '.',
];
// 回送信息
$this->replyWithMessage(
[
'text' => implode(PHP_EOL ,$text),
'parse_mode' => 'Markdown',
]
);
} else {
// 群组
if (Config::get('new_telegram_group_quiet') === true) {
// 群组中不回应
return;
}
// 发送 '输入中' 会话状态
$this->replyWithChatAction(['action' => Actions::TYPING]);
$text = [
'Pong!',
'这个群组的 ID 是 ' . $ChatID . '.',
];
// 回送信息
$this->replyWithMessage(
[
'text' => implode(PHP_EOL ,$text),
]
);
}
}
}

@ -31,7 +31,10 @@ class StartCommand extends Command
$Update = $this->getUpdate();
$Message = $Update->getMessage();
if ($Message->getChat()->getId() > 0) {
// 消息会话 ID
$ChatID = $Message->getChat()->getId();
if ($ChatID > 0) {
// 私人会话
// 发送 '输入中' 会话状态

@ -17,6 +17,8 @@ class Process
[
Commands\HelpCommand::class,
Commands\StartCommand::class,
Commands\PingCommand::class,
Commands\CheckinCommand::class,
]
);
$update = $bot->commandsHandler(true);

Loading…
Cancel
Save