-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
85 lines (69 loc) · 2.55 KB
/
app.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import requests
from dotenv import load_dotenv
from flask import Flask, request
load_dotenv()
app = Flask(__name__)
PAGE_ACCESS_TOKEN = os.getenv('ACCESS_TOKEN')
FB_API_URL = os.getenv('FB_API_URL')
@app.route('/')
def hello_world():
return 'Hello World!'
@app.route('/webhook', methods=['GET'])
def verify_webhook():
if request.args.get('hub.verify_token') == os.getenv('VERIFY_TOKEN'):
return request.args.get('hub.challenge')
return "Invalid token"
def detect_text(uri):
from google.cloud import vision
client = vision.ImageAnnotatorClient()
response = requests.get(uri)
image = vision.types.Image(content=response.content)
response = client.text_detection(image=image)
texts = response.text_annotations
string = texts[0].description
return string
def send_message(psid, ocr_message):
payload = {
'message': {
'text': ocr_message
},
'recipient': {
'id': psid
},
'notification_type': 'REGULAR'
}
auth = {
'access_token': PAGE_ACCESS_TOKEN
}
response = requests.post(FB_API_URL, params=auth, json=payload)
return response.json()
@app.route('/webhook', methods=['POST'])
def listen():
json_res = request.json
payload = json_res['entry']
for entries in payload:
# Checks for the text message event
if 'message' in entries:
psid = entries['sender']['id']
text_message = entries['message']['text']
response = send_message(psid, "Please upload an image")
print('Response ', response)
# Checks for the attachment message event
if 'messaging' in entries:
for attach in entries['messaging']:
psid = attach['sender']['id']
if 'message' in attach:
if 'attachments' in attach['message']:
for resource in attach['message']['attachments']:
# Image attachment for scrapping
if resource['type'] == 'image':
# Fetch the payload url
if 'payload' in resource:
image_url = resource['payload']['url']
ocr_message = detect_text(image_url)
response = send_message(psid, ocr_message)
print('Response ', response)
return 'Processed Response'
if __name__ == '__main__':
app.run(host='0.0.0.0', port='8000')