-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtg-reflect.php
53 lines (44 loc) · 1.65 KB
/
tg-reflect.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
<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/settings.php';
use Telegram\Bot\Api;
use Telegram\Bot\Exceptions\TelegramSDKException;
// Set up the connection to the SQLite database
$db_file = __DIR__ . '/prompts.sqlite';
$db = new SQLite3($db_file);
// Set up the Telegram bot
$telegram = new Api($botToken);
// Retrieve a random question from the database
$query = 'SELECT body,id FROM prompts WHERE (last_used IS NULL OR last_used = 0) ORDER BY RANDOM() LIMIT 1';
$result = $db->query($query);
$row = $result->fetchArray();
// If no question found, select 100 oldest prompts and choose randomly from them
if (! $row) {
$query = 'SELECT body,id from (SELECT body,id FROM prompts ORDER BY last_used ASC LIMIT 100) order by random() limit 1';
$result = $db->query($query);
$rows = [];
while ($row = $result->fetchArray()) {
$rows[] = $row['body'] . '(' . $row['id'] . ')';
}
$question = $rows[array_rand($rows)];
} else {
$question = $row['body'] . ' (' . $row['id'] . ')';
}
// Update the `last_used` column with the current timestamp
$updateQuery = 'UPDATE prompts SET last_used = :timestamp WHERE body = :question';
$stmt = $db->prepare($updateQuery);
$stmt->bindValue(':timestamp', time(), SQLITE3_INTEGER);
$stmt->bindValue(':question', $question, SQLITE3_TEXT);
$stmt->execute();
// Send the question as a message to the Telegram chat using the Telegram Bot API
try {
$telegram->sendMessage([
'chat_id' => $chatId,
'text' => $question,
]);
} catch (TelegramSDKException $e) {
// Handle exceptions from the Telegram Bot API
echo $e->getMessage();
}
// Close the database connection
$db->close();