├── Procfile ├── Readme.md ├── install.sh ├── requirements.txt └── server.py /Procfile: -------------------------------------------------------------------------------- 1 | web: gunicorn server:app -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Chatbot BootCamp 2 | 3 | ## Code examples for Bots can be found in the branches: 4 | * [Echo Server (flask example)](https://github.com/nurdtechie98/mr.robot/tree/echo) 5 | * [Number API Chatbot](https://github.com/nurdtechie98/mr.robot/tree/numbers_api) 6 | * [Fun Translation API](https://github.com/nurdtechie98/mr.robot/tree/fun_translation) 7 | * [Sentiment Analysis](https://github.com/nurdtechie98/ChatBot-BootCamp/tree/sentiment_analysis) 8 | * [Codecell Chatbot](https://github.com/KJSCE-Codecell/Chatbot-Codecell) 9 | > Checkout welcome() function in app/respond.py for format of quick-replies and images. 10 | 11 | 12 | ## List of Resources: 13 | * [Supplementary PPT for workshop](https://drive.google.com/file/d/1OoZhprc_lAmgMf6gLozJdKRYaFM0sl6k/view?usp=sharing) 14 | * [Official Dialogue Flow Tutorials](https://dialogflow.com/docs/tutorial-build-an-agent) 15 | * [Flask Messenger Echobot without DialogueFlow](https://tsaprailis.com/2016/06/02/How-to-build-and-deploy-a-Facebook-Messenger-bot-with-Python-and-Flask-a-tutorial/) 16 | * [Facebook App Review](https://developers.facebook.com/docs/apps/review/) 17 | > Note: In order to make your bot public and respond to all the people the above procedure is compulsory 18 | 19 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | apt-get install python3-pip 2 | pip3 install --upgrade pip 3 | python3 -m pip install -r requirements.txt 4 | apt install git 5 | apt install snapd 6 | snap install ngrok 7 | snap install --classic heroku 8 | snap install --classic vscode 9 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | certifi==2019.3.9 2 | chardet==3.0.4 3 | Click==7.0 4 | Flask==1.0.2 5 | gunicorn==19.9.0 6 | idna==2.8 7 | itsdangerous==1.1.0 8 | Jinja2==2.10 9 | MarkupSafe==1.1.1 10 | requests==2.21.0 11 | urllib3==1.24.1 12 | Werkzeug==0.14.1 13 | dialogflow 14 | beautifulsoup4==4.7.1 15 | -------------------------------------------------------------------------------- /server.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, request, make_response, jsonify 2 | import json 3 | import requests 4 | 5 | ''' 6 | Functions of the imported modules => 7 | 1. Flask - to initialise and use the flask app 8 | 2. request - to handle the requests sent to this flask server 9 | 3. make_response - creates a response object for the flask server 10 | 4. jsonify - creates a json response object, with header set to json 11 | 5. json - JSON is a syntax for storing and exchanging data. 12 | 6. requests - to make requests ('GET','POST',...) from python files 13 | ''' 14 | 15 | app = Flask(__name__) 16 | url = 'https://api.funtranslations.com/translate/' 17 | 18 | 19 | @app.route('/webhook', methods=['POST']) 20 | def webhook(): 21 | req = request.get_json(silent=True, force=True) 22 | print(req) 23 | query_result = req.get('queryResult') 24 | try: 25 | intent = # extact intent name ( displayName ) attribute from query_result 26 | except AttributeError: 27 | return 'json error' 28 | print('Intent: ' + intent) 29 | if intent == 'Translation Init': 30 | fulfillment = # extact text ( fulfillmentText ) attribute from query_result 31 | language = # extract language attribute from query_result 32 | return make_response(jsonify({'fulfillmentText': fulfillment, 'followupEventInput': {'name': 'Translate', 'parameters': {'language': language}, 'languageCode': 'en-US'}})) 33 | elif intent == 'Translate': 34 | query = # extact quert text ( queryText ) attribute from query_result 35 | language = # extract language attribute passed as context from query_result 36 | payload = {'text': query} 37 | final_url = url + language + '.json' 38 | res = # make a post request to final_url using given data ( payload ) 39 | if res.status_code == 200: 40 | text = # extract result ( translated text ) from response object 41 | else: 42 | print(res.status_code) 43 | text = "request overlimit" 44 | return # return a response object in json with fulfillmentText as attribute mapped with retreived results from API 45 | else: 46 | return # return a response object in json with fulfillmentText as attribute 47 | 48 | if __name__ == '__main__': 49 | app.run() 50 | --------------------------------------------------------------------------------