Skip to content

Commit 9939c3b

Browse files
committed
Initial
0 parents  commit 9939c3b

20 files changed

+734
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vendor
2+
.idea
3+
composer.lock
4+
*.local.*

App.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Config\DbConfig;
4+
use Config\BotConfig;
5+
use MrMuminov\PhpI18n\I18n;
6+
7+
class App
8+
{
9+
public static DbConfig $database;
10+
public static bool $debug = false;
11+
public static BotConfig $bot;
12+
public static I18n $i18n;
13+
14+
public function __construct(
15+
DbConfig $database,
16+
BotConfig $bot,
17+
I18n $i18n,
18+
bool $debug = false,
19+
)
20+
{
21+
self::$database = $database;
22+
self::$bot = $bot;
23+
self::$debug = $debug;
24+
self::$i18n = $i18n;
25+
}
26+
27+
public static function log(mixed $message): void
28+
{
29+
@fputs(@fopen('php://stdout', 'a+'), print_r($message, 1) . PHP_EOL);
30+
}
31+
32+
public static function debug(mixed $message): void
33+
{
34+
@fputs(@fopen('php://stdout', 'a+'), var_export($message, 1) . PHP_EOL);
35+
}
36+
}

Commands/HelloCommand.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Commands;
4+
5+
use App;
6+
use Services\UserService;
7+
use Longman\TelegramBot\Request;
8+
use Longman\TelegramBot\Telegram;
9+
use Longman\TelegramBot\Entities\Update;
10+
use Longman\TelegramBot\Commands\UserCommand;
11+
use Longman\TelegramBot\Entities\ServerResponse;
12+
13+
class HelloCommand extends UserCommand
14+
{
15+
protected $name = 'hello';
16+
protected $description = 'A command for hello';
17+
protected $usage = '/hello';
18+
protected $version = '1.0.0';
19+
20+
protected UserService $userService;
21+
22+
public function __construct(Telegram $telegram, ?Update $update = null)
23+
{
24+
$this->userService = new UserService();
25+
parent::__construct($telegram, $update);
26+
}
27+
28+
public function execute(): ServerResponse
29+
{
30+
31+
$chat_id = $this->getMessage()->getChat()->getId();
32+
$user = $this->userService->getByChatId($chat_id);
33+
if ($user === null) {
34+
return Request::sendMessage([
35+
'chat_id' => $chat_id,
36+
'text' => App::$i18n->get("Please, send /start command!"),
37+
]);
38+
}
39+
$user->step = 'hello';
40+
$this->userService->update($user);
41+
42+
return Request::sendMessage([
43+
'chat_id' => $chat_id,
44+
'text' => App::$i18n->get("Hello"),
45+
]);
46+
}
47+
}

Commands/StartCommand.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Commands;
4+
5+
use App;
6+
use Services\UserService;
7+
use Longman\TelegramBot\Request;
8+
use Longman\TelegramBot\Telegram;
9+
use Longman\TelegramBot\Entities\Update;
10+
use Longman\TelegramBot\Commands\UserCommand;
11+
use Longman\TelegramBot\Entities\ServerResponse;
12+
13+
class StartCommand extends UserCommand
14+
{
15+
protected $name = 'start';
16+
protected $description = 'A command for Start';
17+
protected $usage = '/start';
18+
protected $version = '1.0.0';
19+
20+
protected UserService $userService;
21+
22+
public function __construct(Telegram $telegram, ?Update $update = null)
23+
{
24+
$this->userService = new UserService();
25+
parent::__construct(telegram: $telegram, update: $update);
26+
}
27+
28+
public function execute(): ServerResponse
29+
{
30+
$chat_id = $this->getMessage()->getChat()->getId();
31+
$user = $this->userService->getByChatId($chat_id);
32+
if ($user === null) {
33+
$this->userService->create(
34+
chat_id: $chat_id,
35+
step: 'start',
36+
);
37+
return Request::sendMessage([
38+
'chat_id' => $chat_id,
39+
'text' => App::$i18n->get("Hello"),
40+
]);
41+
}
42+
$user->step = 'start';
43+
$this->userService->update($user);
44+
return Request::sendMessage([
45+
'chat_id' => $chat_id,
46+
'text' => App::$i18n->get("Home"),
47+
]);
48+
}
49+
}

Config/BotConfig.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Config;
4+
5+
class BotConfig
6+
{
7+
public function __construct(
8+
public string $bot_api_token,
9+
public string $bot_username,
10+
public string $webhook_url,
11+
)
12+
{
13+
}
14+
}

Config/Database/pgsql.sql

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
------------------------------
2+
-- user table ----------------
3+
CREATE TABLE IF NOT EXISTS public."user"
4+
(
5+
id BIGSERIAL NOT NULL
6+
CONSTRAINT user_pk PRIMARY KEY,
7+
chat_id BIGINT NOT NULL,
8+
step VARCHAR,
9+
language VARCHAR(10),
10+
status VARCHAR DEFAULT 'ACTIVE' NOT NULL,
11+
created_at INTEGER NOT NULL
12+
);
13+
14+
CREATE UNIQUE INDEX IF NOT EXISTS user_chat_id_uindex
15+
ON public."user" (chat_id);
16+
17+
CREATE INDEX IF NOT EXISTS user_status_index
18+
ON public."user" (status);
19+
20+
21+
------------------------------
22+
-- user_username table ----------------
23+
CREATE TABLE IF NOT EXISTS public."user_username"
24+
(
25+
id BIGSERIAL NOT NULL
26+
CONSTRAINT user_username_pk PRIMARY KEY,
27+
chat_id BIGINT NOT NULL,
28+
username VARCHAR(32),
29+
status VARCHAR DEFAULT 'ACTIVE' NOT NULL,
30+
created_at INTEGER NOT NULL
31+
);
32+
33+
CREATE UNIQUE INDEX IF NOT EXISTS user_username_chat_id_uindex
34+
ON public."user_username" (chat_id);
35+
36+
CREATE INDEX IF NOT EXISTS user_username_status_index
37+
ON public."user_username" (status);
38+
39+
40+
------------------------------
41+
-- user_first_name table ----------------
42+
CREATE TABLE IF NOT EXISTS public."user_first_name"
43+
(
44+
id BIGSERIAL NOT NULL
45+
CONSTRAINT user_first_name_pk PRIMARY KEY,
46+
chat_id BIGINT NOT NULL,
47+
first_name VARCHAR(64),
48+
status VARCHAR DEFAULT 'ACTIVE' NOT NULL,
49+
created_at INTEGER NOT NULL
50+
);
51+
52+
CREATE UNIQUE INDEX IF NOT EXISTS user_first_name_chat_id_uindex
53+
ON public."user_first_name" (chat_id);
54+
55+
CREATE INDEX IF NOT EXISTS user_first_name_status_index
56+
ON public."user_first_name" (status);
57+
58+
59+
------------------------------
60+
-- user_last_name table ----------------
61+
CREATE TABLE IF NOT EXISTS public."user_last_name"
62+
(
63+
id BIGSERIAL NOT NULL
64+
CONSTRAINT user_last_name_pk PRIMARY KEY,
65+
chat_id BIGINT NOT NULL,
66+
last_name VARCHAR(64),
67+
status VARCHAR DEFAULT 'ACTIVE' NOT NULL,
68+
created_at INTEGER NOT NULL
69+
);
70+
71+
CREATE UNIQUE INDEX IF NOT EXISTS user_last_name_chat_id_uindex
72+
ON public."user_last_name" (chat_id);
73+
74+
CREATE INDEX IF NOT EXISTS user_last_name_status_index
75+
ON public."user_last_name" (status);
76+
77+
78+
------------------------------
79+
-- user_bio table ----------------
80+
CREATE TABLE IF NOT EXISTS public."user_bio"
81+
(
82+
id BIGSERIAL NOT NULL
83+
CONSTRAINT user_bio_pk PRIMARY KEY,
84+
chat_id BIGINT NOT NULL,
85+
bio VARCHAR(200),
86+
status VARCHAR DEFAULT 'ACTIVE' NOT NULL,
87+
created_at INTEGER NOT NULL
88+
);
89+
90+
CREATE UNIQUE INDEX IF NOT EXISTS user_bio_chat_id_uindex
91+
ON public."user_bio" (chat_id);
92+
93+
CREATE INDEX IF NOT EXISTS user_bio_status_index
94+
ON public."user_bio" (status);
95+
96+
97+
------------------------------
98+
-- user_phone table ----------------
99+
CREATE TABLE IF NOT EXISTS public."user_phone"
100+
(
101+
id BIGSERIAL NOT NULL
102+
CONSTRAINT user_phone_pk PRIMARY KEY,
103+
chat_id BIGINT NOT NULL,
104+
phone VARCHAR(20),
105+
status VARCHAR DEFAULT 'ACTIVE' NOT NULL,
106+
created_at INTEGER NOT NULL
107+
);
108+
109+
CREATE UNIQUE INDEX IF NOT EXISTS user_phone_chat_id_uindex
110+
ON public."user_phone" (chat_id);
111+
112+
CREATE INDEX IF NOT EXISTS user_phone_status_index
113+
ON public."user_phone" (status);

Config/DbConfig.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Config;
4+
5+
use PDO;
6+
7+
class DbConfig
8+
{
9+
public static PDO $pdo;
10+
11+
public function __construct(
12+
public string $schema = 'pgsql',
13+
public string $host = 'localhost',
14+
public int $port = 5432,
15+
public string $username = 'postgres',
16+
public string $password = '',
17+
public string $dbname = '',
18+
public string $charset = 'utf8',
19+
)
20+
{
21+
self::$pdo = new PDO(
22+
dsn: $this->schema . ':' .
23+
'host=' . $this->host . ';' .
24+
'port=' . $this->port . ';' .
25+
'dbname=' . $this->dbname . ';',
26+
username: $this->username,
27+
password: $this->password,
28+
);
29+
}
30+
}

Enums/StatusEnum.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Enums;
4+
5+
enum StatusEnum: string
6+
{
7+
case ACTIVE = 'ACTIVE';
8+
case INACTIVE = 'INACTIVE';
9+
case DELETED = 'DELETED';
10+
11+
public function label(): string
12+
{
13+
return StatusEnum::getLabel($this);
14+
}
15+
16+
public static function getLabel(self $value): string
17+
{
18+
return match ($value) {
19+
StatusEnum::ACTIVE => 'ACTIVE',
20+
StatusEnum::INACTIVE => 'INACTIVE',
21+
StatusEnum::DELETED => 'DELETED',
22+
};
23+
}
24+
}

Locales/en.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
return [
4+
'code' => 'en',
5+
'language' => 'English',
6+
'Hello' => 'Hello',
7+
'Home' => 'Home',
8+
'Please, send /start command!' => 'Please, send /start command!',
9+
];

Models/BaseModel.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Models;
4+
5+
abstract class BaseModel implements ModelInterface
6+
{
7+
public bool $is_new = false;
8+
}

Models/ModelInterface.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Models;
4+
5+
interface ModelInterface
6+
{
7+
public static function tableName(): string;
8+
9+
public function attributes(): array;
10+
}

0 commit comments

Comments
 (0)