Skip to content

Commit 9401575

Browse files
committed
Init commit 😉
0 parents  commit 9401575

11 files changed

+362
-0
lines changed

.gitignore

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
#### joe made this: http://goel.io/joe
2+
3+
#### python ####
4+
# Byte-compiled / optimized / DLL files
5+
__pycache__/
6+
*.py[cod]
7+
*$py.class
8+
9+
# C extensions
10+
*.so
11+
12+
# Distribution / packaging
13+
.Python
14+
build/
15+
develop-eggs/
16+
dist/
17+
downloads/
18+
eggs/
19+
.eggs/
20+
lib/
21+
lib64/
22+
parts/
23+
sdist/
24+
var/
25+
wheels/
26+
*.egg-info/
27+
.installed.cfg
28+
*.egg
29+
MANIFEST
30+
31+
# PyInstaller
32+
# Usually these files are written by a python script from a template
33+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
34+
*.manifest
35+
*.spec
36+
37+
# Installer logs
38+
pip-log.txt
39+
pip-delete-this-directory.txt
40+
41+
# Unit test / coverage reports
42+
htmlcov/
43+
.tox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
.hypothesis/
51+
.pytest_cache/
52+
53+
# Translations
54+
*.mo
55+
*.pot
56+
57+
# Django stuff:
58+
*.log
59+
local_settings.py
60+
db.sqlite3
61+
62+
# Flask stuff:
63+
instance/
64+
.webassets-cache
65+
66+
# Scrapy stuff:
67+
.scrapy
68+
69+
# Sphinx documentation
70+
docs/_build/
71+
72+
# PyBuilder
73+
target/
74+
75+
# Jupyter Notebook
76+
.ipynb_checkpoints
77+
78+
# IPython
79+
profile_default/
80+
ipython_config.py
81+
82+
# pyenv
83+
.python-version
84+
85+
# celery beat schedule file
86+
celerybeat-schedule
87+
88+
# SageMath parsed files
89+
*.sage.py
90+
91+
# Environments
92+
.env
93+
.venv
94+
env/
95+
venv/
96+
ENV/
97+
env.bak/
98+
venv.bak/
99+
100+
# Spyder project settings
101+
.spyderproject
102+
.spyproject
103+
104+
# Rope project settings
105+
.ropeproject
106+
107+
# mkdocs documentation
108+
/site
109+
110+
# mypy
111+
.mypy_cache/
112+
.dmypy.json
113+
dmypy.json
114+
115+
116+
#### jetbrains ####
117+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
118+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
119+
120+
# User-specific stuff
121+
.idea/**/workspace.xml
122+
.idea/**/tasks.xml
123+
.idea/**/usage.statistics.xml
124+
.idea/**/dictionaries
125+
.idea/**/shelf
126+
127+
# Generated files
128+
.idea/**/contentModel.xml
129+
130+
# Sensitive or high-churn files
131+
.idea/**/dataSources/
132+
.idea/**/dataSources.ids
133+
.idea/**/dataSources.local.xml
134+
.idea/**/sqlDataSources.xml
135+
.idea/**/dynamic.xml
136+
.idea/**/uiDesigner.xml
137+
.idea/**/dbnavigator.xml
138+
139+
# Gradle
140+
.idea/**/gradle.xml
141+
.idea/**/libraries
142+
143+
# Gradle and Maven with auto-import
144+
# When using Gradle or Maven with auto-import, you should exclude module files,
145+
# since they will be recreated, and may cause churn. Uncomment if using
146+
# auto-import.
147+
# .idea/modules.xml
148+
# .idea/*.iml
149+
# .idea/modules
150+
151+
# CMake
152+
cmake-build-*/
153+
154+
# Mongo Explorer plugin
155+
.idea/**/mongoSettings.xml
156+
157+
# File-based project format
158+
*.iws
159+
160+
# IntelliJ
161+
out/
162+
163+
# mpeltonen/sbt-idea plugin
164+
.idea_modules/
165+
166+
# JIRA plugin
167+
atlassian-ide-plugin.xml
168+
169+
# Cursive Clojure plugin
170+
.idea/replstate.xml
171+
172+
# Crashlytics plugin (for Android Studio and IntelliJ)
173+
com_crashlytics_export_strings.xml
174+
crashlytics.properties
175+
crashlytics-build.properties
176+
fabric.properties
177+
178+
# Editor-based Rest Client
179+
.idea/httpRequests
180+
181+
# Google Cloud vision Credentials
182+
cred.json

.idea/Parsy.iml

+24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Parsy
2+
3+
This is a messenger chat bot used for copying the text from images that are shared in the chat.
4+
This is a goto OCR for messenger
5+
6+
## Steps for setting up Google Cloud Vision
7+
Set the environment variable `GOOGLE_APPLICATION_CREDENTIALS` to the file path of the JSON file that contains your service account key.
8+
```bash
9+
export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
10+
```
11+
12+
## Resources Used
13+
* [Secure Deployment](https://medium.com/@samuel.ngigi/deploying-python-flask-to-aws-and-installing-ssl-1216b41f8511)
14+
15+
## Author
16+
* yashLadha

app.py

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import os
2+
import requests
3+
from dotenv import load_dotenv
4+
from flask import Flask, request
5+
6+
load_dotenv()
7+
8+
app = Flask(__name__)
9+
10+
PAGE_ACCESS_TOKEN = os.getenv('ACCESS_TOKEN')
11+
FB_API_URL = os.getenv('FB_API_URL')
12+
13+
14+
@app.route('/')
15+
def hello_world():
16+
return 'Hello World!'
17+
18+
19+
@app.route('/webhook', methods=['GET'])
20+
def verify_webhook():
21+
if request.args.get('hub.verify_token') == os.getenv('VERIFY_TOKEN'):
22+
return request.args.get('hub.challenge')
23+
return "Invalid token"
24+
25+
26+
def detect_text(uri):
27+
from google.cloud import vision
28+
client = vision.ImageAnnotatorClient()
29+
response = requests.get(uri)
30+
image = vision.types.Image(content=response.content)
31+
response = client.text_detection(image=image)
32+
texts = response.text_annotations
33+
string = texts[0].description
34+
return string
35+
36+
37+
def send_message(psid, ocr_message):
38+
payload = {
39+
'message': {
40+
'text': ocr_message
41+
},
42+
'recipient': {
43+
'id': psid
44+
},
45+
'notification_type': 'REGULAR'
46+
}
47+
auth = {
48+
'access_token': PAGE_ACCESS_TOKEN
49+
}
50+
response = requests.post(FB_API_URL, params=auth, json=payload)
51+
return response.json()
52+
53+
54+
@app.route('/webhook', methods=['POST'])
55+
def listen():
56+
json_res = request.json
57+
payload = json_res['entry']
58+
for entries in payload:
59+
# Checks for the text message event
60+
if 'message' in entries:
61+
psid = entries['sender']['id']
62+
text_message = entries['message']['text']
63+
response = send_message(psid, "Please upload an image")
64+
print('Response ', response)
65+
66+
# Checks for the attachment message event
67+
if 'messaging' in entries:
68+
for attach in entries['messaging']:
69+
psid = attach['sender']['id']
70+
if 'message' in attach:
71+
if 'attachments' in attach['message']:
72+
for resource in attach['message']['attachments']:
73+
# Image attachment for scrapping
74+
if resource['type'] == 'image':
75+
# Fetch the payload url
76+
if 'payload' in resource:
77+
image_url = resource['payload']['url']
78+
ocr_message = detect_text(image_url)
79+
response = send_message(psid, ocr_message)
80+
print('Response ', response)
81+
return 'Processed Response'
82+
83+
84+
if __name__ == '__main__':
85+
app.run(host='0.0.0.0', port='8000')

requirements.txt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
cachetools==3.0.0
2+
certifi==2018.11.29
3+
chardet==3.0.4
4+
Click==7.0
5+
Flask==1.0.2
6+
google-api-core==1.6.0
7+
google-auth==1.6.1
8+
google-cloud-vision==0.35.1
9+
googleapis-common-protos==1.5.5
10+
grpcio==1.16.1
11+
idna==2.7
12+
itsdangerous==1.1.0
13+
Jinja2==2.10
14+
MarkupSafe==1.1.0
15+
protobuf==3.6.1
16+
pyasn1==0.4.4
17+
pyasn1-modules==0.2.2
18+
PyMessager==1.0.1
19+
python-dotenv==0.9.1
20+
pytz==2018.7
21+
requests==2.20.1
22+
rsa==4.0
23+
six==1.11.0
24+
urllib3==1.24.1
25+
Werkzeug==0.14.1

start.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gunicorn --bind 0.0.0.0:8000 wsgi:app

wsgi.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from app import app
2+
3+
if __name__ == '__main__':
4+
app.run()

0 commit comments

Comments
 (0)