Commit 1d55723a authored by Marco Boretto's avatar Marco Boretto

Merge pull request #82 from noplanman/code_cleanup_Admin

Initial code cleanup of `Admin commands`.
parents 6c03ff17 5ceb2295
<?php
/*
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
*/
namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
/**
* Admin "/chats" command
*/
class ChatsCommand extends Command
{
/**
* Name
*
* @var string
*/
protected $name = 'chats';
/**
* Description
*
* @var string
*/
protected $description = 'List all chats stored by the bot';
protected $usage = '/chats ';
/**
* Usage
*
* @var string
*/
protected $usage = '/chats';
/**
* Version
*
* @var string
*/
protected $version = '1.0.0';
/**
* If this command is enabled
*
* @var boolean
*/
protected $enabled = true;
/**
* If this command is public
*
* @var boolean
*/
protected $public = true;
//need Mysql
protected $need_mysql = true;
/**
* If this command needs mysql
*
* @var boolean
*/
protected $need_mysql = false;
/**
* Execution if MySQL is required but not available
*
* @return boolean
*/
public function executeNoDB()
{
//Database not setted or without connection
//Preparing message
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$data = [];
$data['chat_id'] = $chat_id;
$data['text'] = 'Sorry no database connection, unable to execute '.$this->name.' command.';
$data = [
'chat_id' => $chat_id,
'text' => 'Sorry no database connection, unable to execute "' . $this->name . '" command.',
];
$result = Request::sendMessage($data);
return $result->isOk();
}
/**
* Execute command
*
* @return boolean
*/
public function execute()
{
$update = $this->getUpdate();
......@@ -60,38 +114,38 @@ class ChatsCommand extends Command
$user_chats = 0;
$group_chats = 0;
$super_group_chats = 0;
$text = "List of bot chats:\n";
$text = 'List of bot chats:' . "\n";
foreach ($results as $result) {
//initialize a chat object
//Initialize a chat object
$result['id'] = $result['chat_id'];
$chat = new Chat($result);
if ($chat->isPrivateChat()) {
$text .= '- P '.$chat->tryMention()."\n";
$text .= '- P ' . $chat->tryMention() . "\n";
++$user_chats;
} elseif ($chat->isGroupChat()) {
$text .= '- G '.$chat->getTitle()."\n";
$text .= '- G ' . $chat->getTitle() . "\n";
++$group_chats;
} elseif ($chat->isSuperGroup()) {
$text .= '- S '.$chat->getTitle()."\n";
$text .= '- S ' . $chat->getTitle() . "\n";
++$super_group_chats;
}
}
if (($group_chats + $user_chats + $super_group_chats) == 0) {
$text = "No chats found..";
if (($user_chats + $group_chats + $super_group_chats) == 0) {
$text = 'No chats found..';
} else {
$text .= "\nPrivate Chats: ".$user_chats;
$text .= "\nGroup: ".$group_chats;
$text .= "\nSuper Group: ".$super_group_chats;
$text .= "\nTot: ".($group_chats + $user_chats + $super_group_chats);
$text .= "\n" . 'Private Chats: ' . $user_chats;
$text .= "\n" . 'Group: ' . $group_chats;
$text .= "\n" . 'Super Group: ' . $super_group_chats;
$text .= "\n" . 'Total: ' . ($user_chats + $group_chats + $super_group_chats);
}
$data = [];
$data['chat_id'] = $chat_id;
$data['text'] = $text;
$data = [
'chat_id' => $chat_id,
'text' => $text,
];
$result = Request::sendMessage($data);
return $result->isOk();
}
......
<?php
/*
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
*/
namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
/**
* Admin "/sendtoall" command
*/
class SendtoallCommand extends Command
{
/**
* Name
*
* @var string
*/
protected $name = 'sendtoall';
/**
* Description
*
* @var string
*/
protected $description = 'Send the message to all the user\'s bot';
/**
* Usage
*
* @var string
*/
protected $usage = '/sendall <message to send>';
/**
* Version
*
* @var string
*/
protected $version = '1.2.0';
/**
* If this command is enabled
*
* @var boolean
*/
protected $enabled = true;
/**
* If this command is public
*
* @var boolean
*/
protected $public = true;
//need Mysql
/**
* If this command needs mysql
*
* @var boolean
*/
protected $need_mysql = true;
/**
* Execution if MySQL is required but not available
*
* @return boolean
*/
public function executeNoDB()
{
//Database not setted or without connection
//Preparing message
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$data = [];
$data['chat_id'] = $chat_id;
$data['text'] = 'Sorry no database connection, unable to execute '.$this->name.' command.';
$data = [
'chat_id' => $chat_id,
'text' => 'Sorry no database connection, unable to execute "' . $this->name . '" command.',
];
$result = Request::sendMessage($data);
return $result->isOk();
}
/**
* Execute command
*
* @todo Don't use empty, as a string of '0' is regarded to be empty
*
* @return boolean
*/
public function execute()
{
$update = $this->getUpdate();
......@@ -50,11 +105,11 @@ class SendtoallCommand extends Command
$text = $message->getText(true);
if (empty($text)) {
$text = 'Write the message to sent: /sendall <message>';
$text = 'Write the message to send: /sendall <message>';
} else {
$results = Request::sendToActiveChats(
'sendMessage', //callback function to execute (see Request.php methods)
array('text'=> $text), //Param to evaluate the request
['text' => $text], //Param to evaluate the request
true, //Send to groups (group chat)
true, //Send to super groups chats (super group chat)
true, //Send to users (single chat)
......@@ -65,7 +120,7 @@ class SendtoallCommand extends Command
$tot = 0;
$fail = 0;
$text = "Message sended to:\n";
$text = 'Message sent to:' . "\n";
foreach ($results as $result) {
$status = '';
$type = '';
......@@ -88,17 +143,18 @@ class SendtoallCommand extends Command
}
++$tot;
$text .= $tot.') '.$status.' '.$type.' '.$name."\n";
$text .= $tot . ') ' . $status . ' ' . $type . ' ' . $name . "\n";
}
$text .= "Delivered: ".($tot - $fail).'/'.$tot."\n";
$text .= 'Delivered: ' . ($tot - $fail) . '/' . $tot . "\n";
}
if ($tot == 0) {
$text = "No users or chats found..";
$text = 'No users or chats found..';
}
$data = [];
$data['chat_id'] = $chat_id;
$data['text'] = $text;
$data = [
'chat_id' => $chat_id,
'text' => $text,
];
$result = Request::sendMessage($data);
return $result->isOk();
......
<?php
/*
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
*/
namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
/**
* Admin "/sendtochannel" command
*/
class SendtochannelCommand extends Command
{
/**
* Name
*
* @var string
*/
protected $name = 'sendtochannel';
/**
* Description
*
* @var string
*/
protected $description = 'Send message to a channel';
/**
* Usage
*
* @var string
*/
protected $usage = '/sendchannel <message to send>';
/**
* Version
*
* @var string
*/
protected $version = '0.1.0';
/**
* If this command is enabled
*
* @var boolean
*/
protected $enabled = true;
/**
* If this command is public
*
* @var boolean
*/
protected $public = true;
//need Mysql
/**
* If this command needs mysql
*
* @var boolean
*/
protected $need_mysql = false;
/**
* Execute command
*
* @todo Don't use empty, as a string of '0' is regarded to be empty
*
* @return boolean
*/
public function execute()
{
$update = $this->getUpdate();
......@@ -35,26 +85,29 @@ class SendtochannelCommand extends Command
$chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId();
$text = $message->getText(true);
if (empty($text)) {
$text_back = 'Write the message to sent: /sendtochannel <message>';
$text_back = 'Write the message to send: /sendtochannel <message>';
} else {
$your_channel = $this->getConfig('your_channel');
//Send message to channel
$data = [];
$data['chat_id'] = $your_channel;
$data['text'] = $text;
$data = [
'chat_id' => $your_channel,
'text' => $text,
];
$result = Request::sendMessage($data);
if ($result->isOk()) {
$text_back = 'Message sent succesfully to: '.$your_channel;
$text_back = 'Message sent succesfully to: ' . $your_channel;
} else {
$text_back = 'Sorry message not sent to: '.$your_channel;
$text_back = 'Sorry message not sent to: ' . $your_channel;
}
}
$data = [];
$data['chat_id'] = $chat_id;
$data['text'] = $text_back;
$data = [
'chat_id' => $chat_id,
'text' => $text_back,
];
$result = Request::sendMessage($data);
return $result->isOk();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment