Я не проверял. Точно как пишется не помню, но идея рабочая.
<?php
class SqliteStore {
protected $db;
public function __construct($tableName, $filePath = 'db.sqlite') {
$this->db = new SQLite3($filePath);
$this->tableName = $this->db->escapeString($tableName);
if (is_numeric($tableName[0])) {
$details = sprintf(
"sqlite will choke on table names that start w/ a number. yours starts w/ '%s'",
$tableName[0]
);
throw new Exception($details);
}
//wrap in try/catch & ignore warnings as workaround to lack of 'if not exists' in sqlite version
try {
$sql = "create table $tableName ( key text primary key, value text, tm INT )";
@$this->db->query( $sql );
} catch ( Exception $e ) {
// var_dump($e);
}
}
public function get($key, $defvalue = '') {
$sql = sprintf(
"SELECT value FROM %s WHERE key = '%s';",
$this->tableName, $key
);
$result = $this->db->query($sql)->fetchArray(SQLITE3_ASSOC);
if ($result) {
$result = $result['value'];
} else { $result = $defvalue; }
return $result;
}
public function set($key, $value){
$time = time();
$sql = sprintf(
"REPLACE INTO %s (key, value, tm) VALUES ('%s', '%s', %d);",
$this->tableName, $this->db->escapeString($key), $this->db->escapeString($value), $time
);
//allow exceptions to bubble up
$this->db->exec($sql);
}
public function delete($key){
$sql = sprintf(
"DELETE FROM %s WHERE key = '%s';",
$this->tableName, $this->db->escapeString($key)
);
//allow exceptions to bubble up
$this->db->exec($sql);
}
public function deleteOld($seconds){
$time = time();
$sql = sprintf(
"DELETE FROM %s WHERE (%d - tm > %d);",
$this->tableName, $time, $seconds
);
//allow exceptions to bubble up
$this->db->exec($sql);
}
public function delete_old($seconds){
return $this->deleteOld($seconds);
}
}
$ss = new SqliteStore('tg');
$data = json_decode(file_get_contents('php://input'), TRUE);
$message = trim($data["message"]["text"]);
$chat_id = $data["chat"]["id"];
if ($message === '/start') {
$method = 'sendMessage';
$send_data = [
'text' => "Привет! Я бот для анализа активности подписчиков в каналах, чатах и группах."
];
}
# Проверяем, вводит ли пользователь ссылку.
elseif (strpos($message, 'http') === 0) {
$ss->set('saveChannelLink'.$chat_id, $message);
$method = 'sendMessage';
$send_data = [
'text' => "Отлично! Отправь мне число:"
];
}
elseif (is_numeric($message)) {
$ss->set('saveChannelNumber'.$chat_id, $message);
$method = 'sendMessage';
$saveChannelLink = $ss->get('saveChannelLink'.$chat_id);
$saveChannelNumber = $ss->get('saveChannelNumber'.$chat_id);
$send_data = [
'text' => "Отлично! Ссылка: " . $saveChannelLink . "\nЧисло: " . $saveChannelNumber
];
echo json_encode($send_data);