-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9939c3b
Showing
20 changed files
with
734 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
vendor | ||
.idea | ||
composer.lock | ||
*.local.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
use Config\DbConfig; | ||
use Config\BotConfig; | ||
use MrMuminov\PhpI18n\I18n; | ||
|
||
class App | ||
{ | ||
public static DbConfig $database; | ||
public static bool $debug = false; | ||
public static BotConfig $bot; | ||
public static I18n $i18n; | ||
|
||
public function __construct( | ||
DbConfig $database, | ||
BotConfig $bot, | ||
I18n $i18n, | ||
bool $debug = false, | ||
) | ||
{ | ||
self::$database = $database; | ||
self::$bot = $bot; | ||
self::$debug = $debug; | ||
self::$i18n = $i18n; | ||
} | ||
|
||
public static function log(mixed $message): void | ||
{ | ||
@fputs(@fopen('php://stdout', 'a+'), print_r($message, 1) . PHP_EOL); | ||
} | ||
|
||
public static function debug(mixed $message): void | ||
{ | ||
@fputs(@fopen('php://stdout', 'a+'), var_export($message, 1) . PHP_EOL); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Commands; | ||
|
||
use App; | ||
use Services\UserService; | ||
use Longman\TelegramBot\Request; | ||
use Longman\TelegramBot\Telegram; | ||
use Longman\TelegramBot\Entities\Update; | ||
use Longman\TelegramBot\Commands\UserCommand; | ||
use Longman\TelegramBot\Entities\ServerResponse; | ||
|
||
class HelloCommand extends UserCommand | ||
{ | ||
protected $name = 'hello'; | ||
protected $description = 'A command for hello'; | ||
protected $usage = '/hello'; | ||
protected $version = '1.0.0'; | ||
|
||
protected UserService $userService; | ||
|
||
public function __construct(Telegram $telegram, ?Update $update = null) | ||
{ | ||
$this->userService = new UserService(); | ||
parent::__construct($telegram, $update); | ||
} | ||
|
||
public function execute(): ServerResponse | ||
{ | ||
|
||
$chat_id = $this->getMessage()->getChat()->getId(); | ||
$user = $this->userService->getByChatId($chat_id); | ||
if ($user === null) { | ||
return Request::sendMessage([ | ||
'chat_id' => $chat_id, | ||
'text' => App::$i18n->get("Please, send /start command!"), | ||
]); | ||
} | ||
$user->step = 'hello'; | ||
$this->userService->update($user); | ||
|
||
return Request::sendMessage([ | ||
'chat_id' => $chat_id, | ||
'text' => App::$i18n->get("Hello"), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Commands; | ||
|
||
use App; | ||
use Services\UserService; | ||
use Longman\TelegramBot\Request; | ||
use Longman\TelegramBot\Telegram; | ||
use Longman\TelegramBot\Entities\Update; | ||
use Longman\TelegramBot\Commands\UserCommand; | ||
use Longman\TelegramBot\Entities\ServerResponse; | ||
|
||
class StartCommand extends UserCommand | ||
{ | ||
protected $name = 'start'; | ||
protected $description = 'A command for Start'; | ||
protected $usage = '/start'; | ||
protected $version = '1.0.0'; | ||
|
||
protected UserService $userService; | ||
|
||
public function __construct(Telegram $telegram, ?Update $update = null) | ||
{ | ||
$this->userService = new UserService(); | ||
parent::__construct(telegram: $telegram, update: $update); | ||
} | ||
|
||
public function execute(): ServerResponse | ||
{ | ||
$chat_id = $this->getMessage()->getChat()->getId(); | ||
$user = $this->userService->getByChatId($chat_id); | ||
if ($user === null) { | ||
$this->userService->create( | ||
chat_id: $chat_id, | ||
step: 'start', | ||
); | ||
return Request::sendMessage([ | ||
'chat_id' => $chat_id, | ||
'text' => App::$i18n->get("Hello"), | ||
]); | ||
} | ||
$user->step = 'start'; | ||
$this->userService->update($user); | ||
return Request::sendMessage([ | ||
'chat_id' => $chat_id, | ||
'text' => App::$i18n->get("Home"), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Config; | ||
|
||
class BotConfig | ||
{ | ||
public function __construct( | ||
public string $bot_api_token, | ||
public string $bot_username, | ||
public string $webhook_url, | ||
) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
------------------------------ | ||
-- user table ---------------- | ||
CREATE TABLE IF NOT EXISTS public."user" | ||
( | ||
id BIGSERIAL NOT NULL | ||
CONSTRAINT user_pk PRIMARY KEY, | ||
chat_id BIGINT NOT NULL, | ||
step VARCHAR, | ||
language VARCHAR(10), | ||
status VARCHAR DEFAULT 'ACTIVE' NOT NULL, | ||
created_at INTEGER NOT NULL | ||
); | ||
|
||
CREATE UNIQUE INDEX IF NOT EXISTS user_chat_id_uindex | ||
ON public."user" (chat_id); | ||
|
||
CREATE INDEX IF NOT EXISTS user_status_index | ||
ON public."user" (status); | ||
|
||
|
||
------------------------------ | ||
-- user_username table ---------------- | ||
CREATE TABLE IF NOT EXISTS public."user_username" | ||
( | ||
id BIGSERIAL NOT NULL | ||
CONSTRAINT user_username_pk PRIMARY KEY, | ||
chat_id BIGINT NOT NULL, | ||
username VARCHAR(32), | ||
status VARCHAR DEFAULT 'ACTIVE' NOT NULL, | ||
created_at INTEGER NOT NULL | ||
); | ||
|
||
CREATE UNIQUE INDEX IF NOT EXISTS user_username_chat_id_uindex | ||
ON public."user_username" (chat_id); | ||
|
||
CREATE INDEX IF NOT EXISTS user_username_status_index | ||
ON public."user_username" (status); | ||
|
||
|
||
------------------------------ | ||
-- user_first_name table ---------------- | ||
CREATE TABLE IF NOT EXISTS public."user_first_name" | ||
( | ||
id BIGSERIAL NOT NULL | ||
CONSTRAINT user_first_name_pk PRIMARY KEY, | ||
chat_id BIGINT NOT NULL, | ||
first_name VARCHAR(64), | ||
status VARCHAR DEFAULT 'ACTIVE' NOT NULL, | ||
created_at INTEGER NOT NULL | ||
); | ||
|
||
CREATE UNIQUE INDEX IF NOT EXISTS user_first_name_chat_id_uindex | ||
ON public."user_first_name" (chat_id); | ||
|
||
CREATE INDEX IF NOT EXISTS user_first_name_status_index | ||
ON public."user_first_name" (status); | ||
|
||
|
||
------------------------------ | ||
-- user_last_name table ---------------- | ||
CREATE TABLE IF NOT EXISTS public."user_last_name" | ||
( | ||
id BIGSERIAL NOT NULL | ||
CONSTRAINT user_last_name_pk PRIMARY KEY, | ||
chat_id BIGINT NOT NULL, | ||
last_name VARCHAR(64), | ||
status VARCHAR DEFAULT 'ACTIVE' NOT NULL, | ||
created_at INTEGER NOT NULL | ||
); | ||
|
||
CREATE UNIQUE INDEX IF NOT EXISTS user_last_name_chat_id_uindex | ||
ON public."user_last_name" (chat_id); | ||
|
||
CREATE INDEX IF NOT EXISTS user_last_name_status_index | ||
ON public."user_last_name" (status); | ||
|
||
|
||
------------------------------ | ||
-- user_bio table ---------------- | ||
CREATE TABLE IF NOT EXISTS public."user_bio" | ||
( | ||
id BIGSERIAL NOT NULL | ||
CONSTRAINT user_bio_pk PRIMARY KEY, | ||
chat_id BIGINT NOT NULL, | ||
bio VARCHAR(200), | ||
status VARCHAR DEFAULT 'ACTIVE' NOT NULL, | ||
created_at INTEGER NOT NULL | ||
); | ||
|
||
CREATE UNIQUE INDEX IF NOT EXISTS user_bio_chat_id_uindex | ||
ON public."user_bio" (chat_id); | ||
|
||
CREATE INDEX IF NOT EXISTS user_bio_status_index | ||
ON public."user_bio" (status); | ||
|
||
|
||
------------------------------ | ||
-- user_phone table ---------------- | ||
CREATE TABLE IF NOT EXISTS public."user_phone" | ||
( | ||
id BIGSERIAL NOT NULL | ||
CONSTRAINT user_phone_pk PRIMARY KEY, | ||
chat_id BIGINT NOT NULL, | ||
phone VARCHAR(20), | ||
status VARCHAR DEFAULT 'ACTIVE' NOT NULL, | ||
created_at INTEGER NOT NULL | ||
); | ||
|
||
CREATE UNIQUE INDEX IF NOT EXISTS user_phone_chat_id_uindex | ||
ON public."user_phone" (chat_id); | ||
|
||
CREATE INDEX IF NOT EXISTS user_phone_status_index | ||
ON public."user_phone" (status); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Config; | ||
|
||
use PDO; | ||
|
||
class DbConfig | ||
{ | ||
public static PDO $pdo; | ||
|
||
public function __construct( | ||
public string $schema = 'pgsql', | ||
public string $host = 'localhost', | ||
public int $port = 5432, | ||
public string $username = 'postgres', | ||
public string $password = '', | ||
public string $dbname = '', | ||
public string $charset = 'utf8', | ||
) | ||
{ | ||
self::$pdo = new PDO( | ||
dsn: $this->schema . ':' . | ||
'host=' . $this->host . ';' . | ||
'port=' . $this->port . ';' . | ||
'dbname=' . $this->dbname . ';', | ||
username: $this->username, | ||
password: $this->password, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Enums; | ||
|
||
enum StatusEnum: string | ||
{ | ||
case ACTIVE = 'ACTIVE'; | ||
case INACTIVE = 'INACTIVE'; | ||
case DELETED = 'DELETED'; | ||
|
||
public function label(): string | ||
{ | ||
return StatusEnum::getLabel($this); | ||
} | ||
|
||
public static function getLabel(self $value): string | ||
{ | ||
return match ($value) { | ||
StatusEnum::ACTIVE => 'ACTIVE', | ||
StatusEnum::INACTIVE => 'INACTIVE', | ||
StatusEnum::DELETED => 'DELETED', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
return [ | ||
'code' => 'en', | ||
'language' => 'English', | ||
'Hello' => 'Hello', | ||
'Home' => 'Home', | ||
'Please, send /start command!' => 'Please, send /start command!', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Models; | ||
|
||
abstract class BaseModel implements ModelInterface | ||
{ | ||
public bool $is_new = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Models; | ||
|
||
interface ModelInterface | ||
{ | ||
public static function tableName(): string; | ||
|
||
public function attributes(): array; | ||
} |
Oops, something went wrong.