import os
import sys
from flask import Flask
from flask import Response
from flask import render_template
from twilio.rest import TwilioRestClient
import twilio.twiml
import praw

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('home.html')

@app.route('/make_call')
def make_call():
    account_sid = os.environ['ACCOUNT_SID']
    auth_token = os.environ['AUTH_TOKEN']
    client = TwilioRestClient(account_sid, auth_token)

    call = client.calls.create(to="+13054406756",
                               from_="+16175130992",
                               url="http://obscure-dawn-3571.herokuapp.com/message")

    return "Call in progress"

@app.route('/message', methods=['GET','POST'])
def message():
    reload(sys)
    sys.setdefaultencoding('utf8')
    r = praw.Reddit(user_agent='web_caller_zeke')
    titles = r.get_front_page(limit=1)
    
    resp = twilio.twiml.Response()
    resp.say("Hey Chris!. How is it going?.\
    This is Ezequiel with some news from Reddit.")
    
    for x in titles:
      resp.say(str(x.title))
    
    resp.say("Cheers!")

    return Response(str(resp), mimetype='text/xml')

# if __name__ == "__main__":
#   app.run(debug=True)
