-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
126 lines (99 loc) · 4.83 KB
/
app.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
from typing import Tuple
from flask import Flask, render_template, redirect, request, sessions
from flask.ctx import after_this_request
from twilio.twiml.messaging_response import Body, Message, Redirect, MessagingResponse
import json
from decouple import config
from user_data import get_user
import asyncio
app = Flask(__name__)
app.secret_key = config('APP_SECRET')
@app.route('/')
def index():
return render_template('home.html')
def write(data):
session['Loaded'] = True
with open("data.json", "w") as database:
json.dump(data, database)
def go():
data = get_user(config('USER'), config('PASSWORD'))
return write(data)
session = {'Login':False, 'State':0, 'Loaded':False}
@app.route( '/bot', methods=['POST'])
def bot():
incoming_msg = request.values.get('Body', '').lower()
resp = MessagingResponse()
msg = resp.message()
responded = False
# handling session
if session['Login']:
if '?' in incoming_msg:
if session['Loaded']:
msg.body('your data sucessfully loaded. \nReply with following to get imformation\n*1.* *General*- general information \n*2* *User*- user info \n*3.* *Balance*- balance Inquiry \n*4.* *Logout*- to end your session.')
responded = True
else:
msg.body('still Loading!. Please wait for few seconds')
responded = True
if not session['Loaded']:
if 'start' in incoming_msg:
go()
msg.body("*Loading data*\nthis may take some time. Please reply with *?* to know status of your information")
responded = True
elif 'logout' in incoming_msg:
session['Login'] = False
session['Loaded'] = False
with open("data.json", "w") as database:
json.dump({}, database)
msg.body('session ended sucessfully! \n*Thank you*')
responded = True
else:
msg.body('your session alredy started. \n type *?* to know more.')
responded = True
if session['Loaded']:
if 'general' in incoming_msg:
val = ""
with open("data.json", "r") as database:
json_object = json.load(database)
val = json_object["1"]
msg_str1 = '*Name:* '+val['Name']+'\n'+'*'+val['Account Status']+'*'+'\n'+'*VC_NO.*- '+val['VC_NO.']+'\n'+'*Model*- '+val['Model']+'\n'
msg.body(msg_str1)
responded = True
elif 'balance' in incoming_msg:
val = ""
with open("data.json", "r") as database:
json_object = json.load(database)
val = json_object["2"]
msg_str2 = '*Acount balance till now:-* '+val['Balance_Today'] +'\n'+'*Last Recharge Amount*- ' +val['Last Recharge Amount']+'\n'+ '*Last Recharge Date*- '+val['Last Recharge Date']+'\n'+'*Next Recharge Date*- '+val['Next Recharge Date']+'\n' +'*Full Month Recharge*- '+ val['Full Month Recharge'] + '\n'
msg.body(msg_str2)
responded = True
elif 'user' in incoming_msg:
val = ""
with open("data.json", "r") as database:
json_object = json.load(database)
val = json_object["3"]
msg_str3 = "*Name*- "+ val['Name'] + '\n' + '*Registered Telephone Number*- '+ val['Registered Telephone Number'] + '\n' + '*Registered Email ID*- ' + val['Registered Email ID'] + '\n' + '*Address*- ' + val['Address'] + '\n'
msg.body(msg_str3)
responded = True
elif 'logout' in incoming_msg:
session['Login'] = False
session['Loaded'] = False
with open("data.json", "w") as database:
json.dump({}, database)
msg.body('session ended sucessfully! \n*Thank you*')
responded = True
else:
msg.body('Type *?* to know your command \n Type *START* to get your details or to send refresh command')
responded = True
if not responded:
msg.body('an Error occured! again Type *START* to start bot')
else:
if "yes" in incoming_msg:
session['Login'] = True
msg.body("your session began \ntype *START*- To getting your information")
responded = True
else:
msg.body('*your session is not initialised* \nfor developement purpose we stored your Auth Payloads \nIf you want to start your session reply with *YES*')
responded = True
return str(resp)
if __name__ == "__main__":
app.run(debug=True)