WHAT error in this - <?php // Your bot token $botToken =

'692779Et_e91Q97jP5gbjDhD0apKU30I';

// Your webhook URL
$webhookUrl = 'https://ttrsop.live/Autokicker.php';

// Set the webhook
$apiUrl = "https://api.telegram.org/bot$botToken/setWebhook";
$data = array('url' => $webhookUrl);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($apiUrl, false, $context);

if ($result === false) {
die('Failed to set up the webhook.');
} else {
echo 'Webhook set up successfully.';
}

// Handle incoming updates
$input = file_get_contents("php://input");
$update = json_decode($input, true);

if (isset($update['message']) && isset($update['message']['chat']['id'])) {
$chatId = $update['message']['chat']['id'];

if (isset($update['message']['new_chat_members'])) {
// Handle new member join
$newMember = $update['message']['new_chat_members'][0];
$newMemberId = $newMember['id'];

// Send a welcome message to the new member
$welcomeMessage = "Welcome to the group! User ID: $newMemberId";
sendMessage($botToken, $chatId, $welcomeMessage);
} elseif (isset($update['message']['left_chat_member'])) {
// Handle member leave
$leftMember = $update['message']['left_chat_member'];
$leftMemberId = $leftMember['id'];

// Send a farewell message to the leaving member
$farewellMessage = "Goodbye! User ID: $leftMemberId";
sendMessage($botToken, $chatId, $farewellMessage);
}
}

function sendMessage($token, $chatId, $message) {
$apiUrl = "https://api.telegram.org/bot$token/sendMessage";
$data = array('chat_id' => $chatId, 'text' => $message);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($apiUrl, false, $context);

if ($result === false) {
echo 'Failed to send message.';
}
}

20 ответов

84 просмотра
Trusted Seller- Автор вопроса

s0m31 why this not work

add 'allowed_updates' => '["message", "chat_member"]' to data that goes to setWebhook

and better remove leftovers of your tokens

Trusted Seller- Автор вопроса

Now fine - <?php // Your bot token $botToken = 'YOUR_BOT_TOKEN'; // Your webhook URL $webhookUrl = 'https://your-webhook-url/your-webhook-endpoint'; // Set the webhook with 'allowed_updates' parameter $apiUrl = "https://api.telegram.org/bot$botToken/setWebhook"; $data = array( 'url' => $webhookUrl, 'allowed_updates' => '["message", "chat_member"]' ); $options = array( 'http' => array( 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data), ), ); $context = stream_context_create($options); $result = file_get_contents($apiUrl, false, $context); if ($result === false) { die('Failed to set up the webhook.'); } else { echo 'Webhook set up successfully.'; } // Handle incoming updates $input = file_get_contents("php://input"); $update = json_decode($input, true); if (isset($update['message']) && isset($update['message']['chat']['id'])) { $chatId = $update['message']['chat']['id']; if (isset($update['message']['new_chat_members'])) { // Handle new member join $newMember = $update['message']['new_chat_members'][0]; $newMemberId = $newMember['id']; // Send a welcome message to the new member $welcomeMessage = "Welcome to the group! User ID: $newMemberId"; sendMessage($botToken, $chatId, $welcomeMessage); } elseif (isset($update['message']['left_chat_member'])) { // Handle member leave $leftMember = $update['message']['left_chat_member']; $leftMemberId = $leftMember['id']; // Send a farewell message to the leaving member $farewellMessage = "Goodbye! User ID: $leftMemberId"; sendMessage($botToken, $chatId, $farewellMessage); } } function sendMessage($token, $chatId, $message) { $apiUrl = "https://api.telegram.org/bot$token/sendMessage"; $data = array('chat_id' => $chatId, 'text' => $message); $options = array( 'http' => array( 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data), ), ); $context = stream_context_create($options); $result = file_get_contents($apiUrl, false, $context); if ($result === false) { echo 'Failed to send message.'; } }

Trusted Seller- Автор вопроса

NOT WORK

Trusted Seller
Now fine - <?php // Your bot token $botToken = 'YO...

new_chat_members and left_chat_member are only service messages. You should use chat_member update to catch them all

Trusted Seller
NOT WORK

try to debug your code yourself. chat_member updates are known to be buggy. Maybe mine is just a service message

Trusted Seller
What i add on offset

do not add this parameter. Its for getupdates only

Trusted Seller- Автор вопроса
Erry
new_chat_members and left_chat_member are only ser...

Now - <?php // Your bot token $botToken = 'YOUR_BOT_TOKEN'; // Your webhook URL $webhookUrl = 'https://your-webhook-url/your-webhook-endpoint'; // Set the webhook with 'allowed_updates' parameter $apiUrl = "https://api.telegram.org/bot$botToken/setWebhook"; $data = array( 'url' => $webhookUrl, 'allowed_updates' => '["chat_member"]' ); $options = array( 'http' => array( 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data), ), ); $context = stream_context_create($options); $result = file_get_contents($apiUrl, false, $context); if ($result === false) { die('Failed to set up the webhook.'); } else { echo 'Webhook set up successfully.'; } // Handle incoming updates $input = file_get_contents("php://input"); $update = json_decode($input, true); if (isset($update['message']) && isset($update['message']['chat']['id'])) { $chatId = $update['message']['chat']['id']; if (isset($update['message']['chat_member'])) { // Handle chat_member update $chatMember = $update['message']['chat_member']; $chatMemberId = $chatMember['user']['id']; if ($chatMember['new_chat_member']) { // New member joined $welcomeMessage = "Welcome to the group! User ID: $chatMemberId"; sendMessage($botToken, $chatId, $welcomeMessage); } else { // Member left $farewellMessage = "Goodbye! User ID: $chatMemberId"; sendMessage($botToken, $chatId, $farewellMessage); } } } function sendMessage($token, $chatId, $message) { $apiUrl = "https://api.telegram.org/bot$token/sendMessage"; $data = array('chat_id' => $chatId, 'text' => $message); $options = array( 'http' => array( 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data), ), ); $context = stream_context_create($options); $result = file_get_contents($apiUrl, false, $context); if ($result === false) { echo 'Failed to send message.'; } }

Trusted Seller- Автор вопроса
Trusted Seller
Now - <?php // Your bot token $botToken = 'YOUR_BO...

if (isset($update['chat_member'])) { // Handle chat_member update $chatMember = $update['chat_member']; $newChatMemberId = $chatMember['new_chat_member']['user']['id']; $newChatMemberStatus = $chatMember['new_chat_member']['status']; $oldChatMemberStatus = $chatMember['old_chat_member']['status']; if ( $newChatMemberStatus == 'member' && $oldChatMemberStatus == 'left') { // New member joined $welcomeMessage = "Welcome to the group! User ID: $newChatMemberId"; sendMessage($botToken, $chatId, $welcomeMessage); } else if ( $newChatMemberStatus == 'left' && $oldChatMemberStatus == 'member' ) { // Member left $farewellMessage = "Goodbye! User ID: $newChatMemberId"; sendMessage($botToken, $chatId, $farewellMessage); } }

Trusted Seller- Автор вопроса
Erry
if (isset($update['chat_member'])) { /...

Why it's need to add old chat member status?

Trusted Seller
Again ur code not work

I just posted a solution. Debug it and fix other mistakes in your code

Vanellope von Schweetz
Why it's need to add old chat member status?

Because otherwise it will catch also promotion, restrictions etc.

Похожие вопросы

Карта сайта