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
bc0d810e
Unverified
Commit
bc0d810e
authored
Mar 23, 2018
by
Armando Lüscher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve and simplify sendtochannel command.
parent
09f23ff9
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
101 additions
and
80 deletions
+101
-80
SendtochannelCommand.php
src/Commands/AdminCommands/SendtochannelCommand.php
+101
-80
No files found.
src/Commands/AdminCommands/SendtochannelCommand.php
View file @
bc0d810e
...
@@ -10,12 +10,12 @@
...
@@ -10,12 +10,12 @@
namespace
Longman\TelegramBot\Commands\AdminCommands
;
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\Commands\AdminCommand
;
use
Longman\TelegramBot\Conversation
;
use
Longman\TelegramBot\Entities\Chat
;
use
Longman\TelegramBot\Entities\Keyboard
;
use
Longman\TelegramBot\Entities\Message
;
use
Longman\TelegramBot\Entities\Message
;
use
Longman\TelegramBot\
Exception\TelegramException
;
use
Longman\TelegramBot\
Request
;
class
SendtochannelCommand
extends
AdminCommand
class
SendtochannelCommand
extends
AdminCommand
{
{
...
@@ -37,7 +37,7 @@ class SendtochannelCommand extends AdminCommand
...
@@ -37,7 +37,7 @@ class SendtochannelCommand extends AdminCommand
/**
/**
* @var string
* @var string
*/
*/
protected
$version
=
'0.
2
.0'
;
protected
$version
=
'0.
3
.0'
;
/**
/**
* @var bool
* @var bool
...
@@ -64,8 +64,8 @@ class SendtochannelCommand extends AdminCommand
...
@@ -64,8 +64,8 @@ class SendtochannelCommand extends AdminCommand
$user_id
=
$message
->
getFrom
()
->
getId
();
$user_id
=
$message
->
getFrom
()
->
getId
();
$type
=
$message
->
getType
();
$type
=
$message
->
getType
();
// 'Cast' the command type
in
to message to protect the machine state
// 'Cast' the command type to message to protect the machine state
// if the comm
ma
d is recalled when the conversation is already started
// if the comm
an
d is recalled when the conversation is already started
in_array
(
$type
,
[
'command'
,
'text'
],
true
)
&&
$type
=
'message'
;
in_array
(
$type
,
[
'command'
,
'text'
],
true
)
&&
$type
=
'message'
;
$text
=
trim
(
$message
->
getText
(
true
));
$text
=
trim
(
$message
->
getText
(
true
));
...
@@ -89,6 +89,15 @@ class SendtochannelCommand extends AdminCommand
...
@@ -89,6 +89,15 @@ class SendtochannelCommand extends AdminCommand
$notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
$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
)
{
switch
(
$state
)
{
case
-
1
:
case
-
1
:
// getConfig has not been configured asking for channel to administer
// getConfig has not been configured asking for channel to administer
...
@@ -96,9 +105,13 @@ class SendtochannelCommand extends AdminCommand
...
@@ -96,9 +105,13 @@ class SendtochannelCommand extends AdminCommand
$notes
[
'state'
]
=
-
1
;
$notes
[
'state'
]
=
-
1
;
$this
->
conversation
->
update
();
$this
->
conversation
->
update
();
$data
[
'text'
]
=
'Insert the channel name: (@yourchannel)'
;
$result
=
$this
->
replyToChat
(
$data
[
'reply_markup'
]
=
Keyboard
::
remove
([
'selective'
=>
true
]);
'Insert the channel name or ID (_@yourchannel_ or _-12345_)'
,
$result
=
Request
::
sendMessage
(
$data
);
[
'parse_mode'
=>
'markdown'
,
'reply_markup'
=>
Keyboard
::
remove
([
'selective'
=>
true
]),
]
);
break
;
break
;
}
}
...
@@ -111,25 +124,29 @@ class SendtochannelCommand extends AdminCommand
...
@@ -111,25 +124,29 @@ class SendtochannelCommand extends AdminCommand
default
:
default
:
case
0
:
case
0
:
// getConfig has been configured choose channel
// getConfig has been configured choose channel
if
(
$type
!==
'message'
||
!
in_array
(
$text
,
$channels
,
true
)
)
{
if
(
$type
!==
'message'
||
$text
===
''
)
{
$notes
[
'state'
]
=
0
;
$notes
[
'state'
]
=
0
;
$this
->
conversation
->
update
();
$this
->
conversation
->
update
();
$keyboard
=
[];
$keyboard
=
array_map
(
function
(
$channel
)
{
foreach
(
$channels
as
$channel
)
{
return
[
$channel
];
$keyboard
[]
=
[
$channel
];
},
$channels
);
}
$data
[
'reply_markup'
]
=
new
Keyboard
(
$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
,
'keyboard'
=>
$keyboard
,
'resize_keyboard'
=>
true
,
'resize_keyboard'
=>
true
,
'one_time_keyboard'
=>
true
,
'one_time_keyboard'
=>
true
,
'selective'
=>
true
,
'selective'
=>
true
,
]
]
),
]
);
);
$data
[
'text'
]
=
'Select a channel from the keyboard:'
;
$result
=
Request
::
sendMessage
(
$data
);
break
;
break
;
}
}
$notes
[
'channel'
]
=
$text
;
$notes
[
'channel'
]
=
$text
;
...
@@ -142,9 +159,10 @@ class SendtochannelCommand extends AdminCommand
...
@@ -142,9 +159,10 @@ class SendtochannelCommand extends AdminCommand
$notes
[
'state'
]
=
1
;
$notes
[
'state'
]
=
1
;
$this
->
conversation
->
update
();
$this
->
conversation
->
update
();
$data
[
'reply_markup'
]
=
Keyboard
::
remove
([
'selective'
=>
true
]);
$result
=
$this
->
replyToChat
(
$data
[
'text'
]
=
'Insert the content you want to share: text, photo, audio...'
;
'Insert the content you want to share: text, photo, audio...'
,
$result
=
Request
::
sendMessage
(
$data
);
[
'reply_markup'
=>
Keyboard
::
remove
([
'selective'
=>
true
])]
);
break
;
break
;
}
}
$notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
$notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
...
@@ -156,22 +174,20 @@ class SendtochannelCommand extends AdminCommand
...
@@ -156,22 +174,20 @@ class SendtochannelCommand extends AdminCommand
$notes
[
'state'
]
=
2
;
$notes
[
'state'
]
=
2
;
$this
->
conversation
->
update
();
$this
->
conversation
->
update
();
// Execute this just with object that allow caption
// Grab any existing caption.
if
(
in_array
(
$notes
[
'message_type'
],
[
'video'
,
'photo'
],
true
))
{
if
(
$caption
=
$message
->
getCaption
())
{
$data
[
'reply_markup'
]
=
new
Keyboard
(
$notes
[
'caption'
]
=
$caption
;
[
$text
=
'No'
;
'keyboard'
=>
[[
'Yes'
,
'No'
]],
}
elseif
(
in_array
(
$notes
[
'message_type'
],
[
'video'
,
'photo'
],
true
))
{
'resize_keyboard'
=>
true
,
$text
=
'Would you like to insert a caption?'
;
'one_time_keyboard'
=>
true
,
'selective'
=>
true
,
]
);
$data
[
'text'
]
=
'Would you like to insert a caption?'
;
if
(
!
$text_yes_or_no
&&
$notes
[
'last_message_id'
]
!==
$message
->
getMessageId
())
{
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
;
break
;
}
}
}
}
...
@@ -183,21 +199,26 @@ class SendtochannelCommand extends AdminCommand
...
@@ -183,21 +199,26 @@ class SendtochannelCommand extends AdminCommand
$notes
[
'state'
]
=
3
;
$notes
[
'state'
]
=
3
;
$this
->
conversation
->
update
();
$this
->
conversation
->
update
();
$data
[
'text'
]
=
'Insert caption:'
;
$result
=
$this
->
replyToChat
(
$data
[
'reply_markup'
]
=
Keyboard
::
remove
([
'selective'
=>
true
]);
'Insert caption:'
,
$result
=
Request
::
sendMessage
(
$data
);
[
'reply_markup'
=>
Keyboard
::
remove
([
'selective'
=>
true
])]
);
break
;
break
;
}
}
$notes
[
'last_message_id'
]
=
$message
->
getMessageId
();
$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
;
$notes
[
'caption'
]
=
$text
;
}
// no break
// no break
case
4
:
case
4
:
if
(
!
$text_yes_or_no
||
$notes
[
'last_message_id'
]
===
$message
->
getMessageId
())
{
if
(
!
$text_yes_or_no
||
$notes
[
'last_message_id'
]
===
$message
->
getMessageId
())
{
$notes
[
'state'
]
=
4
;
$notes
[
'state'
]
=
4
;
$this
->
conversation
->
update
();
$this
->
conversation
->
update
();
$data
[
'text'
]
=
'Message will look like this:'
;
$result
=
$this
->
replyToChat
(
'Message will look like this:'
);
$result
=
Request
::
sendMessage
(
$data
);
if
(
$notes
[
'message_type'
]
!==
'command'
)
{
if
(
$notes
[
'message_type'
]
!==
'command'
)
{
if
(
$notes
[
'set_caption'
])
{
if
(
$notes
[
'set_caption'
])
{
...
@@ -205,14 +226,7 @@ class SendtochannelCommand extends AdminCommand
...
@@ -205,14 +226,7 @@ class SendtochannelCommand extends AdminCommand
}
}
$this
->
sendBack
(
new
Message
(
$notes
[
'message'
],
$this
->
telegram
->
getBotUsername
()),
$data
);
$this
->
sendBack
(
new
Message
(
$notes
[
'message'
],
$this
->
telegram
->
getBotUsername
()),
$data
);
$data
[
'reply_markup'
]
=
new
Keyboard
(
$data
[
'reply_markup'
]
=
$yes_no_keyboard
;
[
'keyboard'
=>
[[
'Yes'
,
'No'
]],
'resize_keyboard'
=>
true
,
'one_time_keyboard'
=>
true
,
'selective'
=>
true
,
]
);
$data
[
'text'
]
=
'Would you like to post it?'
;
$data
[
'text'
]
=
'Would you like to post it?'
;
if
(
!
$text_yes_or_no
&&
$notes
[
'last_message_id'
]
!==
$message
->
getMessageId
())
{
if
(
!
$text_yes_or_no
&&
$notes
[
'last_message_id'
]
!==
$message
->
getMessageId
())
{
...
@@ -230,13 +244,14 @@ class SendtochannelCommand extends AdminCommand
...
@@ -230,13 +244,14 @@ class SendtochannelCommand extends AdminCommand
$data
[
'reply_markup'
]
=
Keyboard
::
remove
([
'selective'
=>
true
]);
$data
[
'reply_markup'
]
=
Keyboard
::
remove
([
'selective'
=>
true
]);
if
(
$notes
[
'post_message'
])
{
if
(
$notes
[
'post_message'
])
{
$data
[
'parse_mode'
]
=
'markdown'
;
$data
[
'text'
]
=
$this
->
publish
(
$data
[
'text'
]
=
$this
->
publish
(
new
Message
(
$notes
[
'message'
],
$this
->
telegram
->
getBotUsername
()),
new
Message
(
$notes
[
'message'
],
$this
->
telegram
->
getBotUsername
()),
$notes
[
'channel'
],
$notes
[
'channel'
],
$notes
[
'caption'
]
$notes
[
'caption'
]
);
);
}
else
{
}
else
{
$data
[
'text'
]
=
'Abort by user, message not sent..'
;
$data
[
'text'
]
=
'Abort
ed
by user, message not sent..'
;
}
}
$this
->
conversation
->
stop
();
$this
->
conversation
->
stop
();
...
@@ -294,26 +309,35 @@ class SendtochannelCommand extends AdminCommand
...
@@ -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 \Longman\TelegramBot\Entities\Message $message
* @param
int $channel
* @param
string|int $channel_id
* @param string|null $caption
* @param string|null $caption
*
*
* @return string
* @return string
* @throws \Longman\TelegramBot\Exception\TelegramException
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
*/
protected
function
publish
(
Message
$message
,
$channel
,
$caption
=
null
)
protected
function
publish
(
Message
$message
,
$channel
_id
,
$caption
=
null
)
{
{
$
data
=
[
$
res
=
$this
->
sendBack
(
$message
,
[
'chat_id'
=>
$channel
,
'chat_id'
=>
$channel
_id
,
'caption'
=>
$caption
,
'caption'
=>
$caption
,
];
]
)
;
if
(
$this
->
sendBack
(
$message
,
$data
)
->
isOk
())
{
if
(
$res
->
isOk
())
{
$response
=
'Message sent successfully to: '
.
$channel
;
/** @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
{
}
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
.
'- Does the channel exist?'
.
PHP_EOL
.
'- Is the bot an admin of the channel?'
;
'- Is the bot an admin of the channel?'
;
}
}
...
@@ -332,23 +356,20 @@ class SendtochannelCommand extends AdminCommand
...
@@ -332,23 +356,20 @@ class SendtochannelCommand extends AdminCommand
public
function
executeNoDb
()
public
function
executeNoDb
()
{
{
$message
=
$this
->
getMessage
();
$message
=
$this
->
getMessage
();
$chat_id
=
$message
->
getChat
()
->
getId
();
$text
=
trim
(
$message
->
getText
(
true
));
$text
=
trim
(
$message
->
getText
(
true
));
$data
=
[
if
(
$text
===
''
)
{
'chat_id'
=>
$chat_id
,
return
$this
->
replyToChat
(
'Usage: '
.
$this
->
getUsage
());
'text'
=>
'Usage: '
.
$this
->
getUsage
(),
}
];
if
(
$text
!==
''
)
{
$channels
=
array_filter
((
array
)
$this
->
getConfig
(
'your_channel'
));
$channels
=
(
array
)
$this
->
getConfig
(
'your_channel'
);
if
(
empty
(
$channels
))
{
$first_channel
=
$channels
[
0
];
return
$this
->
replyToChat
(
'No channels defined in the command config!'
);
$data
[
'text'
]
=
$this
->
publish
(
new
Message
(
$message
->
getRawData
(),
$this
->
telegram
->
getBotUsername
()),
$first_channel
);
}
}
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