File size: 4,043 Bytes
68a9462
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import hashlib
import streamlit as st
import textflowsms as tf
from datetime import datetime

# Central Time Zone Adjustment
import pytz
central = pytz.timezone('US/Central')

# Function to format phone number
def format_phone_number(phone_number):
    if len(phone_number) == 10 and not phone_number.startswith('+'):
        return '+1' + phone_number
    return phone_number

# Function to hash a password
def hash_password(password):
    return hashlib.sha256(password.encode()).hexdigest()

# Function to save user data to a file
def save_user_data(phone_number, password_hash):
    timestamp = datetime.now(central).strftime('%d%m%y-%H-%M')
    file_name = f"phone-{timestamp}.txt"
    with open(file_name, 'w') as file:
        file.write(f"{password_hash}\n")
    return file_name

# Function to check if user is authenticated
def is_user_authenticated(phone_number, hash_value):
    for file_name in os.listdir():
        if file_name.startswith('phone-') and phone_number in file_name:
            with open(file_name, 'r') as file:
                stored_hash = file.readline().strip()
                if stored_hash == hash_value:
                    return True
    return False

# Initialize session state
if 'phone_number' not in st.session_state:
    st.session_state['phone_number'] = '+19522583980'  # Default phone number
if 'password' not in st.session_state:
    st.session_state['password'] = ''
if 'hash' not in st.session_state:
    st.session_state['hash'] = ''
if 'authenticated' not in st.session_state:
    st.session_state['authenticated'] = False

# Sidebar inputs
user_phone_input = st.sidebar.text_input("πŸ“± Mobile Phone", value=st.session_state.get('phone_number', ''))
password_input = st.sidebar.text_input("πŸ”‘ Set Password", type='password')

# Save button
if st.sidebar.button('πŸ’Ύ Save Settings'):
    st.session_state['phone_number'] = format_phone_number(user_phone_input)
    hashed_password = hash_password(password_input)
    st.session_state['password'] = hashed_password
    file_name = save_user_data(st.session_state['phone_number'], hashed_password)
    st.sidebar.success(f"Settings saved successfully! Data saved in {file_name}")
    # Record save event with timestamp
    st.session_state['save_time'] = datetime.now(central).strftime('%Y-%m-%d %H:%M:%S')


# Function to send verification SMS
def send_verification_sms():
    
    # Load the API key from an environment variable
    api_key = os.getenv('API_KEY')
    tf.useKey(api_key)
    #tf.sendSMS("+19522583980", "Test 2")

    base_url = "https://huggingface.co/spaces/awacke1/RT-SMS-Phone-Verify"
    # Correct the sendSMS function call
    #phone=st.session_state['phone_number']
    phone=user_phone_input
    st.write('Sending SMS to phone number ' + phone)
    
    hashmessage=f"Verify here: {base_url}?hash={st.session_state['password']}"
    st.write('Hash message: ' + hashmessage)
    
    result = tf.sendSMS(phone, hashmessage)
    
    if(result.ok):
        print(result.data)
        st.sidebar.success("Verification link sent via SMS")
    else:
      print(result.message)


# URL hash handling
query_params = st.experimental_get_query_params()
if 'hash' in query_params:
    st.session_state['hash'] = query_params['hash'][0]
    st.session_state['authenticated'] = is_user_authenticated(st.session_state['phone_number'], st.session_state['hash'])
    if st.session_state['authenticated']:
        st.write("βœ… User has authenticated using text")

# Display the main area if authenticated
if st.session_state['authenticated']:
    st.write("## Main Area")
    # [Display main area content]
else:
    if st.sidebar.button('πŸ“¨ Send Verify Request'):
        send_verification_sms()

# Display history for the specific phone number
st.write("## πŸ“œ Verification History")
history_files = [f for f in os.listdir() if f.startswith('phone-') and st.session_state['phone_number'] in f]
for file_name in history_files:
    with open(file_name, 'r') as file:
        st.write(f"{file_name}: {file.read()}")