Created
September 20, 2018 14:00
-
-
Save ctison/cda3951d729aee939862f56c28d3b9af to your computer and use it in GitHub Desktop.
Python Scenario
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request | |
import json | |
app = Flask(__name__) | |
SCENARIO = { | |
1: { | |
'title': 'Main Menu', | |
'text': 'Here is the menu. What would you like to explore ?', | |
'choices': [2, '3'], | |
}, | |
2: { | |
'title': 'Option A', | |
'text': 'Here is option A. What sub-option would you like to explore ?', | |
'choices': [], # [4, 5, 6], | |
}, | |
'3': { | |
'title': 'Option B', | |
'text': 'Here is option B. What sub-option would you like to explore ?', | |
'choices': [], # [4, 8, 11], | |
}, | |
} | |
@app.route('/', methods=['POST']) | |
def bot(): | |
# Check Authorization | |
try: | |
auth = request.headers['Authorization'] | |
except KeyError: | |
return '', 401 | |
# Get json payload | |
data = request.get_json(force=True, cache=False) | |
# Get payload type | |
try: | |
type = data['type'] | |
except KeyError: | |
return json.dumps({'error': 'missing type'}), 400 | |
# Handle payload message type | |
if type == 'message': | |
try: | |
text = data['text'] | |
except KeyError: | |
text = '' | |
# Start scenario | |
if text == 'Hello': | |
resp = { | |
'text': 'Hello ! My name is _ and I will guide you !', | |
'buttons': [{ | |
'id': 1, | |
'title': SCENARIO[1]['title'], | |
}], | |
} | |
return json.dumps(resp) | |
else: | |
return json.dumps({'text': 'Hi !'}) | |
# Handle button tap | |
elif type == 'button_tap': | |
payload = data['payload'] | |
menu = SCENARIO[payload['button_id']] | |
response = { | |
'text': menu['text'], | |
'buttons': [], | |
} | |
for choice in menu['choices']: | |
response['buttons'].append({ | |
'id': choice, | |
'title': SCENARIO[choice]['title'], | |
}) | |
return json.dumps(response) | |
else: | |
return json.dumps({'error': 'type not found'}), 404 | |
################################################################################ | |
################################ Test ########################################## | |
################################################################################ | |
def test(): | |
with app.test_client() as c: | |
# Test GET | |
resp = c.get('/') | |
assert resp.status_code == 405 | |
# Test Authorization | |
resp = c.post('/', data={}) | |
assert resp.status_code == 401 | |
HEADERS = {'Authorization': 'X'} | |
# Test random message | |
resp = c.post('/', headers=HEADERS, json={'type':'message','text':"What's up ?"}) | |
assert resp.status_code == 200 | |
assert json.loads(resp.data)['text'] == 'Hi !' | |
# Test beginning of scenario | |
resp = c.post('/', headers=HEADERS, json={'type':'message','text':'Hello'}) | |
assert resp.status_code == 200 | |
data = json.loads(resp.data) | |
assert data['text'] == 'Hello ! My name is _ and I will guide you !' | |
assert data['buttons'] == [{'id':1,'title':'Main Menu'}] | |
# Test button tapped | |
resp = c.post('/', headers=HEADERS, json={'type':'button_tap','payload':{'button_id':1}}) | |
data = json.loads(resp.data) | |
assert data['text'] == SCENARIO[1]['text'] | |
assert data['buttons'] == [{'id':2,'title':'Option A'},{'id':'3','title':'Option B'}] | |
print('Tests: OK ✔ 🎉') | |
if __name__ == '__main__': test() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.PHONY: serve test | |
serve: | |
env FLASK_APP=bot.py FLASK_DEBUG=1 flask run | |
test: | |
python3 bot.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment