From 2650cc17510d96575c9c2b8688ccce9c9f83ccb6 Mon Sep 17 00:00:00 2001 From: Abbas mkhzomi Date: Fri, 8 Apr 2022 19:52:25 +0430 Subject: [PATCH 1/6] seeders fixed --- db/seeds/LevelSeeder.php | 6 +----- db/seeds/UserSeeder.php | 5 +---- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/db/seeds/LevelSeeder.php b/db/seeds/LevelSeeder.php index bd39eca..8de04f0 100644 --- a/db/seeds/LevelSeeder.php +++ b/db/seeds/LevelSeeder.php @@ -14,17 +14,13 @@ class LevelSeeder extends AbstractSeed */ public function run() { - $level = $this->fetchRow("SELECT * FROM levels ORDER BY orders DESC") ?? ["orders" => 0]; - $faker = Faker\Factory::create(); $data = []; - $orders = $level["orders"]; for ($i = 0; $i < 100; $i++) { $data[] = [ "quest" => $faker->text(20), "answer" => $faker->text(20), - "orders" => ++$orders, - "difficulty" => round($orders / 10, 0), + "difficulty" => rand(0, 100), ]; } diff --git a/db/seeds/UserSeeder.php b/db/seeds/UserSeeder.php index 6632f37..f63a9be 100644 --- a/db/seeds/UserSeeder.php +++ b/db/seeds/UserSeeder.php @@ -18,17 +18,14 @@ public function getDependencies() */ public function run() { - $level = $this->fetchRow("SELECT * FROM levels ORDER BY orders DESC") ?? ["orders" => 0]; - $faker = Faker\Factory::create(); $data = []; - $level = $level["orders"]; for ($i = 0; $i < 10; $i++) { $data[] = [ "chat_id" => $faker->text(), "credit" => rand(50, 500), - "level" => rand(0, $level), + "level_id" => rand(0, 100), "invite_key" => uniqid(), ]; } From 7945ea9d9ace8f1facbbf2196d4c7506af9fd80f Mon Sep 17 00:00:00 2001 From: Abbas mkhzomi Date: Fri, 8 Apr 2022 19:55:02 +0430 Subject: [PATCH 2/6] run.bat added to run the bot in windows --- run.bat | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 run.bat diff --git a/run.bat b/run.bat new file mode 100644 index 0000000..acbcdaf --- /dev/null +++ b/run.bat @@ -0,0 +1,3 @@ +:start +php index.php +goto start \ No newline at end of file From 6c720bf1a9d352a75b9cf335bfa21d92980d43c4 Mon Sep 17 00:00:00 2001 From: Abbas mkhzomi Date: Fri, 8 Apr 2022 21:04:57 +0430 Subject: [PATCH 3/6] inPutHelper refactored --- app/Helper/InputHelper.php | 160 +++++++++++-------------------------- 1 file changed, 47 insertions(+), 113 deletions(-) diff --git a/app/Helper/InputHelper.php b/app/Helper/InputHelper.php index f918e2e..7861d18 100644 --- a/app/Helper/InputHelper.php +++ b/app/Helper/InputHelper.php @@ -14,23 +14,28 @@ use App\Controller\Command\ChatIdCommandController; use App\Controller\Command\StartCommandController; use App\Controller\Game\GameController; -use App\Controller\Keyboard\AboutKeyboardController; -use App\Controller\Keyboard\BuyCreditKeyboardController; -use App\Controller\Keyboard\ContactKeyboardController; -use App\Controller\Keyboard\FreeCreditKeyboardController; -use App\Controller\Keyboard\GameContinueKeyboardController; -use App\Controller\Keyboard\GameStartKeyboardController; -use App\Controller\Keyboard\HintKeyboardController; -use App\Controller\Keyboard\LeaderBoardKeyboardController; -use App\Controller\Keyboard\SupportKeyboardController; -use App\Controller\Keyboard\YourCreditKeyboardController; -use App\Controller\Keyboard\YoutubeKeyboardController; use App\Model\User; class InputHelper { private $update; + private array $replays = [ + 'سکه رایگان' => 'FreeCreditKeyboardController', + 'برترین ها' => 'LeaderBoardKeyboardController', + 'ادامه بازی' => 'GameContinueKeyboardController', + 'خرید سکه' => 'BuyCreditKeyboardController', + 'آموزش ساخت بازی' => 'YoutubeKeyboardController', + 'شروع بازی' => 'GameStartKeyboardController', + 'درباره ما' => 'AboutKeyboardController', + 'تماس با ما' => 'ContactKeyboardController', + 'کمک می‌خوای' => 'HintKeyboardController', + 'حمایت از ما' => 'SupportKeyboardController', + 'سکه‌های شما' => 'YourCreditKeyboardController', + ]; + + private string $controllersNs = 'App\Controller\Keyboard'; + public function __construct(array $update) { // sanitize frequent access variable @@ -51,25 +56,22 @@ public function __invoke() public function run() { - switch ($this->update["message"]["chat"]["type"]) { - case "private": - return $this->private(); - break; - case "group": - case "supergroup": - return $this->group(); - break; - } + return match ($this->update["message"]["chat"]["type"]) { + "private" => $this->private(), + "group", "supergroup" => $this->group(), + }; } + private function private() { - if (!User::get_first("WHERE chat_id=:chat_id", [":chat_id" => $this->update["message"]["chat"]["id"]])) { + if ( ! User::get_first("WHERE chat_id=:chat_id", [":chat_id" => $this->update["message"]["chat"]["id"]])) { return (new StartCommandController($this->update))(); } if (isset($this->update["message"]["text"])) { return $this->text(); } + return null; } @@ -111,74 +113,27 @@ private function text() if ($admin = $this->admin()) { return $admin; } + return $this->game(); } private function native_commands() { - switch ($this->update["message"]["text"]) { - case "/start": - return (new StartCommandController($this->update))(); - break; - case "/chat_id": - return (new ChatIdCommandController($this->update))(); - break; - - default: - return null; - break; - } + return match ($this->update["message"]["text"]) { + "/start" => (new StartCommandController($this->update))(), + "/chat_id" => (new ChatIdCommandController($this->update))(), + default => null, + }; } private function reply_keyboard() { - switch ($this->update["message"]["text"]) { - case "💸 سکه رایگان": - return (new FreeCreditKeyboardController($this->update))(); - break; - case "🥇 برترین ها": - return (new LeaderBoardKeyboardController($this->update))(); - break; - case "❣️ ادامه بازی": - return (new GameContinueKeyboardController($this->update))(); - break; - case "💳 خرید سکه": - return (new BuyCreditKeyboardController($this->update))(); - break; - // case "💵 سکه‌های شما‌ : ": - // return (new YourCreditKeyboardController($this->update))(); - // break; - case "آموزش ساخت بازی 🕹": - return (new YoutubeKeyboardController($this->update))(); - break; - case "شروع بازی": - return (new GameStartKeyboardController($this->update))(); - break; - case "ادامه بازی": - return (new GameStartKeyboardController($this->update))(); - break; - case "🖥 درباره ما": - return (new AboutKeyboardController($this->update))(); - break; - case "📞 تماس با ما": - return (new ContactKeyboardController($this->update))(); - break; - // case "🪄 کمک می‌خوای ؟": - // return (new HintKeyboardController($this->update))(); - // break; - case "😍 حمایت از ما": - return (new SupportKeyboardController($this->update))(); - break; - - default: - break; - } + foreach ($this->replays as $text => $class) { + if (str_contains($this->update["message"]["text"], $text)) { + $class = $this->controllersNs.'\\'.$class; - if (stripos($this->update["message"]["text"], "کمک می‌خوای")) { - return (new HintKeyboardController($this->update))(); - } - if (stripos($this->update["message"]["text"], "سکه‌های شما‌")) { - return (new YourCreditKeyboardController($this->update))(); + return (new $class($this->update))(); + } } return false; @@ -197,40 +152,19 @@ private function admin() return false; } $command = substr($this->update["message"]["text"], 0, 10); - switch ($command) { - case "!help": - return (new HelpController($this->update))(); - break; - case "!aNewLevel": - return (new AddLevelController($this->update))(); - break; - case "!aNewHints": - return (new AddHintController($this->update))(); - break; - case "!aOuttexts": - return (new AddOutputMessageController($this->update))(); - break; - case "!listLevel": - return (new ListLevelsController($this->update))(); - break; - case "!listHints": - return (new ListHintsController($this->update))(); - break; - case "!listOMesg": - return (new ListOutputMessagesController($this->update))(); - break; - case "!getUserId": - return (new GetUserByChatIdController($this->update))(); - break; - case "!newAds": - return (new AddNewAdvertiseController($this->update))(); - break; - - default: - return false; - break; - } - return false; + + return match ($command) { + "!help" => (new HelpController($this->update))(), + "!aNewLevel" => (new AddLevelController($this->update))(), + "!aNewHints" => (new AddHintController($this->update))(), + "!aOuttexts" => (new AddOutputMessageController($this->update))(), + "!listLevel" => (new ListLevelsController($this->update))(), + "!listHints" => (new ListHintsController($this->update))(), + "!listOMesg" => (new ListOutputMessagesController($this->update))(), + "!getUserId" => (new GetUserByChatIdController($this->update))(), + "!newAds" => (new AddNewAdvertiseController($this->update))(), + default => false, + }; } private function game() From 84bc6935c548c47a7733287797b2dcb2547fceaf Mon Sep 17 00:00:00 2001 From: Abbas mkhzomi Date: Fri, 8 Apr 2022 21:45:00 +0430 Subject: [PATCH 4/6] profile and back controllers created --- .../Keyboard/BackKeyboardController.php | 18 ++++++++++++++++ .../Keyboard/ProfileKeyboardController.php | 18 ++++++++++++++++ app/Enums/OutputMessageEnum.php | 4 +++- app/Helper/InputHelper.php | 2 ++ app/Helper/KeyboardMakerHepler.php | 21 ++++++++++++++++++- app/Helper/OutputHelper.php | 5 +++-- 6 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 app/Controller/Keyboard/BackKeyboardController.php create mode 100644 app/Controller/Keyboard/ProfileKeyboardController.php diff --git a/app/Controller/Keyboard/BackKeyboardController.php b/app/Controller/Keyboard/BackKeyboardController.php new file mode 100644 index 0000000..72036af --- /dev/null +++ b/app/Controller/Keyboard/BackKeyboardController.php @@ -0,0 +1,18 @@ +chat_id, OutputMessageEnum::BACK); + + return true; + } +} diff --git a/app/Controller/Keyboard/ProfileKeyboardController.php b/app/Controller/Keyboard/ProfileKeyboardController.php new file mode 100644 index 0000000..9ca62ac --- /dev/null +++ b/app/Controller/Keyboard/ProfileKeyboardController.php @@ -0,0 +1,18 @@ +chat_id, OutputMessageEnum::PROFILE); + + return true; + } +} diff --git a/app/Enums/OutputMessageEnum.php b/app/Enums/OutputMessageEnum.php index d051cb6..fbb592e 100644 --- a/app/Enums/OutputMessageEnum.php +++ b/app/Enums/OutputMessageEnum.php @@ -10,13 +10,15 @@ enum OutputMessageEnum: int case LEVEL = 4; case FINISH_GAME = 5; - // about keyboards + // Main keyboards case START_GAME = 20; case CONTINUE = 21; case ABOUT = 22; case CONTACT = 23; case SUPPORT = 25; case YOUTUBE = 26; + case PROFILE = 27; + case BACK = 28; // Commands case START_COMMAND_GUEST = 30; diff --git a/app/Helper/InputHelper.php b/app/Helper/InputHelper.php index 7861d18..ff2992a 100644 --- a/app/Helper/InputHelper.php +++ b/app/Helper/InputHelper.php @@ -28,10 +28,12 @@ class InputHelper 'آموزش ساخت بازی' => 'YoutubeKeyboardController', 'شروع بازی' => 'GameStartKeyboardController', 'درباره ما' => 'AboutKeyboardController', + 'پروفایل' => 'ProfileKeyboardController', 'تماس با ما' => 'ContactKeyboardController', 'کمک می‌خوای' => 'HintKeyboardController', 'حمایت از ما' => 'SupportKeyboardController', 'سکه‌های شما' => 'YourCreditKeyboardController', + 'بازگشت' => 'BackKeyboardController', ]; private string $controllersNs = 'App\Controller\Keyboard'; diff --git a/app/Helper/KeyboardMakerHepler.php b/app/Helper/KeyboardMakerHepler.php index 9672669..68439af 100644 --- a/app/Helper/KeyboardMakerHepler.php +++ b/app/Helper/KeyboardMakerHepler.php @@ -18,7 +18,9 @@ class KeyboardMakerHepler "contact" => "📞 تماس با ما", "hint" => "🪄 کمک می‌خوای ؟", "support" => "😍 حمایت از ما", - "profile" => "💀 پروفایل", + "profile" => "🖼 پروفایل", + "change" => "تغییر پروفایل 📝", + "back" => "بازگشت 🔙", ]; public static function by_type(OutputMessageEnum $type) @@ -117,8 +119,25 @@ public static function no_mission() true ); } + public static function FINISH_GAME() { return self::no_mission(); } + + public static function profile(): array + { + return TelegramHelper::make_keyboard( + [ + [["text" => self::$texts["change"]], ["text" => self::$texts["back"]]], + ], + true, + true + ); + } + + public static function back(): array + { + return self::default_keyboard(); + } } diff --git a/app/Helper/OutputHelper.php b/app/Helper/OutputHelper.php index e9ee094..17c3933 100644 --- a/app/Helper/OutputHelper.php +++ b/app/Helper/OutputHelper.php @@ -24,10 +24,11 @@ public static function by_type(string $_chat_id, OutputMessageEnum $_type, bool $message = OutputMessage::by_type($_type); } if (empty($message)) { - return false; + $text = $_type->name; + } else { + $text = $message->text; } - $text = $message->text; if (!empty($data)) { $text = self::fill_data($message->text, $data); From 15e1037a0c35145e038ee225a46acb22b20f8edf Mon Sep 17 00:00:00 2001 From: Abbas mkhzomi Date: Sat, 9 Apr 2022 02:47:25 +0430 Subject: [PATCH 5/6] name and image added to user --- app/Helper/TelegramHelper.php | 14 ++++++++++ app/Model/User.php | 8 ++++++ .../20220408211529_add_name_on_users.php | 27 +++++++++++++++++++ db/seeds/UserSeeder.php | 1 + 4 files changed, 50 insertions(+) create mode 100644 db/migrations/20220408211529_add_name_on_users.php diff --git a/app/Helper/TelegramHelper.php b/app/Helper/TelegramHelper.php index 06c1a25..8bdc62b 100644 --- a/app/Helper/TelegramHelper.php +++ b/app/Helper/TelegramHelper.php @@ -154,4 +154,18 @@ public static function forward(string $_chat_id, string $_from_chat_id, string $ return self::execute("forwardMessage", $query); } + + /** + * @param string $_chat_id + * + * @return false|string + */ + public static function get_first_name(string $_chat_id): bool|string + { + $user = self::get_user($_chat_id); + if (!isset($user['result']['first_name'])) { + return false; + } + return $user['result']['first_name']; + } } diff --git a/app/Model/User.php b/app/Model/User.php index 06aff87..97d59b2 100644 --- a/app/Model/User.php +++ b/app/Model/User.php @@ -3,6 +3,7 @@ use App\Enums\OutputMessageEnum; use App\Helper\OutputHelper; +use App\Helper\TelegramHelper; use PDO; final class User extends Model @@ -10,6 +11,8 @@ final class User extends Model protected static $table = "users"; protected static $fields = [ "id" => PDO::PARAM_INT, + "name" => PDO::PARAM_STR, + "image_id" => PDO::PARAM_STR, "chat_id" => PDO::PARAM_STR, "credit" => PDO::PARAM_INT, "level_id" => PDO::PARAM_INT, @@ -20,6 +23,10 @@ final class User extends Model public function __construct() { + if ($this->name == 'unknown') { + $this->name = TelegramHelper::get_first_name($this->chat_id); + $this->save(); + } } public function level() @@ -34,6 +41,7 @@ public static function get_or_create(string $_chat_id): self if (!$user || empty($user)) { $user = self::create([ "chat_id" => $_chat_id, + "name" => TelegramHelper::get_first_name($_chat_id), "credit" => $_ENV["DEFAULT_CREDIT"], "level_id" => $_ENV["START_LEVEL_ID"], ]); diff --git a/db/migrations/20220408211529_add_name_on_users.php b/db/migrations/20220408211529_add_name_on_users.php new file mode 100644 index 0000000..88e47df --- /dev/null +++ b/db/migrations/20220408211529_add_name_on_users.php @@ -0,0 +1,27 @@ +table('users'); + $table->addColumn('name', 'string') + ->addColumn('image_id', 'string', ['null' => true]) + ->update(); + } +} diff --git a/db/seeds/UserSeeder.php b/db/seeds/UserSeeder.php index f63a9be..77a1b4d 100644 --- a/db/seeds/UserSeeder.php +++ b/db/seeds/UserSeeder.php @@ -24,6 +24,7 @@ public function run() for ($i = 0; $i < 10; $i++) { $data[] = [ "chat_id" => $faker->text(), + "name" => $faker->name(), "credit" => rand(50, 500), "level_id" => rand(0, 100), "invite_key" => uniqid(), From 932695378e1261f1b25bcd2a41666878b01cfb22 Mon Sep 17 00:00:00 2001 From: Abbas mkhzomi Date: Sat, 9 Apr 2022 03:40:17 +0430 Subject: [PATCH 6/6] profile message added --- .../Keyboard/ProfileKeyboardController.php | 3 +- app/Helper/KeyboardMakerHepler.php | 2 +- app/Helper/OutputHelper.php | 17 ++++++++ app/Helper/TelegramHelper.php | 40 +++++++++++++++++++ app/Model/User.php | 3 +- 5 files changed, 60 insertions(+), 5 deletions(-) diff --git a/app/Controller/Keyboard/ProfileKeyboardController.php b/app/Controller/Keyboard/ProfileKeyboardController.php index 9ca62ac..87babef 100644 --- a/app/Controller/Keyboard/ProfileKeyboardController.php +++ b/app/Controller/Keyboard/ProfileKeyboardController.php @@ -10,8 +10,7 @@ class ProfileKeyboardController extends Controller { public function __invoke(): bool { - // Start Message - OutputHelper::by_type($this->chat_id, OutputMessageEnum::PROFILE); + OutputHelper::profile($this->chat_id , $this->user); return true; } diff --git a/app/Helper/KeyboardMakerHepler.php b/app/Helper/KeyboardMakerHepler.php index 68439af..f5154d7 100644 --- a/app/Helper/KeyboardMakerHepler.php +++ b/app/Helper/KeyboardMakerHepler.php @@ -18,7 +18,7 @@ class KeyboardMakerHepler "contact" => "📞 تماس با ما", "hint" => "🪄 کمک می‌خوای ؟", "support" => "😍 حمایت از ما", - "profile" => "🖼 پروفایل", + "profile" => "👤 پروفایل", "change" => "تغییر پروفایل 📝", "back" => "بازگشت 🔙", ]; diff --git a/app/Helper/OutputHelper.php b/app/Helper/OutputHelper.php index 17c3933..5dce877 100644 --- a/app/Helper/OutputHelper.php +++ b/app/Helper/OutputHelper.php @@ -101,4 +101,21 @@ public static function low_credit(string $_chat_id) { self::by_type($_chat_id, OutputMessageEnum::LOW_CREDIT); } + + public static function profile(string $_chat_id , User $user) + { + $image = $user->image_id ?? TelegramHelper::get_user_profile_photo($_chat_id); + $keyboard = KeyboardMakerHepler::by_type(OutputMessageEnum::PROFILE); + $now = new \DateTime(); + $from = new \DateTime($user->created_at); + $diff = $now->diff($from); + $message = <<name} +تعداد سکه : {$user->credit} +سطح : {$user->level()->difficulty} +عضو از : {$diff->days} روز قبل +------ +EOT; + TelegramHelper::send_photo($image, $_chat_id, $message, $keyboard); + } } diff --git a/app/Helper/TelegramHelper.php b/app/Helper/TelegramHelper.php index 8bdc62b..1af5224 100644 --- a/app/Helper/TelegramHelper.php +++ b/app/Helper/TelegramHelper.php @@ -168,4 +168,44 @@ public static function get_first_name(string $_chat_id): bool|string } return $user['result']['first_name']; } + + /** + * @param string $_chat_id + * + * @return bool|string + * @throws \Exception + */ + public static function get_user_profile_photo(string $_chat_id): bool|string + { + $user = self::get_user($_chat_id); + if (!isset($user['result']['id'])) { + return false; + } + return self::execute('getUserProfilePhotos', [ + 'user_id' => $user['result']['id'], + 'limit' => 1, + ])['result']['photos'][0][0]['file_id']; + } + + /** + * @param string $file_id + * @param string $_chat_id + * @param string|null $caption + * @param array $_keyboard + * + * @return bool|array + * @throws \Exception + */ + public static function send_photo(string $file_id, string $_chat_id, string $caption = null, array $_keyboard = []): bool|array + { + $parameters = [ + 'photo' => $file_id, + 'chat_id' => $_chat_id, + 'caption' => $caption, + ]; + if (!empty($_keyboard)) { + $parameters["reply_markup"] = $_keyboard; + } + return self::execute('sendPhoto', $parameters); + } } diff --git a/app/Model/User.php b/app/Model/User.php index 97d59b2..776c5bc 100644 --- a/app/Model/User.php +++ b/app/Model/User.php @@ -31,8 +31,7 @@ public function __construct() public function level() { - $level = Level::get_first("WHERE id=:id", ["id" => $this->level_id]); - return $level; + return Level::get_first("WHERE id=:id", ["id" => $this->level_id]); } public static function get_or_create(string $_chat_id): self