File size: 1,463 Bytes
ee3a6ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from flask import Flask, request, jsonify
import pytesseract
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError

app = Flask(__name__)

# Google App Script API credentials
SCOPES = ['https://www.googleapis.com/auth/script.projects']
SERVICE_ACCOUNT_FILE = 'service_account.json'

# Load credentials from service account file
creds = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, SCOPES)

# Create Google App Script API client
script_service = build('script', 'v1', credentials=creds)

@app.route('/ocr', methods=['POST'])
def ocr():
    # Get image from request
    img = request.get_json()['image']

    # Perform OCR using Tesseract
    text = pytesseract.image_to_string(img)

    # Log OCR result
    print(f'OCR result: {text}')

    # Insert OCR result into Google Chat
    insert_into_google_chat(text)

    return jsonify({'message': 'OCR successful'})

def insert_into_google_chat(text):
    # Create Google Chat message
    message = {
        'text': f'////////⭐️⭐️⭐️⭐️ Bot_gas_main_dev 個人情報の確認 {text}'
    }

    # Insert message into Google Chat
    try:
        script_service.scripts().run(body={'function': 'insertMessage', 'parameters': [message]}).execute()
    except HttpError as e:
        print(f'Error inserting message into Google Chat: {e}')

if __name__ == '__main__':
    app.run(debug=True)