Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmuminov committed Aug 23, 2022
0 parents commit 9939c3b
Show file tree
Hide file tree
Showing 20 changed files with 734 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vendor
.idea
composer.lock
*.local.*
36 changes: 36 additions & 0 deletions App.php
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);
}
}
47 changes: 47 additions & 0 deletions Commands/HelloCommand.php
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"),
]);
}
}
49 changes: 49 additions & 0 deletions Commands/StartCommand.php
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"),
]);
}
}
14 changes: 14 additions & 0 deletions Config/BotConfig.php
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,
)
{
}
}
113 changes: 113 additions & 0 deletions Config/Database/pgsql.sql
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);
30 changes: 30 additions & 0 deletions Config/DbConfig.php
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,
);
}
}
24 changes: 24 additions & 0 deletions Enums/StatusEnum.php
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',
};
}
}
9 changes: 9 additions & 0 deletions Locales/en.php
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!',
];
8 changes: 8 additions & 0 deletions Models/BaseModel.php
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;
}
10 changes: 10 additions & 0 deletions Models/ModelInterface.php
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;
}
Loading

0 comments on commit 9939c3b

Please sign in to comment.