Telegram Bot

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

@ -140,37 +140,52 @@ class User extends Model
)->toString();
}
public function trafficUsagePercent()
{
$total = $this->attributes['u'] + $this->attributes['d'];
$transferEnable = $this->attributes['transfer_enable'];
if ($transferEnable == 0) {
return 0;
}
$percent = $total / $transferEnable;
$percent = round($percent, 2);
$percent *= 100;
return $percent;
}
/*
* 总流量
*/
public function enableTraffic()
{
$transfer_enable = $this->attributes['transfer_enable'];
return Tools::flowAutoShow($transfer_enable);
}
/*
* 总流量[GB]
*/
public function enableTrafficInGB()
{
$transfer_enable = $this->attributes['transfer_enable'];
return Tools::flowToGB($transfer_enable);
}
/*
* 已用流量
*/
public function usedTraffic()
{
$total = $this->attributes['u'] + $this->attributes['d'];
return Tools::flowAutoShow($total);
}
/*
* 已用流量占总流量的百分比
*/
public function trafficUsagePercent()
{
$total = $this->attributes['u'] + $this->attributes['d'];
$transferEnable = $this->attributes['transfer_enable'];
if ($transferEnable == 0) {
return 0;
}
$percent = $total / $transferEnable;
$percent = round($percent, 2);
$percent *= 100;
return $percent;
}
/*
* 剩余流量
*/
public function unusedTraffic()
{
$total = $this->attributes['u'] + $this->attributes['d'];
@ -178,18 +193,75 @@ class User extends Model
return Tools::flowAutoShow($transfer_enable - $total);
}
/*
* 剩余流量占总流量的百分比
*/
public function unusedTrafficPercent()
{
$transferEnable = $this->attributes['transfer_enable'];
if ($transferEnable == 0) {
return 0;
}
$unusedTraffic = $transferEnable - ($this->attributes['u'] + $this->attributes['d']);
$percent = $unusedTraffic / $transferEnable;
$percent = round($percent, 2);
$percent *= 100;
return $percent;
}
/*
* 今天使用的流量
*/
public function TodayusedTraffic()
{
$total = $this->attributes['u'] + $this->attributes['d'] - $this->attributes['last_day_t'];
return Tools::flowAutoShow($total);
}
/*
* 今天使用的流量占总流量的百分比
*/
public function TodayusedTrafficPercent()
{
$transferEnable = $this->attributes['transfer_enable'];
if ($transferEnable == 0) {
return 0;
}
$TodayusedTraffic = $this->attributes['u'] + $this->attributes['d'] - $this->attributes['last_day_t'];
$percent = $TodayusedTraffic / $transferEnable;
$percent = round($percent, 2);
$percent *= 100;
return $percent;
}
/*
* 今天之前已使用的流量
*/
public function LastusedTraffic()
{
$total = $this->attributes['last_day_t'];
return Tools::flowAutoShow($total);
}
/*
* 今天之前已使用的流量占总流量的百分比
*/
public function LastusedTrafficPercent()
{
$transferEnable = $this->attributes['transfer_enable'];
if ($transferEnable == 0) {
return 0;
}
$LastusedTraffic = $this->attributes['last_day_t'];
$percent = $LastusedTraffic / $transferEnable;
$percent = round($percent, 2);
$percent *= 100;
return $percent;
}
/*
* 是否可以签到
*/
public function isAbleToCheckin()
{
$last = $this->attributes['last_check_in_time'];

@ -0,0 +1,120 @@
<?php
namespace App\Utils\Telegram\Commands;
use App\Models\User;
use App\Services\Config;
use Telegram\Bot\Actions;
use Telegram\Bot\Commands\Command;
/**
* Class MyCommand.
*/
class MyCommand extends Command
{
/**
* @var string Command Name
*/
protected $name = 'my';
/**
* @var string Command Description
*/
protected $description = '';
/**
* {@inheritdoc}
*/
public function handle($arguments)
{
$Update = $this->getUpdate();
$Message = $Update->getMessage();
// 消息 ID
$MessageID = $Message->getMessageId();
// 消息会话 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('telegram_id', $SendUser['id'])->first();
if ($User == null) {
// 回送信息
$this->replyWithMessage(
[
'text' => '您未绑定本站账号,您可以进入网站的 **资料编辑**,在右下方绑定您的账号.',
'parse_mode' => 'Markdown',
]
);
return;
}
if ($ChatID > 0) {
// 私人
self::Privacy($User, $SendUser, $ChatID, $Message, $MessageID);
} else {
// 群组
self::Group($User, $SendUser, $ChatID, $Message, $MessageID);
}
}
public function Group($User, $SendUser, $ChatID, $Message, $MessageID)
{
$text = [
'您当前的流量状况:',
'',
'今日已使用[' . $User->TodayusedTrafficPercent(). '%]:' . $User->TodayusedTraffic(),
'之前已使用[' . $User->LastusedTrafficPercent(). '%]:' . $User->LastusedTraffic(),
'流量约剩余[' . $User->unusedTrafficPercent(). '%]:' . $User->unusedTraffic(),
];
// 回送信息
$this->replyWithMessage(
[
'text' => implode(PHP_EOL, $text),
'parse_mode' => 'Markdown',
'reply_to_message_id' => $MessageID,
]
);
}
public function Privacy($User, $SendUser, $ChatID, $Message, $MessageID)
{
$text = [
'您当前的流量状况:',
'',
'今日已使用[' . $User->TodayusedTrafficPercent(). '%]:' . $User->TodayusedTraffic(),
'之前已使用[' . $User->LastusedTrafficPercent(). '%]:' . $User->LastusedTraffic(),
'流量约剩余[' . $User->unusedTrafficPercent(). '%]:' . $User->unusedTraffic(),
];
// 回送信息
$this->replyWithMessage(
[
'text' => implode(PHP_EOL, $text),
'parse_mode' => 'Markdown',
]
);
}
}

@ -19,6 +19,7 @@ class Process
Commands\StartCommand::class,
Commands\PingCommand::class,
Commands\CheckinCommand::class,
Commands\MyCommand::class,
]
);
$update = $bot->commandsHandler(true);

Loading…
Cancel
Save