Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
TelegramBot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
TelegramBot
Commits
1d55723a
Commit
1d55723a
authored
Feb 04, 2016
by
Marco Boretto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #82 from noplanman/code_cleanup_Admin
Initial code cleanup of `Admin commands`.
parents
6c03ff17
5ceb2295
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
225 additions
and
62 deletions
+225
-62
ChatsCommand.php
src/Admin/ChatsCommand.php
+82
-28
SendtoallCommand.php
src/Admin/SendtoallCommand.php
+75
-19
SendtochannelCommand.php
src/Admin/SendtochannelCommand.php
+68
-15
No files found.
src/Admin/ChatsCommand.php
View file @
1d55723a
<?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
)
{
//
i
nitialize a chat object
//
I
nitialize 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
.=
"
\n
Private Chats: "
.
$user_chats
;
$text
.=
"
\n
Group: "
.
$group_chats
;
$text
.=
"
\n
Super Group: "
.
$super_group_chats
;
$text
.=
"
\n
Tot: "
.
(
$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
();
}
...
...
src/Admin/SendtoallCommand.php
View file @
1d55723a
<?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 sen
t
: /sendall <message>'
;
$text
=
'Write the message to sen
d
: /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
();
...
...
src/Admin/SendtochannelCommand.php
View file @
1d55723a
<?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 sen
t
: /sendtochannel <message>'
;
$text_back
=
'Write the message to sen
d
: /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
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment