gptbot.php
$chatId,
'text' => $message,
'reply_to_message_id' => $replyToMessageId,
'parse_mode' => 'markdown'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
$response = curl_exec($ch);
if (curl_errno($ch)) {
error_log('Telegram API cURL error: ' . curl_error($ch));
}
curl_close($ch);
return $response;
}
function handleTelegramWebhook($token) {
// Get the incoming update from Telegram
$update = file_get_contents("php://input");
$updateArray = json_decode($update, true);
if (isset($updateArray['message']) && isset($updateArray['message']['chat']['type'])) {
$chatType = $updateArray['message']['chat']['type'];
if ($chatType === 'private') {
$chatId = $updateArray['message']['chat']['id'];
$userMessage = $updateArray['message']['text'];
$messageId = $updateArray['message']['message_id'];
$apiResponse = getApiResponse($userMessage);
sendTelegramMessage($chatId, $apiResponse, $messageId, $token);
}
} else {
error_log("No valid message or chat type found in the update.");
}
}
// Replace with your bot token
$botToken = '6231554332:AAFEZTmqukg-ZVWJMygqkKW9zx0C5oQ5EXo';
handleTelegramWebhook($botToken);
?>
Comments
Post a Comment