awacke1 commited on
Commit
68a9462
β€’
1 Parent(s): 7bb1f81

Create backup2.app.py

Browse files
Files changed (1) hide show
  1. backup2.app.py +111 -0
backup2.app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import hashlib
3
+ import streamlit as st
4
+ import textflowsms as tf
5
+ from datetime import datetime
6
+
7
+ # Central Time Zone Adjustment
8
+ import pytz
9
+ central = pytz.timezone('US/Central')
10
+
11
+ # Function to format phone number
12
+ def format_phone_number(phone_number):
13
+ if len(phone_number) == 10 and not phone_number.startswith('+'):
14
+ return '+1' + phone_number
15
+ return phone_number
16
+
17
+ # Function to hash a password
18
+ def hash_password(password):
19
+ return hashlib.sha256(password.encode()).hexdigest()
20
+
21
+ # Function to save user data to a file
22
+ def save_user_data(phone_number, password_hash):
23
+ timestamp = datetime.now(central).strftime('%d%m%y-%H-%M')
24
+ file_name = f"phone-{timestamp}.txt"
25
+ with open(file_name, 'w') as file:
26
+ file.write(f"{password_hash}\n")
27
+ return file_name
28
+
29
+ # Function to check if user is authenticated
30
+ def is_user_authenticated(phone_number, hash_value):
31
+ for file_name in os.listdir():
32
+ if file_name.startswith('phone-') and phone_number in file_name:
33
+ with open(file_name, 'r') as file:
34
+ stored_hash = file.readline().strip()
35
+ if stored_hash == hash_value:
36
+ return True
37
+ return False
38
+
39
+ # Initialize session state
40
+ if 'phone_number' not in st.session_state:
41
+ st.session_state['phone_number'] = '+19522583980' # Default phone number
42
+ if 'password' not in st.session_state:
43
+ st.session_state['password'] = ''
44
+ if 'hash' not in st.session_state:
45
+ st.session_state['hash'] = ''
46
+ if 'authenticated' not in st.session_state:
47
+ st.session_state['authenticated'] = False
48
+
49
+ # Sidebar inputs
50
+ user_phone_input = st.sidebar.text_input("πŸ“± Mobile Phone", value=st.session_state.get('phone_number', ''))
51
+ password_input = st.sidebar.text_input("πŸ”‘ Set Password", type='password')
52
+
53
+ # Save button
54
+ if st.sidebar.button('πŸ’Ύ Save Settings'):
55
+ st.session_state['phone_number'] = format_phone_number(user_phone_input)
56
+ hashed_password = hash_password(password_input)
57
+ st.session_state['password'] = hashed_password
58
+ file_name = save_user_data(st.session_state['phone_number'], hashed_password)
59
+ st.sidebar.success(f"Settings saved successfully! Data saved in {file_name}")
60
+ # Record save event with timestamp
61
+ st.session_state['save_time'] = datetime.now(central).strftime('%Y-%m-%d %H:%M:%S')
62
+
63
+
64
+ # Function to send verification SMS
65
+ def send_verification_sms():
66
+
67
+ # Load the API key from an environment variable
68
+ api_key = os.getenv('API_KEY')
69
+ tf.useKey(api_key)
70
+ #tf.sendSMS("+19522583980", "Test 2")
71
+
72
+ base_url = "https://huggingface.co/spaces/awacke1/RT-SMS-Phone-Verify"
73
+ # Correct the sendSMS function call
74
+ #phone=st.session_state['phone_number']
75
+ phone=user_phone_input
76
+ st.write('Sending SMS to phone number ' + phone)
77
+
78
+ hashmessage=f"Verify here: {base_url}?hash={st.session_state['password']}"
79
+ st.write('Hash message: ' + hashmessage)
80
+
81
+ result = tf.sendSMS(phone, hashmessage)
82
+
83
+ if(result.ok):
84
+ print(result.data)
85
+ st.sidebar.success("Verification link sent via SMS")
86
+ else:
87
+ print(result.message)
88
+
89
+
90
+ # URL hash handling
91
+ query_params = st.experimental_get_query_params()
92
+ if 'hash' in query_params:
93
+ st.session_state['hash'] = query_params['hash'][0]
94
+ st.session_state['authenticated'] = is_user_authenticated(st.session_state['phone_number'], st.session_state['hash'])
95
+ if st.session_state['authenticated']:
96
+ st.write("βœ… User has authenticated using text")
97
+
98
+ # Display the main area if authenticated
99
+ if st.session_state['authenticated']:
100
+ st.write("## Main Area")
101
+ # [Display main area content]
102
+ else:
103
+ if st.sidebar.button('πŸ“¨ Send Verify Request'):
104
+ send_verification_sms()
105
+
106
+ # Display history for the specific phone number
107
+ st.write("## πŸ“œ Verification History")
108
+ history_files = [f for f in os.listdir() if f.startswith('phone-') and st.session_state['phone_number'] in f]
109
+ for file_name in history_files:
110
+ with open(file_name, 'r') as file:
111
+ st.write(f"{file_name}: {file.read()}")