Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,89 +2,88 @@ import os
|
|
2 |
import hashlib
|
3 |
import streamlit as st
|
4 |
import textflowsms as tf
|
|
|
5 |
|
|
|
|
|
|
|
6 |
|
7 |
-
# Function to
|
8 |
def format_phone_number(phone_number):
|
9 |
if len(phone_number) == 10 and not phone_number.startswith('+'):
|
10 |
return '+1' + phone_number
|
11 |
return phone_number
|
12 |
|
13 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
if 'phone_number' not in st.session_state:
|
15 |
st.session_state['phone_number'] = '+19522583980' # Default phone number
|
16 |
if 'password' not in st.session_state:
|
17 |
st.session_state['password'] = ''
|
18 |
if 'hash' not in st.session_state:
|
19 |
st.session_state['hash'] = ''
|
|
|
|
|
20 |
|
21 |
-
#
|
22 |
-
def hash_password(password):
|
23 |
-
return hashlib.sha256(password.encode()).hexdigest()[:6]
|
24 |
-
|
25 |
-
# Function to append to the log file or create it if it doesn't exist
|
26 |
-
def append_to_log(phone_number):
|
27 |
-
if not os.path.exists('LoggedConfirmations.txt'):
|
28 |
-
with open('LoggedConfirmations.txt', 'w') as log_file:
|
29 |
-
log_file.write(f"{phone_number}\n")
|
30 |
-
else:
|
31 |
-
with open('LoggedConfirmations.txt', 'a') as log_file:
|
32 |
-
log_file.write(f"{phone_number}\n")
|
33 |
-
|
34 |
-
# Mobile Phone field with emoji
|
35 |
user_phone_input = st.sidebar.text_input("π± Mobile Phone", value=st.session_state.get('phone_number', ''))
|
36 |
-
|
37 |
-
# Password field with emoji
|
38 |
password_input = st.sidebar.text_input("π Set Password", type='password')
|
39 |
|
40 |
-
#
|
41 |
if st.sidebar.button('πΎ Save Settings'):
|
42 |
st.session_state['phone_number'] = format_phone_number(user_phone_input)
|
43 |
-
|
44 |
-
st.
|
|
|
|
|
|
|
|
|
45 |
|
46 |
# Function to send verification SMS
|
47 |
def send_verification_sms():
|
48 |
-
|
49 |
-
#
|
50 |
-
api_key = os.getenv('API_KEY')
|
51 |
-
tf.useKey(api_key)
|
52 |
-
base_url = "https://huggingface.co/spaces/awacke1/RT-SMS-Phone-Verify"
|
53 |
-
base_url2 = "huggingface.co/spaces/awacke1/RT-SMS-Phone-Verify" # SMS - remove the https:// since SMS only likes plain text links without protocol
|
54 |
-
phone=user_phone_input
|
55 |
-
phone_session=st.session_state['phone_number']
|
56 |
-
st.write('Sending SMS to phone number ' + phone)
|
57 |
-
hashmessage=f"Verify here: {base_url}?hash={st.session_state['password']}"
|
58 |
-
hashmessage2=f"Verify here {base_url}?hash={st.session_state['password']}"
|
59 |
-
#st.write('Hash message: ' + hashmessage)
|
60 |
-
result = tf.sendSMS(phone, hashmessage2)
|
61 |
-
|
62 |
-
if(result.ok):
|
63 |
-
st.write('Success with result data:')
|
64 |
-
st.write(result.data)
|
65 |
-
st.sidebar.success("Verification link sent via SMS")
|
66 |
-
else:
|
67 |
-
st.write('Failure with result message:' + result.message)
|
68 |
-
#st.sidebar.success("Verification link sent via SMS")
|
69 |
|
70 |
-
|
71 |
-
# Check if the hash parameter is provided
|
72 |
query_params = st.experimental_get_query_params()
|
73 |
if 'hash' in query_params:
|
74 |
st.session_state['hash'] = query_params['hash'][0]
|
75 |
-
|
76 |
-
st.
|
|
|
77 |
|
78 |
-
#
|
79 |
-
if
|
|
|
|
|
|
|
80 |
if st.sidebar.button('π¨ Send Verify Request'):
|
81 |
send_verification_sms()
|
82 |
|
83 |
-
# Display history
|
84 |
st.write("## π Verification History")
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
else:
|
90 |
-
st.write("No verification history found.")
|
|
|
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 |
# Function to send verification SMS
|
64 |
def send_verification_sms():
|
65 |
+
# [SMS sending logic remains the same]
|
66 |
+
# ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
# URL hash handling
|
|
|
69 |
query_params = st.experimental_get_query_params()
|
70 |
if 'hash' in query_params:
|
71 |
st.session_state['hash'] = query_params['hash'][0]
|
72 |
+
st.session_state['authenticated'] = is_user_authenticated(st.session_state['phone_number'], st.session_state['hash'])
|
73 |
+
if st.session_state['authenticated']:
|
74 |
+
st.write("β
User has authenticated using text")
|
75 |
|
76 |
+
# Display the main area if authenticated
|
77 |
+
if st.session_state['authenticated']:
|
78 |
+
st.write("## Main Area")
|
79 |
+
# [Display main area content]
|
80 |
+
else:
|
81 |
if st.sidebar.button('π¨ Send Verify Request'):
|
82 |
send_verification_sms()
|
83 |
|
84 |
+
# Display history for the specific phone number
|
85 |
st.write("## π Verification History")
|
86 |
+
history_files = [f for f in os.listdir() if f.startswith('phone-') and st.session_state['phone_number'] in f]
|
87 |
+
for file_name in history_files:
|
88 |
+
with open(file_name, 'r') as file:
|
89 |
+
st.write(f"{file_name}: {file.read()}")
|
|
|
|