-
-
Notifications
You must be signed in to change notification settings - Fork 963
/
Copy pathConversationDB.php
132 lines (113 loc) · 3.54 KB
/
ConversationDB.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?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(): void
{
if (!defined('TB_CONVERSATION')) {
define('TB_CONVERSATION', self::$table_prefix . 'conversation');
}
}
/**
* Select a conversation from the DB
*
* @param int $user_id
* @param int $chat_id
* @param int $limit
*
* @return array|bool
* @throws TelegramException
*/
public static function selectConversation(int $user_id, int $chat_id, $limit = 0)
{
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 > 0) {
$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 > 0) {
$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 int $user_id
* @param int $chat_id
* @param string $command
*
* @return bool
* @throws TelegramException
*/
public static function insertConversation(int $user_id, int $chat_id, string $command): bool
{
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): bool
{
// Auto update the update_at field.
$fields_values['updated_at'] = self::getTimestamp();
return self::update(TB_CONVERSATION, $fields_values, $where_fields_values);
}
}