Skip to content

Instantly share code, notes, and snippets.

@chrisshroba
Created September 23, 2016 01:30
Show Gist options
  • Save chrisshroba/322d370f6ef9feae2e086e9b94937bd0 to your computer and use it in GitHub Desktop.
Save chrisshroba/322d370f6ef9feae2e086e9b94937bd0 to your computer and use it in GitHub Desktop.
Dogfacts Slack Bot
from flask import *
import random
import json
app = Flask(__name__)
@app.before_request
def before_request():
raw_text = open('facts.txt').read()
g.facts = raw_text.split('\n')
@app.route("/fact")
def getFact():
text = request.args.get('text',None)
num_facts = 1
if text in list(map(str,range(2,10))):
num_facts = int(text)
response_text=""
for i in range(num_facts):
response_text += "{}\n".format(random.choice(g.facts))
response = {
"response_type": "in_channel",
"text": response_text
}
json_response = json.dumps(response)
return Response(json_response, mimetype='application/json')
app.run(debug=True, host="0.0.0.0", port=4101)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment