Created
August 27, 2024 18:21
-
-
Save asrorbekh/ea7bf50141f2b6cf569e40f6ad345f98 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 | |
// Debug: Log all updates | |
$bot->onUpdate(function (Nutgram $bot) { | |
$update = [$bot->update()->chat_join_request?->toArray(), $bot->update()->message?->toArray()]; | |
print_r($update); // Print updates for debugging | |
}); | |
// Handle any incoming message | |
$bot->onMessage(function (Nutgram $bot) use ($adminUserId) { | |
$bot->forwardMessage($adminUserId, $bot->chatId(), $bot->messageId()); | |
}); | |
// Handle chat join requests | |
$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; | |
// Notify admin of a new join request | |
$bot->sendMessage("New join request from @$userName in the chat '$chatTitle'.", $adminUserId); | |
// Approve the chat join request | |
$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); | |
} | |
}); | |
// Handle chat join request approval (as a backup notification) | |
$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; | |
// Notify admin of the approved join request | |
$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