Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F625364
ConversationDB.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None
ConversationDB.php
View Options
<?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
;
use
Exception
;
use
Longman
\
TelegramBot
\
Exception
\
TelegramException
;
use
PDO
;
class
ConversationDB
extends
DB
{
/**
* Initialize conversation table
*/
public
static
function
initializeConversation
(
)
{
if
(
!
defined
(
'TB_CONVERSATION'
)
)
{
define
(
'TB_CONVERSATION'
,
self
::
$table_prefix
.
'conversation'
)
;
}
}
/**
* Select a conversation from the DB
*
* @param string $user_id
* @param string $chat_id
* @param int|null $limit
*
* @return array|bool
* @throws TelegramException
*/
public
static
function
selectConversation
(
$user_id
,
$chat_id
,
$limit
=
null
)
{
if
(
!
self
::
isDbConnected
(
)
)
{
return
false
;
}
try
{
$sql
=
'
SELECT *
FROM `'
.
TB_CONVERSATION
.
'`
WHERE `status` = :status
AND `chat_id` = :chat_id
AND `user_id` = :user_id
'
;
if
(
$limit
!==
null
)
{
$sql
.=
' LIMIT :limit'
;
}
$sth
=
self
::
$pdo
->
prepare
(
$sql
)
;
$sth
->
bindValue
(
':status'
,
'active'
)
;
$sth
->
bindValue
(
':user_id'
,
$user_id
)
;
$sth
->
bindValue
(
':chat_id'
,
$chat_id
)
;
if
(
$limit
!==
null
)
{
$sth
->
bindValue
(
':limit'
,
$limit
,
PDO
::
PARAM_INT
)
;
}
$sth
->
execute
(
)
;
return
$sth
->
fetchAll
(
PDO
::
FETCH_ASSOC
)
;
}
catch
(
Exception
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
(
)
)
;
}
}
/**
* Insert the conversation in the database
*
* @param string $user_id
* @param string $chat_id
* @param string $command
*
* @return bool
* @throws TelegramException
*/
public
static
function
insertConversation
(
$user_id
,
$chat_id
,
$command
)
{
if
(
!
self
::
isDbConnected
(
)
)
{
return
false
;
}
try
{
$sth
=
self
::
$pdo
->
prepare
(
'INSERT INTO `'
.
TB_CONVERSATION
.
'`
(`status`, `user_id`, `chat_id`, `command`, `notes`, `created_at`, `updated_at`)
VALUES
(:status, :user_id, :chat_id, :command, :notes, :created_at, :updated_at)
'
)
;
$date
=
self
::
getTimestamp
(
)
;
$sth
->
bindValue
(
':status'
,
'active'
)
;
$sth
->
bindValue
(
':command'
,
$command
)
;
$sth
->
bindValue
(
':user_id'
,
$user_id
)
;
$sth
->
bindValue
(
':chat_id'
,
$chat_id
)
;
$sth
->
bindValue
(
':notes'
,
'[]'
)
;
$sth
->
bindValue
(
':created_at'
,
$date
)
;
$sth
->
bindValue
(
':updated_at'
,
$date
)
;
return
$sth
->
execute
(
)
;
}
catch
(
Exception
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
(
)
)
;
}
}
/**
* Update a specific conversation
*
* @param array $fields_values
* @param array $where_fields_values
*
* @return bool
* @throws TelegramException
*/
public
static
function
updateConversation
(
array
$fields_values
,
array
$where_fields_values
)
{
// Auto update the update_at field.
$fields_values
[
'updated_at'
]
=
self
::
getTimestamp
(
)
;
return
self
::
update
(
TB_CONVERSATION
,
$fields_values
,
$where_fields_values
)
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, Nov 14, 11:22 PM (1 d, 12 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
118594
Default Alt Text
ConversationDB.php (3 KB)
Attached To
Mode
R123 php-telegram-bot
Attached
Detach File
Event Timeline