├── .gitignore ├── Assets ├── app.jpg ├── output1.jpeg ├── output2.jpeg ├── output3.jpeg ├── output4.jpeg └── whatsapp.png ├── LICENSE ├── README.md ├── data └── credentials.json ├── main.py ├── requirements.txt └── src ├── pythonREPL.py └── services.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /Assets/app.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itspyguru/Python-on-WhatsApp/beef692742fcac51d4f3f942dd10134d1dbc387a/Assets/app.jpg -------------------------------------------------------------------------------- /Assets/output1.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itspyguru/Python-on-WhatsApp/beef692742fcac51d4f3f942dd10134d1dbc387a/Assets/output1.jpeg -------------------------------------------------------------------------------- /Assets/output2.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itspyguru/Python-on-WhatsApp/beef692742fcac51d4f3f942dd10134d1dbc387a/Assets/output2.jpeg -------------------------------------------------------------------------------- /Assets/output3.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itspyguru/Python-on-WhatsApp/beef692742fcac51d4f3f942dd10134d1dbc387a/Assets/output3.jpeg -------------------------------------------------------------------------------- /Assets/output4.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itspyguru/Python-on-WhatsApp/beef692742fcac51d4f3f942dd10134d1dbc387a/Assets/output4.jpeg -------------------------------------------------------------------------------- /Assets/whatsapp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itspyguru/Python-on-WhatsApp/beef692742fcac51d4f3f942dd10134d1dbc387a/Assets/whatsapp.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Prajjwal Pathak 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Python On WhatsApp

2 |

Run your python codes on whatsapp along with talking to a chatbot

3 |

4 | 5 | 6 | 7 |

8 | 9 |

10 | This is a small python project to run python on whatsapp. and i created this project for HackCBS 4.0 hackathon organized by HackerEarth. I am thinking to turn it to a complete chatbot service for python soon. 11 | 12 | A working bot preview is here : [pythonWA](https://www.youtube.com/watch?v=Rlv0gcXmX7g) 13 |

14 | 15 |

Features

16 | 21 | 22 | 23 | 24 | ## How to Download 25 | 26 | Clone this repository or download this project from here [Download PythonWA Source Code](https://downgit.github.io/#/home?url=https://github.com/pyGuru123/Python-on-WhatsApp) 27 | 28 | ## Requirements 29 | 30 | * A Twilio account. If you are new to Twilio [create a free account now](http://www.twilio.com/referral/7fB3Je) 31 | 32 | * In Addition, you will be required to download ngrok while running this bot on localhost. Download it from [here](https://ngrok.com/download) 33 | 34 | * All the required packages are written inside the requirements.txt file. 35 | 36 | Use the package manager [pip](https://pip.pypa.io/en/stable/) to install them by running this command :- 37 | 38 | ```bash 39 | pip install -r requirements.txt 40 | ``` 41 | 42 | ## Setup 43 | 44 | This is a 7 step setup process.\ 45 | 46 | * download source code 47 | * install packages from requirements.txt by pip install -r requirements.txt 48 | * download ngrok and extract exe, and make a account on ngrok website 49 | * create a twilio account for free 50 | * run main.py in your terminal 51 | * run ngrok by double clicking ngrok.exe and execute this command > ngrok http 5000 52 | * copy the ngrok forwarding link and paste it in twilio sandbox 53 | 54 | now write any code or query on whatsapp and send and done 55 | 56 | ## Contributing 57 | Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate. 58 | 59 |
60 | 61 | ### Show some ❤️ by starring this repository! 62 | 63 |
64 | -------------------------------------------------------------------------------- /data/credentials.json: -------------------------------------------------------------------------------- 1 | {"wolfram-alpha": ""} -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import requests 2 | from flask import Flask, request 3 | from twilio.twiml.messaging_response import MessagingResponse 4 | 5 | from src.pythonREPL import execute_python, install_package 6 | import src.services as services 7 | 8 | app = Flask(__name__) 9 | 10 | @app.route('/bot', methods=['POST']) 11 | def bot(): 12 | incoming_msg = request.values.get('Body', '').strip() 13 | isPythonCode = False 14 | resp = MessagingResponse() 15 | msg = resp.message() 16 | 17 | if incoming_msg.startswith('#!python3'): 18 | code = incoming_msg.lstrip('#!python3') 19 | output = execute_python(code) 20 | msg.body(output) 21 | isPythonCode = True 22 | 23 | elif incoming_msg.startswith('!pip install'): 24 | package = incoming_msg.split()[-1] 25 | output = install_package(package) 26 | msg.body(output) 27 | isPythonCode = True 28 | 29 | if isPythonCode: 30 | return str(resp) 31 | 32 | incoming_msg = incoming_msg.lower() 33 | 34 | if 'your name' in incoming_msg: 35 | output = 'I am Itachi Uchiha' 36 | 37 | elif 'date' in incoming_msg: 38 | output = services.get_date() 39 | 40 | elif 'time' in incoming_msg: 41 | output = services.get_time() 42 | 43 | elif 'joke' in incoming_msg: 44 | output = services.get_joke() 45 | 46 | elif 'quote' in incoming_msg: 47 | output = services.get_quote() 48 | 49 | else: 50 | api_key = services.fetch_apikey('wolfram-alpha') 51 | if api_key == None: 52 | output = 'Wolfram alpha api key needed, check docs' 53 | else: 54 | output = services.chatbot(api_key, incoming_msg) 55 | 56 | msg.body(output) 57 | return str(resp) 58 | 59 | if __name__ == '__main__': 60 | app.run() -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | certifi==2019.9.11 2 | chardet==3.0.4 3 | Click==7.0 4 | Flask==1.1.1 5 | idna==2.8 6 | itsdangerous==1.1.0 7 | Jinja2==2.10.3 8 | MarkupSafe==1.1.1 9 | PyJWT==1.7.1 10 | pytz==2019.3 11 | requests==2.22.0 12 | six==1.13.0 13 | twilio==6.33.1 14 | urllib3==1.25.7 15 | Werkzeug==0.16.0 -------------------------------------------------------------------------------- /src/pythonREPL.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import importlib 3 | import contextlib 4 | import subprocess 5 | from io import StringIO 6 | 7 | @contextlib.contextmanager 8 | def stdoutIO(stdout=None): 9 | old = sys.stdout 10 | if stdout is None: 11 | stdout = StringIO() 12 | sys.stdout = stdout 13 | yield stdout 14 | sys.stdout = old 15 | 16 | def execute_python(code): 17 | with stdoutIO() as c: 18 | try: 19 | exec(code) 20 | except: 21 | print("Something wrong with the code") 22 | return c.getvalue() 23 | 24 | def install_package(package): 25 | try: 26 | importlib.import_module(package) 27 | return f'{package} already installed' 28 | except: 29 | subprocess.check_call([sys.executable, "-m", "pip", "install", package]) 30 | return f'{package} installed successfully' -------------------------------------------------------------------------------- /src/services.py: -------------------------------------------------------------------------------- 1 | import os 2 | import json 3 | import random 4 | import requests 5 | import datetime 6 | 7 | def get_date(): 8 | dt = datetime.datetime.now() 9 | dt = dt.date() 10 | return dt.strftime('%B %d, %Y') 11 | 12 | def get_time(): 13 | dt = datetime.datetime.now() 14 | dt = dt.time() 15 | return dt.strftime('%I:%M %p') 16 | 17 | def get_joke(): 18 | url = 'https://some-random-api.ml/joke' 19 | r = requests.get(url) 20 | data = r.json() 21 | return data['joke'] 22 | 23 | def get_quote(): 24 | url = 'https://api.quotable.io/random' 25 | output = '' 26 | 27 | r = requests.get(url) 28 | quote = r.json() 29 | output += quote['content'] + '\n' 30 | output += f" -{quote['author']}" 31 | 32 | return output 33 | 34 | def fetch_apikey(api): 35 | with open('data/credentials.json') as f: 36 | data = json.load(f) 37 | key = data.get(api, None) 38 | 39 | return data[api] 40 | 41 | def chatbot(api_key, query): 42 | url = f"http://api.wolframalpha.com/v1/result?appid={api_key}&i={query}%3f" 43 | r = requests.get(url) 44 | data = r.text 45 | if data == 'Wolfram|Alpha did not understand your input': 46 | return 'Couldn\'t understand the query' 47 | else: 48 | return data 49 | --------------------------------------------------------------------------------