-
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.
Merge pull request #2 from yaleksandr89/bot/translation-bot
Реализован бот переводчик v1
- Loading branch information
Showing
10 changed files
with
662 additions
and
42 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
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 |
---|---|---|
@@ -1,16 +1,18 @@ | ||
{ | ||
"require-dev": { | ||
"symfony/var-dumper": "^6.0" | ||
}, | ||
"require": { | ||
"guzzlehttp/guzzle": "^7.4", | ||
"irazasyed/telegram-bot-sdk": "^3.4" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"SimpleBot\\": "simple-bot/", | ||
"TbaPhpSdk\\": "tba-php-sdk/", | ||
"YaTranslationBot\\": "translation-bot/" | ||
} | ||
"require-dev": { | ||
"symfony/var-dumper": "^6.0" | ||
}, | ||
"require": { | ||
"guzzlehttp/guzzle": "^7.4", | ||
"irazasyed/telegram-bot-sdk": "^3.4", | ||
"dejurin/php-google-translate-for-free": "^1.0", | ||
"ext-pdo": "*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"SimpleBot\\": "simple-bot/", | ||
"TbaPhpSdk\\": "tba-php-sdk/", | ||
"YaTranslationBot\\": "translation-bot-v1/" | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,138 @@ | ||
<?php | ||
|
||
namespace YaTranslationBot; | ||
|
||
use PDO; | ||
use PDOException; | ||
use PDOStatement; | ||
|
||
final class DB | ||
{ | ||
private ?PDO $connection = null; | ||
|
||
private PDOStatement $stmt; | ||
|
||
private static ?DB $instance = null; | ||
|
||
private function __construct() | ||
{ | ||
} | ||
|
||
private function __clone() | ||
{ | ||
} | ||
|
||
private function __wakeup() | ||
{ | ||
} | ||
|
||
public static function getInstance(): DB | ||
{ | ||
return self::$instance ?? (self::$instance = new self()); | ||
} | ||
|
||
public function getConnection(array $dbConfig): DB | ||
{ | ||
if ($this->connection instanceof PDO) { | ||
return $this; | ||
} | ||
|
||
$dsn = "{$dbConfig['type']}:host={$dbConfig['host']};port={$dbConfig['port']};dbname={$dbConfig['name']}"; | ||
|
||
try { | ||
$this->connection = new PDO($dsn, $dbConfig['user'], $dbConfig['password'], $dbConfig['options']); | ||
} catch (PDOException $exception) { | ||
file_put_contents( | ||
__DIR__ . '/try_catch_db_logs.txt', | ||
date('d.m.Y H:i:s') . PHP_EOL . print_r($exception, true), | ||
FILE_APPEND | ||
); | ||
die; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function query(string $query, array $params = []): false|DB | ||
{ | ||
try { | ||
$this->stmt = $this->connection->prepare($query); | ||
$this->stmt->execute($params); | ||
} catch (PDOException $exception) { | ||
file_put_contents( | ||
__DIR__ . '/try_catch_db_logs.txt', | ||
date('d.m.Y H:i:s') . PHP_EOL . print_r($exception, true), | ||
FILE_APPEND | ||
); | ||
die; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function findAll(): array | ||
{ | ||
return $this | ||
->stmt | ||
->fetchAll(); | ||
} | ||
|
||
public function find(): ?array | ||
{ | ||
$result = $this | ||
->stmt | ||
->fetch(); | ||
|
||
if (!$result) { | ||
return null; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public function getChatId(int $chatId): ?array | ||
{ | ||
return $this | ||
->query('SELECT * FROM chat WHERE chat_id=:chatId', ['chatId' => $chatId]) | ||
->find(); | ||
} | ||
|
||
public function setChat( | ||
int $chatId, | ||
string $firstName, | ||
string $lastName, | ||
string $username, | ||
string $date, | ||
string $lang | ||
): void { | ||
$this | ||
->query( | ||
'INSERT INTO chat | ||
(chat_id, first_name, last_name, username, date, lang) | ||
VALUES (:chatId, :firstName, :lastName, :username, :date, :lang)', | ||
[ | ||
'chatId' => $chatId, | ||
'firstName' => $firstName, | ||
'lastName' => $lastName, | ||
'username' => $username, | ||
'date' => $date, | ||
'lang' => $lang | ||
] | ||
); | ||
} | ||
|
||
public function updateChat(int $chatId, string $lang, string $date): void | ||
{ | ||
$this | ||
->query( | ||
'UPDATE chat | ||
SET lang=:lang, date=:date | ||
WHERE chat_id=:chatId', | ||
[ | ||
'chatId' => $chatId, | ||
'lang' => $lang, | ||
'date' => $date | ||
] | ||
); | ||
} | ||
} |
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,18 @@ | ||
Создание таблицы `chat` в БД `telegram_bot_translation`: | ||
|
||
```sql | ||
CREATE TABLE `telegram_bot_translation`.`chat` | ||
( | ||
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, | ||
`chat_id` BIGINT UNSIGNED NOT NULL, | ||
`first_name` VARCHAR(510) NOT NULL, | ||
`last_name` VARCHAR(510) NOT NULL, | ||
`username` VARCHAR(510) NOT NULL, | ||
`date` DATETIME NOT NULL, | ||
`lang` VARCHAR(50) NOT NULL, | ||
|
||
PRIMARY KEY (`id`), | ||
UNIQUE (`chat_id`) | ||
) | ||
ENGINE = InnoDB; | ||
``` |
Oops, something went wrong.