File size: 1,915 Bytes
9cf96c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# server.py
# where your python app starts

# init project
from flask import Flask, jsonify, render_template, request
#from typing_extensions import Literal
#from quart import Quart, jsonify, render_template, request
from discord_bot import discord_bot, sendMessageToChannel
from pingpong import pingpong
from threading import Thread
application = Flask(__name__)
#application.config['TIMEOUT'] = 1000


# I've started you off with Flask, 
# but feel free to use whatever libs or frameworks you'd like through `.requirements.txt`.

# unlike express, static files are automatic: http://flask.pocoo.org/docs/0.12/quickstart/#static-files

# http://flask.pocoo.org/docs/0.12/quickstart/#routing
# http://flask.pocoo.org/docs/0.12/quickstart/#rendering-templates
@application.route('/')
def hello():
    return render_template('index.html')

# Simple in-memory store
dreams = [
  'Find and count some sheep',
  'Climb a really tall mountain',
  'Wash the dishes',
]

@application.route('/status')
def status():
    return "Hello. I am alive!"

@application.route('/webhook', methods=['POST'])
def webhook():
    data = request.json
    message = data.get('message', 'No message provided')
    if 'id' in data or 'log_message' in data:
        sendMessageToChannel(data)
        return data
    return jsonify({'status': 'Message sent to Discord'})

@application.route('/dreams', methods=['GET'])
def get_dreams():
    return jsonify(dreams)

# could also use the POST body instead of query string: http://flask.pocoo.org/docs/0.12/quickstart/#the-request-object
@application.route('/dreams', methods=['POST'])
def add_dream():
    dreams.append(request.args.get('dream'))
    return ''
  
# listen for requests 
def run():
    if __name__ == "__main__":
        from os import environ
        application.run(host='0.0.0.0', port=int(environ['PORT']))
        
t = Thread(target=run)
t.start()
pingpong()
discord_bot()