UNPKG

keera-topup-v1

Version:

TrueMoney Wallet topup module (redeem angpao/voucher)

30 lines (25 loc) 1.18 kB
import Tesseract from "tesseract.js"; import fetch from "node-fetch"; import fs from "fs"; export async function readSlip(link) { try { // โหลดรูปจากลิงก์ const res = await fetch(link); const buffer = await res.buffer(); fs.writeFileSync("temp.png", buffer); // ใช้ Tesseract อ่านข้อความ const { data: { text } } = await Tesseract.recognize("temp.png", "tha+eng", { logger: m => console.log(m) }); // ตัวอย่าง parse ยอดเงินจากข้อความ const match = text.match(/(\d{1,3}(?:,\d{3})*(?:\.\d{2})?)/); const amount = match ? parseFloat(match[1].replace(/,/g, "")) : 0; // สมมติชื่อเจ้าของจากข้อความ const ownerMatch = text.match(/ชื่อผู้รับเงิน:\s*(.+)/i); const owner = ownerMatch ? ownerMatch[1] : "Unknown"; return { amount, owner }; } catch (e) { throw new Error("Failed to read slip: " + e.message); } finally { // ลบ temp.png หลังอ่าน fs.unlinkSync("temp.png"); } }