-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathHelpers.php
More file actions
105 lines (93 loc) · 2.99 KB
/
Helpers.php
File metadata and controls
105 lines (93 loc) · 2.99 KB
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
<?php
/**
* This file is part of the PHP Telegram Support Bot.
*
* (c) PHP Telegram Bot Team (https://github.com/php-telegram-bot)
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace TelegramBot\SupportBot;
use PhpTelegramBot\Core\DB;
use PhpTelegramBot\Core\Request;
use PhpTelegramBot\Core\TelegramLog;
class Helpers
{
/**
* Get a simple option value.
*
* @todo: Move into core!
*
* @param string $name
* @param mixed $default
*
* @return mixed
*/
public static function getSimpleOption($name, $default = false)
{
return json_decode(DB::getPdo()->query("
SELECT `value`
FROM `simple_options`
WHERE `name` = '{$name}'
")->fetchColumn() ?: '', true) ?? $default;
}
/**
* Set a simple option value.
*
* @todo: Move into core!
*
* @param string $name
* @param mixed $value
*
* @return bool
*/
public static function setSimpleOption($name, $value): bool
{
return DB::getPdo()->prepare("
INSERT INTO `simple_options`
(`name`, `value`) VALUES (?, ?)
ON DUPLICATE KEY UPDATE
`name` = VALUES(`name`),
`value` = VALUES(`value`)
")->execute([$name, json_encode($value)]);
}
/**
* Delete any old welcome messages from the group.
*/
public static function deleteOldWelcomeMessages(): void
{
$chat_id = getenv('TG_SUPPORT_GROUP_ID');
$welcome_message_ids = self::getSimpleOption('welcome_message_ids', []);
foreach ($welcome_message_ids as $key => $message_id) {
// Be sure to keep the latest one.
if ($key === 'latest') {
continue;
}
$deletion = Request::deleteMessage(compact('chat_id', 'message_id'));
if (!$deletion->isOk()) {
// Let's just save the error for now if it fails, to see if we can fix this better.
TelegramLog::error(sprintf(
'Chat ID: %s, Message ID: %s, Error Code: %s, Error Message: %s',
$chat_id,
$message_id,
$deletion->getErrorCode(),
$deletion->getDescription()
));
}
unset($welcome_message_ids[$key]);
}
self::setSimpleOption('welcome_message_ids', $welcome_message_ids);
}
/**
* Save the latest welcome message to the option.
*
* @param int $welcome_message_id
*/
public static function saveLatestWelcomeMessage($welcome_message_id): void
{
$welcome_message_ids = self::getSimpleOption('welcome_message_ids', []);
$new_welcome_message_ids = array_values($welcome_message_ids) + ['latest' => $welcome_message_id];
self::setSimpleOption('welcome_message_ids', $new_welcome_message_ids);
}
}