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
c5a5923d
Unverified
Commit
c5a5923d
authored
Mar 31, 2018
by
Armando Lüscher
Committed by
GitHub
Mar 31, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #810 from noplanman/improve_admin_send_commands
Improve admin send commands
parents
db530e2a
e6aca7a3
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
149 additions
and
135 deletions
+149
-135
CHANGELOG.md
CHANGELOG.md
+1
-0
SendtoallCommand.php
src/Commands/AdminCommands/SendtoallCommand.php
+47
-55
SendtochannelCommand.php
src/Commands/AdminCommands/SendtochannelCommand.php
+101
-80
No files found.
CHANGELOG.md
View file @
c5a5923d
...
...
@@ -13,6 +13,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
-
Updated Travis to use Trusty containers (for HHVM) and add PHP 7.2 to the tests.
-
Add debug log entry instead of throwing an exception for duplicate updates.
-
`Telegram::handleGetUpdates()`
can now work without a database connection (not enabled by default).
-
Improved
`/sendtochannel`
and
`/sendtoall`
commands, using new message helpers.
### Deprecated
### Removed
### Fixed
...
...
src/Commands/AdminCommands/SendtoallCommand.php
View file @
c5a5923d
...
...
@@ -38,7 +38,7 @@ class SendtoallCommand extends AdminCommand
/**
* @var string
*/
protected
$version
=
'1.
4
.0'
;
protected
$version
=
'1.
5
.0'
;
/**
* @var bool
...
...
@@ -53,14 +53,13 @@ class SendtoallCommand extends AdminCommand
*/
public
function
execute
()
{
$message
=
$this
->
getMessage
();
$chat_id
=
$message
->
getChat
()
->
getId
();
$text
=
$message
->
getText
(
true
);
$text
=
$this
->
getMessage
()
->
getText
(
true
);
if
(
$text
===
''
)
{
$text
=
'Write the message to send: /sendtoall <message>'
;
}
else
{
return
$this
->
replyToChat
(
'Usage: '
.
$this
->
getUsage
());
}
/** @var ServerResponse[] $results */
$results
=
Request
::
sendToActiveChats
(
'sendMessage'
,
//callback function to execute (see Request.php methods)
[
'text'
=>
$text
],
//Param to evaluate the request
...
...
@@ -72,12 +71,15 @@ class SendtoallCommand extends AdminCommand
]
);
if
(
empty
(
$results
))
{
return
$this
->
replyToChat
(
'No users or chats found.'
);
}
$total
=
0
;
$failed
=
0
;
$text
=
'Message sent to:'
.
PHP_EOL
;
/** @var ServerResponse $result */
foreach
(
$results
as
$result
)
{
$name
=
''
;
$type
=
''
;
...
...
@@ -104,16 +106,6 @@ class SendtoallCommand extends AdminCommand
}
$text
.=
'Delivered: '
.
(
$total
-
$failed
)
.
'/'
.
$total
.
PHP_EOL
;
if
(
$total
===
0
)
{
$text
=
'No users or chats found..'
;
}
}
$data
=
[
'chat_id'
=>
$chat_id
,
'text'
=>
$text
,
];
return
Request
::
sendMessage
(
$data
);
return
$this
->
replyToChat
(
$text
);
}
}
src/Commands/AdminCommands/SendtochannelCommand.php
View file @
c5a5923d
...
...
@@ -10,12 +10,12 @@
namespace
Longman\TelegramBot\Commands\AdminCommands
;
use
Longman\TelegramBot\Entities\Keyboard
;
use
Longman\TelegramBot\Request
;
use
Longman\TelegramBot\Conversation
;
use
Longman\TelegramBot\Commands\AdminCommand
;
use
Longman\TelegramBot\Conversation
;
use
Longman\TelegramBot\Entities\Chat
;
use
Longman\TelegramBot\Entities\Keyboard
;
use
Longman\TelegramBot\Entities\Message
;
use
Longman\TelegramBot\
Exception\TelegramException
;
use
Longman\TelegramBot\
Request
;
class
SendtochannelCommand
extends
AdminCommand
{
...
...
@@ -37,7 +37,7 @@ class SendtochannelCommand extends AdminCommand
/**
* @var string
*/
protected
$version
=
'0.
2
.0'
;
protected
$version
=
'0.
3
.0'
;
/**
* @var bool
...
...
@@ -64,8 +64,8 @@ class SendtochannelCommand extends AdminCommand
$user_id
=
$message
->
getFrom
()
->
getId
();
$type
=
$message
->
getType
();
// 'Cast' the command type
in
to message to protect the machine state
// if the comm
ma
d is recalled when the conversation is already started
// 'Cast' the command type to message to protect the machine state
// if the comm
an
d is recalled when the conversation is already started
in_array
(
$type
,
[
'command'
,
'text'
],
true
)
&&
$type
=
'message'
;
$text
=
trim
(
$message
->
getText
(
true
));
...
...
@@ -89,6 +89,15 @@ class SendtochannelCommand extends AdminCommand
$notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
}
$yes_no_keyboard
=
new
Keyboard
(
[
'keyboard'
=>
[[
'Yes'
,
'No'
]],
'resize_keyboard'
=>
true
,
'one_time_keyboard'
=>
true
,
'selective'
=>
true
,
]
);
switch
(
$state
)
{
case
-
1
:
// getConfig has not been configured asking for channel to administer
...
...
@@ -96,9 +105,13 @@ class SendtochannelCommand extends AdminCommand
$notes
[
'state'
]
=
-
1
;
$this
->
conversation
->
update
();
$data
[
'text'
]
=
'Insert the channel name: (@yourchannel)'
;
$data
[
'reply_markup'
]
=
Keyboard
::
remove
([
'selective'
=>
true
]);
$result
=
Request
::
sendMessage
(
$data
);
$result
=
$this
->
replyToChat
(
'Insert the channel name or ID (_@yourchannel_ or _-12345_)'
,
[
'parse_mode'
=>
'markdown'
,
'reply_markup'
=>
Keyboard
::
remove
([
'selective'
=>
true
]),
]
);
break
;
}
...
...
@@ -111,25 +124,29 @@ class SendtochannelCommand extends AdminCommand
default
:
case
0
:
// getConfig has been configured choose channel
if
(
$type
!==
'message'
||
!
in_array
(
$text
,
$channels
,
true
)
)
{
if
(
$type
!==
'message'
||
$text
===
''
)
{
$notes
[
'state'
]
=
0
;
$this
->
conversation
->
update
();
$keyboard
=
[];
foreach
(
$channels
as
$channel
)
{
$keyboard
[]
=
[
$channel
];
}
$data
[
'reply_markup'
]
=
new
Keyboard
(
$keyboard
=
array_map
(
function
(
$channel
)
{
return
[
$channel
];
},
$channels
);
$result
=
$this
->
replyToChat
(
'Choose a channel from the keyboard'
.
PHP_EOL
.
'_or_ insert the channel name or ID (_@yourchannel_ or _-12345_)'
,
[
'parse_mode'
=>
'markdown'
,
'reply_markup'
=>
new
Keyboard
(
[
'keyboard'
=>
$keyboard
,
'resize_keyboard'
=>
true
,
'one_time_keyboard'
=>
true
,
'selective'
=>
true
,
]
),
]
);
$data
[
'text'
]
=
'Select a channel from the keyboard:'
;
$result
=
Request
::
sendMessage
(
$data
);
break
;
}
$notes
[
'channel'
]
=
$text
;
...
...
@@ -142,9 +159,10 @@ class SendtochannelCommand extends AdminCommand
$notes
[
'state'
]
=
1
;
$this
->
conversation
->
update
();
$data
[
'reply_markup'
]
=
Keyboard
::
remove
([
'selective'
=>
true
]);
$data
[
'text'
]
=
'Insert the content you want to share: text, photo, audio...'
;
$result
=
Request
::
sendMessage
(
$data
);
$result
=
$this
->
replyToChat
(
'Insert the content you want to share: text, photo, audio...'
,
[
'reply_markup'
=>
Keyboard
::
remove
([
'selective'
=>
true
])]
);
break
;
}
$notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
...
...
@@ -156,22 +174,20 @@ class SendtochannelCommand extends AdminCommand
$notes
[
'state'
]
=
2
;
$this
->
conversation
->
update
();
// Execute this just with object that allow caption
if
(
in_array
(
$notes
[
'message_type'
],
[
'video'
,
'photo'
],
true
))
{
$data
[
'reply_markup'
]
=
new
Keyboard
(
[
'keyboard'
=>
[[
'Yes'
,
'No'
]],
'resize_keyboard'
=>
true
,
'one_time_keyboard'
=>
true
,
'selective'
=>
true
,
]
);
$data
[
'text'
]
=
'Would you like to insert a caption?'
;
// Grab any existing caption.
if
(
$caption
=
$message
->
getCaption
())
{
$notes
[
'caption'
]
=
$caption
;
$text
=
'No'
;
}
elseif
(
in_array
(
$notes
[
'message_type'
],
[
'video'
,
'photo'
],
true
))
{
$text
=
'Would you like to insert a caption?'
;
if
(
!
$text_yes_or_no
&&
$notes
[
'last_message_id'
]
!==
$message
->
getMessageId
())
{
$
data
[
'text'
]
.=
PHP_EOL
.
'Type Yes or No'
;
$
text
.=
PHP_EOL
.
'Type Yes or No'
;
}
$result
=
Request
::
sendMessage
(
$data
);
$result
=
$this
->
replyToChat
(
$text
,
[
'reply_markup'
=>
$yes_no_keyboard
]
);
break
;
}
}
...
...
@@ -183,21 +199,26 @@ class SendtochannelCommand extends AdminCommand
$notes
[
'state'
]
=
3
;
$this
->
conversation
->
update
();
$data
[
'text'
]
=
'Insert caption:'
;
$data
[
'reply_markup'
]
=
Keyboard
::
remove
([
'selective'
=>
true
]);
$result
=
Request
::
sendMessage
(
$data
);
$result
=
$this
->
replyToChat
(
'Insert caption:'
,
[
'reply_markup'
=>
Keyboard
::
remove
([
'selective'
=>
true
])]
);
break
;
}
$notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
if
(
isset
(
$notes
[
'caption'
]))
{
// If caption has already been send with the file, no need to ask for it.
$notes
[
'set_caption'
]
=
true
;
}
else
{
$notes
[
'caption'
]
=
$text
;
}
// no break
case
4
:
if
(
!
$text_yes_or_no
||
$notes
[
'last_message_id'
]
===
$message
->
getMessageId
())
{
$notes
[
'state'
]
=
4
;
$this
->
conversation
->
update
();
$data
[
'text'
]
=
'Message will look like this:'
;
$result
=
Request
::
sendMessage
(
$data
);
$result
=
$this
->
replyToChat
(
'Message will look like this:'
);
if
(
$notes
[
'message_type'
]
!==
'command'
)
{
if
(
$notes
[
'set_caption'
])
{
...
...
@@ -205,14 +226,7 @@ class SendtochannelCommand extends AdminCommand
}
$this
->
sendBack
(
new
Message
(
$notes
[
'message'
],
$this
->
telegram
->
getBotUsername
()),
$data
);
$data
[
'reply_markup'
]
=
new
Keyboard
(
[
'keyboard'
=>
[[
'Yes'
,
'No'
]],
'resize_keyboard'
=>
true
,
'one_time_keyboard'
=>
true
,
'selective'
=>
true
,
]
);
$data
[
'reply_markup'
]
=
$yes_no_keyboard
;
$data
[
'text'
]
=
'Would you like to post it?'
;
if
(
!
$text_yes_or_no
&&
$notes
[
'last_message_id'
]
!==
$message
->
getMessageId
())
{
...
...
@@ -230,13 +244,14 @@ class SendtochannelCommand extends AdminCommand
$data
[
'reply_markup'
]
=
Keyboard
::
remove
([
'selective'
=>
true
]);
if
(
$notes
[
'post_message'
])
{
$data
[
'parse_mode'
]
=
'markdown'
;
$data
[
'text'
]
=
$this
->
publish
(
new
Message
(
$notes
[
'message'
],
$this
->
telegram
->
getBotUsername
()),
$notes
[
'channel'
],
$notes
[
'caption'
]
);
}
else
{
$data
[
'text'
]
=
'Abort by user, message not sent..'
;
$data
[
'text'
]
=
'Abort
ed
by user, message not sent..'
;
}
$this
->
conversation
->
stop
();
...
...
@@ -294,26 +309,35 @@ class SendtochannelCommand extends AdminCommand
}
/**
* Publish a message to a channel and return success or failure message
* Publish a message to a channel and return success or failure message
in markdown format
*
* @param \Longman\TelegramBot\Entities\Message $message
* @param
int $channel
* @param
string|int $channel_id
* @param string|null $caption
*
* @return string
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
protected
function
publish
(
Message
$message
,
$channel
,
$caption
=
null
)
protected
function
publish
(
Message
$message
,
$channel
_id
,
$caption
=
null
)
{
$
data
=
[
'chat_id'
=>
$channel
,
$
res
=
$this
->
sendBack
(
$message
,
[
'chat_id'
=>
$channel
_id
,
'caption'
=>
$caption
,
];
]
)
;
if
(
$this
->
sendBack
(
$message
,
$data
)
->
isOk
())
{
$response
=
'Message sent successfully to: '
.
$channel
;
if
(
$res
->
isOk
())
{
/** @var Chat $channel */
$channel
=
$res
->
getResult
()
->
getChat
();
$escaped_username
=
$channel
->
getUsername
()
?
$this
->
getMessage
()
->
escapeMarkdown
(
$channel
->
getUsername
())
:
''
;
$response
=
sprintf
(
'Message successfully sent to *%s*%s'
,
filter_var
(
$channel
->
getTitle
(),
FILTER_SANITIZE_SPECIAL_CHARS
),
$escaped_username
?
" (@
{
$escaped_username
}
)"
:
''
);
}
else
{
$response
=
'Message not sent to: '
.
$channel
.
PHP_EOL
.
$escaped_username
=
$this
->
getMessage
()
->
escapeMarkdown
(
$channel_id
);
$response
=
"Message not sent to *
{
$escaped_username
}
*"
.
PHP_EOL
.
'- Does the channel exist?'
.
PHP_EOL
.
'- Is the bot an admin of the channel?'
;
}
...
...
@@ -332,23 +356,20 @@ class SendtochannelCommand extends AdminCommand
public
function
executeNoDb
()
{
$message
=
$this
->
getMessage
();
$chat_id
=
$message
->
getChat
()
->
getId
();
$text
=
trim
(
$message
->
getText
(
true
));
$data
=
[
'chat_id'
=>
$chat_id
,
'text'
=>
'Usage: '
.
$this
->
getUsage
(),
];
if
(
$text
===
''
)
{
return
$this
->
replyToChat
(
'Usage: '
.
$this
->
getUsage
());
}
if
(
$text
!==
''
)
{
$channels
=
(
array
)
$this
->
getConfig
(
'your_channel'
);
$first_channel
=
$channels
[
0
];
$data
[
'text'
]
=
$this
->
publish
(
new
Message
(
$message
->
getRawData
(),
$this
->
telegram
->
getBotUsername
()),
$first_channel
);
$channels
=
array_filter
((
array
)
$this
->
getConfig
(
'your_channel'
));
if
(
empty
(
$channels
))
{
return
$this
->
replyToChat
(
'No channels defined in the command config!'
);
}
return
Request
::
sendMessage
(
$data
);
return
$this
->
replyToChat
(
$this
->
publish
(
new
Message
(
$message
->
getRawData
(),
$this
->
telegram
->
getBotUsername
()),
reset
(
$channels
)
),
[
'parse_mode'
=>
'markdown'
]);
}
}
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