Skip to content

Instantly share code, notes, and snippets.

@alessandrocucci
Created November 26, 2016 13:38
Show Gist options
  • Save alessandrocucci/d446d4670bf3bd3a477da72ef984fd8c to your computer and use it in GitHub Desktop.
Save alessandrocucci/d446d4670bf3bd3a477da72ef984fd8c to your computer and use it in GitHub Desktop.
Codemotion 2016 Challenge
"""
CODEMOTION 2016, Milan (IT)
Challenge:
- Write a chatbot
- Must provide REST API
- Write the smallest source code possible
- In Python
Required libs:
- Flask
Run the server:
$> python apibotcodemotion.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Usage:
$> curl -X GET localhost:5000
{
"message": "Why do you bother me? You filthy human!"
}
$> curl -X POST localhost:5000 -d "Hello, Codemotion!!"
{
"message": "So, you ask me: 'Hello, Codemotion!!', and why should I care?"
}
"""
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def answerme():
if request.method == 'POST' and request.form:
message = "So, you ask me: '{0}', and why should I care?".format(next(request.form.iterkeys()))
return jsonify(message=message)
else:
return jsonify(message="Why do you bother me? You filthy human!")
if __name__ == '__main__':
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment