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 ответов

80 просмотров
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.

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

Обсуждают сегодня

Он в одиночку это дело запилил или была какая-то команда?
Aquinary
12
Вообще кстати бывают такие тулкиты чтобы вот разработал под ОС X, всё оттестировал работает А потом собрал под ОС Y - и там просто без вообще любых изменений заработало?
Serg Gini
14
всем привет, кто знает нормальный гайд как настроить отладчик в Intelij на Windows?
QUAD69
6
А, вообще, знает кто-нить альтернативы D в области безопасных, читабельных ОО-языков?
Nik Lan
14
Работа над эдишенами там какая-то ведется? Или пока что тишина?
Serg Gini
1
приветствую. хочу сделать себе D Playground вопрос: можете подсказать с чего мне следует начать и в какую сторону двигаться? P.S.: я не являюсь программистом... но в небольш...
dd
5
а как в dub выключить дебажный билд?
Maxim Filimonov
12
Что кто фри селф хостинг там практикует? Через tunnel?
Serg Gini
8
А что непонятного? В чем сложность взять слайс от вектора? И работать как обычно. Какие-то выдумки опять на ровной почве. Да и RAII в ди прекрасно работает с самого начала.
Aleksandr Druzhinin
14
а почему, кстати, геймдевы обращают такое внимание на "новые" языки типа того же D (а также Zig)?
Maxim Filimonov
9
Карта сайта