9d845c8899
- backend moved to backend directory - added and initialized frontend with vue - moved infrastructure files to infra directory
79 lines
1.9 KiB
Go
79 lines
1.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"FamilyHub/src/domain"
|
|
"FamilyHub/src/utils"
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
)
|
|
|
|
func (h *Handler) HandlePhoto(msg *tgbotapi.Message) {
|
|
photo := msg.Photo[len(msg.Photo)-1]
|
|
|
|
file, err := h.api.GetFile(tgbotapi.FileConfig{FileID: photo.FileID})
|
|
if err != nil {
|
|
h.reply(msg.Chat.ID, "Не смог получить файл 😢")
|
|
return
|
|
}
|
|
|
|
url := file.Link(h.api.Token)
|
|
|
|
resp, err := http.Get(url)
|
|
if err != nil {
|
|
h.reply(msg.Chat.ID, "Ошибка загрузки изображения")
|
|
return
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
imageBytes, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
h.reply(msg.Chat.ID, "Ошибка чтения изображения")
|
|
return
|
|
}
|
|
|
|
text, err := h.ocr.Recognize(context.Background(), imageBytes)
|
|
if err != nil {
|
|
h.reply(msg.Chat.ID, "Ошибка OCR 😢")
|
|
return
|
|
}
|
|
|
|
if text == "" {
|
|
h.reply(msg.Chat.ID, "Текст не найден")
|
|
return
|
|
}
|
|
|
|
receiptMeta := utils.ExtractReceiptMeta(text)
|
|
payload := domain.AddReceiptRequest{Number: receiptMeta.ReceiptID, Date: receiptMeta.Date}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
|
|
txt, err := utils.DecodeQR(imageBytes)
|
|
println(txt)
|
|
|
|
err = h.receiptApi.SendReceipt(ctx, payload)
|
|
|
|
reply := "📄 *Результат распознавания*\n\n"
|
|
if receiptMeta.Date != "" {
|
|
reply += "📅 Дата: " + receiptMeta.Date + "\n"
|
|
} else {
|
|
reply += "📅 Дата: не найдена\n"
|
|
}
|
|
if receiptMeta.ReceiptID != "" {
|
|
reply += "🧾 Номер чека:\n`" + receiptMeta.ReceiptID + "`\n"
|
|
} else {
|
|
reply += "🧾 Номер чека: не найден\n"
|
|
}
|
|
if err != nil {
|
|
reply += "Не удалось отправить чек в API " + err.Error()
|
|
} else {
|
|
reply += "Чек добавлен в базу"
|
|
}
|
|
|
|
h.replyMarkdown(msg.Chat.ID, reply)
|
|
}
|