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
b5e22037
Commit
b5e22037
authored
Feb 27, 2016
by
Marco Boretto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4 from MBoretto/cancel_command
Cancel command
parents
408085f5
93152b3b
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
108 additions
and
15 deletions
+108
-15
CancelCommand.php
src/Commands/UserCommands/CancelCommand.php
+81
-0
Conversation.php
src/Conversation.php
+27
-15
No files found.
src/Commands/UserCommands/CancelCommand.php
0 → 100644
View file @
b5e22037
<?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\UserCommands
;
use
Longman\TelegramBot\Commands\UserCommand
;
use
Longman\TelegramBot\Conversation
;
use
Longman\TelegramBot\Entities\ReplyKeyboardHide
;
use
Longman\TelegramBot\Request
;
/**
* User "/cancel" command
*
* This command cancels the currently active conversation and
* returns a message to let the user know which conversation it was.
* If no conversation is active, the returned message says so.
*/
class
CancelCommand
extends
UserCommand
{
/**#@+
* {@inheritdoc}
*/
protected
$name
=
'cancel'
;
protected
$description
=
'Cancel the currently active conversation'
;
protected
$usage
=
'/cancel'
;
protected
$version
=
'0.1.0'
;
protected
$need_mysql
=
true
;
/**#@-*/
/**
* {@inheritdoc}
*/
public
function
execute
()
{
$text
=
'No active conversation!'
;
//Cancel current conversation if any
$conversation
=
new
Conversation
(
$this
->
getMessage
()
->
getFrom
()
->
getId
(),
$this
->
getMessage
()
->
getChat
()
->
getId
()
);
if
(
$conversation_command
=
$conversation
->
getConversationCommand
())
{
$conversation
->
cancel
();
$text
=
'Conversation "'
.
$conversation_command
.
'" cancelled!'
;
}
return
$this
->
hideKeyboard
(
$text
);
}
/**
* {@inheritdoc}
*/
public
function
executeNoDB
()
{
return
$this
->
hideKeyboard
();
}
/**
* Hide the keyboard and output a text
*
* @param string $text
*
* @return Entities\ServerResponse
*/
private
function
hideKeyboard
(
$text
=
''
)
{
return
Request
::
sendMessage
([
'reply_markup'
=>
new
ReplyKeyboardHide
([
'selective'
=>
true
]),
'chat_id'
=>
$this
->
getMessage
()
->
getChat
()
->
getId
(),
'text'
=>
$text
,
]);
}
}
src/Conversation.php
View file @
b5e22037
...
...
@@ -121,7 +121,7 @@ class Conversation
}
//A conversation with a different name has been opened, unset the DB one and recreate a new one
ConversationDB
::
updateConversation
([
'status'
=>
'cancelled'
],
[
'chat_id'
=>
$this
->
chat_id
,
'user_id'
=>
$this
->
user_id
,
'status'
=>
'active'
]
);
$this
->
cancel
(
);
return
false
;
}
...
...
@@ -144,35 +144,47 @@ class Conversation
}
/**
*
Store the array/variable in the database with json_encode() function
*
Delete the conversation from the database
*
*
@todo Verify the query before assigning the $data member variable
*
Currently the Conversation is not deleted but just set to 'stopped'
*
* @
param array $data
* @
todo should return something
*/
public
function
update
(
$data
)
public
function
stop
(
)
{
//Conversation must exist!
if
(
$this
->
exist
())
{
$fields
[
'data'
]
=
json_encode
(
$data
);
ConversationDB
::
updateConversation
([
'status'
=>
'stopped'
],
[
'chat_id'
=>
$this
->
chat_id
,
'user_id'
=>
$this
->
user_id
,
'status'
=>
'active'
]);
}
}
ConversationDB
::
updateConversation
(
$fields
,
[
'chat_id'
=>
$this
->
chat_id
,
'user_id'
=>
$this
->
user_id
,
'status'
=>
'active'
]);
//TODO verify query success before convert the private var
$this
->
data
=
$data
;
/**
* Set to Cancelled the conversation in the database
*
* @todo should return something
*/
public
function
cancel
()
{
if
(
$this
->
exist
())
{
ConversationDB
::
updateConversation
([
'status'
=>
'cancelled'
],
[
'chat_id'
=>
$this
->
chat_id
,
'user_id'
=>
$this
->
user_id
,
'status'
=>
'active'
]);
}
}
/**
*
Delete the conversation from the database
*
Store the array/variable in the database with json_encode() function
*
*
Currently the Conversation is not deleted but just set to 'stopped'
*
@todo Verify the query before assigning the $data member variable
*
* @
todo should return something
* @
param array $data
*/
public
function
stop
(
)
public
function
update
(
$data
)
{
//Conversation must exist!
if
(
$this
->
exist
())
{
ConversationDB
::
updateConversation
([
'status'
=>
'stopped'
],
[
'chat_id'
=>
$this
->
chat_id
,
'user_id'
=>
$this
->
user_id
,
'status'
=>
'active'
]);
$fields
[
'data'
]
=
json_encode
(
$data
);
ConversationDB
::
updateConversation
(
$fields
,
[
'chat_id'
=>
$this
->
chat_id
,
'user_id'
=>
$this
->
user_id
,
'status'
=>
'active'
]);
//TODO verify query success before convert the private var
$this
->
data
=
$data
;
}
}
...
...
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