-
Notifications
You must be signed in to change notification settings - Fork 1
/
launch.py
141 lines (129 loc) · 5.98 KB
/
launch.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from flask import Flask, render_template, request, Response, url_for
from flaskext.mysql import MySQL
import MySQLdb
import json
import plivo
import plivoxml
import time
auth_id = 'MANJVHMTE4ODRHODA3ND'
auth_token = 'NGNiNjg5N2RlYzEwNmZjNTFhNzQ2NWFkNDY4OWE4'
f_number = ''
app = Flask(__name__)
@app.route('/')
def HomePage():
return render_template('index.html')
@app.route('/list')
def ViewList():
db=MySQLdb.connect("216.12.194.50","purvotar_root", "root1", "purvotar_cfi")
cur = db.cursor()
query = "SELECT * FROM farmersdata"
cur.execute(query)
data = cur.fetchall()
db.close()
html = "<html><style>"
html += "#hor-minimalist-b{ font-family: 'Lucida Sans Unicode', 'Lucida Grande', Sans-Serif;font-size: 12px;background: #fff;margin: 45px;border-collapse: collapse;text-align: left;}"
html+="#hor-minimalist-b th{font-size: 16px;font-weight: normal;color: #039;padding: 16px 10px;border-bottom: 2px solid #6678b1;}"
html+="#hor-minimalist-b td{border-bottom: 1px solid #ccc;color: #669;font-size:12px;padding: 12px 10px;}"
html+="#hor-minimalist-b tbody tr:hover td{color: #009;}</style>"
html += "<body><table width='800' id='hor-minimalist-b'><tr><th>Farmer ID</th><th>Farmer Name</th><th>Village Name</th><th>Phone Number</th><th>Block Name</th><th>Interested</th><th>Adopted</th><th> Call Now</th></tr>"
for row in data:
html += '<tr>'
for ele in row:
col = '<td>'+str(ele)+'</td>'
html += col
par = "/call?key="+str(row[3])
html+="<td><a href='"+par+"'>Call</a></td></tr>"
html += '</table></body></html>'
return html
@app.route('/populate', methods=['POST'])
def addEntry():
'''
name = request.form['name']
village = request.form['vilname']
phno = request.form['phno']
block = request.form['blockname']
'''
a = request.form['name']
b = request.form['vilname']
c = request.form['phno']
d = request.form['blockname']
video_id = request.form['videoID']
video_titles = request.form['videoTitles']
db=MySQLdb.connect("216.12.194.50","purvotar_root", "root1", "purvotar_cfi")
cur = db.cursor()
sql = "INSERT INTO farmersdata(farmerid,name,villagename,phone,blockname) VALUES (null,'"+str(a)+"','"+str(b)+"','"+str(c)+"','"+str(d)+"')"
try:
cur.execute(sql)
db.commit()
except Exception as e:
db.rollback()
print "Problem inserting a row"
db.close()
return "<html><head><meta http-equiv='refresh' content='2;URL=\"http://fathomless-inlet-8852.herokuapp.com/\"'></head><body><center><h2>New entry added</h2></center></body></html>"
@app.route('/call',methods=['GET'])
def make_call():
p = plivo.RestAPI(auth_id,auth_token)
params = {'from':'919242733911', 'to':request.args.get('key', '') , 'answer_url' : 'http://fathomless-inlet-8852.herokuapp.com/answer'}
f_number = request.args.get('key', '')
response = p.make_call(params)
return "<html><head><meta http-equiv='refresh' content='2;URL=\"http://fathomless-inlet-8852.herokuapp.com/list\"'></head><body><center><h2>Call is being made. Please wait while we connect you to the farmer</h2></center></body></html>"
db.commit()
@app.route('/answer', methods=['GET','POST'])
def ivr():
'''
text = 'Hello, you have recently watched S R I technique videos'
response = plivoxml.Response()
params = {'loop':1,'language':"en-US", 'voice':'WOMAN'}
response.addSpeak(text,**params)
return Response(str(response), mimetype='text/xml')
'''
response = plivoxml.Response()
if request.method == 'POST':
getdigits_action_url = url_for('digit', _external=True)
getDigits = plivoxml.GetDigits(action=getdigits_action_url, method='POST',timeout=7, numDigits=1, retries=1)
getDigits.addSpeak("You have recently watched S R I technique video. Did you express interest in this video. Press one for yes. Press two for no")
response.add(getDigits)
response.addSpeak("Sorry, I didn't catch that. Please hangup and try again later.")
return Response(str(response), mimetype='text/xml')
@app.route('/digit', methods=['GET','POST'])
def digit():
response = plivoxml.Response()
time.sleep(2)
if request.method == 'GET':
print 'Got a GET request'
else:
print 'Got a POST request'
digit = request.form['Digits']
print "The digit from phone"
print digit
db=MySQLdb.connect("216.12.194.50","purvotar_root", "root1", "purvotar_cfi")
cur = db.cursor()
if digit == "1":
# Fetch a random joke using the Reddit API.
response.addSpeak('Thank you for your interest in Digital Green')
#sql = 'UPDATE farmersdata SET interested='+'"YES"' + 'WHERE phone='+'"f_number"'
print f_number
sql = 'UPDATE farmersdata SET interested=\'YES\' WHERE phone=\''+f_number+'\''
#print sql
#query = "UPDATE farmersdata SET interested=YES WHERE phone=" + f_number
print "Got the digit one"
elif digit == "2":
# Listen to a song
response.addSpeak('Thank you for the feedback')
sql = 'UPDATE farmersdata SET interested=\'NO\' WHERE phone=\''+f_number+'\''
#sql = 'UPDATE farmersdata SET interested='+'"NO"' + 'WHERE phone='+'f_number'
#query = "UPDATE farmersdata SET interested=NO WHERE phone=" + f_number
print "Got the digit two"
else:
response.addSpeak("Sorry, it's wrong input.")
sql = 'UPDATE farmersdata SET interested=\'NO\' WHERE phone=\''+f_number+'\''
#sql = 'UPDATE farmersdata SET interested='+'"NO"' + 'WHERE phone='+'f_number'
#query = "UPDATE farmersdata SET interested=NO WHERE phone=" + f_number
cur.execute(sql)
db.close()
return Response(str(response), mimetype='text/xml')
if __name__ == "__main__":
import os
port = int(os.environ.get("PORT", 5000))
print port
app.run(host='0.0.0.0',port=port)