GEMINI OCR SCANNER - SMART JSON TEST GAS SCRIPT✅success items fetch from bills/Tokens
// ==========================================
// GEMINI OCR SCANNER - SMART JSON TEST SCRIPT
// ==========================================
function doPost(e) {
try {
var data = JSON.parse(e.postData.contents);
// Check if the payload is coming from the HTML format we set up
var base64Image = data.payload ? data.payload.base64Image : data.image.split(',')[1];
// 🔥 APNI GEMINI API KEY YAHAN DAALEIN 🔥
var apiKey = "AIzaSyBbJ-Pv1df886JSvHA9zkJTe-G-iBHJGXw";
var url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.5-flash-lite:generateContent?key=" + apiKey;
// 🔥 MAGIC PROMPT: AI ko seedha Cart banane ka order diya
var payload = {
"contents": [{
"parts": [
{"text": "Extract ONLY the ordered food/drink items and their quantities from this receipt. Return the result STRICTLY as a JSON array of objects. Example Format: [{\"name\": \"Dosa Spl\", \"qty\": 1}, {\"name\": \"Water Bottle 1lt\", \"qty\": 2}]. Do NOT include prices, totals, GST, or any other text. Do NOT wrap it in ```json markdown, just output the pure JSON array."},
{
"inline_data": {
"mime_type": "image/jpeg",
"data": base64Image
}
}
]
}]
};
var options = {
"method": "post",
"contentType": "application/json",
"payload": JSON.stringify(payload),
"muteHttpExceptions": true // Catch errors without failing the script
};
var response = UrlFetchApp.fetch(url, options);
var json = JSON.parse(response.getContentText());
if(json.error) {
return ContentService.createTextOutput(JSON.stringify({ status: "error", message: json.error.message })).setMimeType(ContentService.MimeType.JSON);
}
var aiResponse = json.candidates[0].content.parts[0].text;
// Clean markdown tags if AI sends them by mistake
aiResponse = aiResponse.replace(/```json/gi, '').replace(/```/g, '').trim();
return ContentService.createTextOutput(JSON.stringify({ status: "success", text: aiResponse })).setMimeType(ContentService.MimeType.JSON);
} catch(err) {
return ContentService.createTextOutput(JSON.stringify({ status: "error", message: err.toString() })).setMimeType(ContentService.MimeType.JSON);
}
}
// CORS ke liye zaroori
function doOptions(e) {
return ContentService.createTextOutput("OK").setMimeType(ContentService.MimeType.TEXT);
}
// Permission Bypass Test Function
function forceAuth() {
UrlFetchApp.fetch("[https://www.google.com](https://www.google.com)");
}