Created
August 27, 2024 18:23
-
-
Save asrorbekh/22b06262db7fd8b9864df542cefeb15f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
require 'vendor/autoload.php'; | |
use Dotenv\Dotenv; | |
use SergiX44\Nutgram\Nutgram; | |
$dotenv = Dotenv::createImmutable(__DIR__); | |
$dotenv->load(); | |
try { | |
$bot = new Nutgram($_ENV['TELEGRAM_BOT_TOKEN']); | |
$adminUserId = $_ENV['ADMIN_ID']; // Admin user ID to receive notifications | |
$bot->onMessage(function (Nutgram $bot) use ($adminUserId) { | |
$bot->forwardMessage($adminUserId, $bot->chatId(), $bot->messageId()); | |
}); | |
$bot->onChatJoinRequest(function (Nutgram $bot) use ($adminUserId) { | |
$chatJoinRequest = $bot->update()->chat_join_request; | |
$userName = $chatJoinRequest->from->username ?? 'Unknown'; | |
$chatTitle = $chatJoinRequest->chat->title; | |
$chatId = $chatJoinRequest->chat->id; | |
$userId = $chatJoinRequest->from->id; | |
$bot->sendMessage("New join request from @$userName in the chat '$chatTitle'.", $adminUserId); | |
$success = $bot->approveChatJoinRequest($chatId, $userId); | |
if ($success) { | |
$bot->sendMessage("User @$userName has been approved to join the chat '$chatTitle'.", $adminUserId); | |
} else { | |
$bot->sendMessage("Failed to approve @$userName's request to join the chat '$chatTitle'.", $adminUserId); | |
} | |
}); | |
$bot->onChatMember(function (Nutgram $bot) use ($adminUserId) { | |
$chatMember = $bot->update()->chat_member; | |
if ($chatMember->new_chat_member->status === 'member') { | |
$userName = $chatMember->new_chat_member->user->username ?? 'Unknown'; | |
$chatTitle = $chatMember->chat->title; | |
$bot->sendMessage("User @$userName has been approved to join the chat '$chatTitle'.", $adminUserId); | |
} | |
}); | |
$bot->run(); | |
} catch (\Exception|\Psr\Container\ContainerExceptionInterface $e) { | |
print_r([ | |
'message' => $e->getMessage(), | |
'line' => $e->getLine(), | |
'file' => $e->getFile(), | |
'trace' => $e->getTrace(), | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment