Spaces:
Sleeping
Sleeping
File size: 3,494 Bytes
5812d71 2180627 5812d71 26254a2 5812d71 77ac838 5812d71 883a1c3 5812d71 2180627 5812d71 883a1c3 5812d71 03ff0b1 5812d71 77ac838 b8b6e7a 7e17b42 7d04492 b8b6e7a 52e59ce b8b6e7a 7e17b42 7d04492 52e59ce 7d04492 a66ff64 1de8b1c a66ff64 1d948f4 5812d71 a66ff64 77ac838 883a1c3 77ac838 883a1c3 77ac838 883a1c3 03ff0b1 |
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 |
import os
import hashlib
import streamlit as st
import textflowsms as tf
# Function to ensure phone number is correctly formatted
def format_phone_number(phone_number):
if len(phone_number) == 10 and not phone_number.startswith('+'):
return '+1' + phone_number
return phone_number
# Initialize session state variables if they don't exist
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'] = ''
# Function to hash a password
def hash_password(password):
return hashlib.sha256(password.encode()).hexdigest()[:6]
# Function to append to the log file or create it if it doesn't exist
def append_to_log(phone_number):
if not os.path.exists('LoggedConfirmations.txt'):
with open('LoggedConfirmations.txt', 'w') as log_file:
log_file.write(f"{phone_number}\n")
else:
with open('LoggedConfirmations.txt', 'a') as log_file:
log_file.write(f"{phone_number}\n")
# Mobile Phone field with emoji
user_phone_input = st.sidebar.text_input("π± Mobile Phone", value=st.session_state.get('phone_number', ''))
# Password field with emoji
password_input = st.sidebar.text_input("π Set Password", type='password')
# Button to save the phone number and password
if st.sidebar.button('πΎ Save Settings'):
st.session_state['phone_number'] = format_phone_number(user_phone_input)
st.session_state['password'] = hash_password(password_input)
st.sidebar.success("Settings saved successfully!")
# 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)
base_url = "https://huggingface.co/spaces/awacke1/RT-SMS-Phone-Verify"
base_url2 = "huggingface.co/spaces/awacke1/RT-SMS-Phone-Verify" # SMS - remove the https:// since SMS only likes plain text links without protocol
phone=user_phone_input
phone_session=st.session_state['phone_number']
st.write('Sending SMS to phone number ' + phone)
hashmessage=f"Verify here: {base_url}?hash={st.session_state['password']}"
hashmessage2=f"Verify here {base_url}?hash={st.session_state['password']}"
#st.write('Hash message: ' + hashmessage)
result = tf.sendSMS(phone, hashmessage2)
if(result.ok):
st.write('Success with result data:')
st.write(result.data)
st.sidebar.success("Verification link sent via SMS")
else:
st.write('Failure with result message:' + result.message)
#st.sidebar.success("Verification link sent via SMS")
# Check if the hash parameter is provided
query_params = st.experimental_get_query_params()
if 'hash' in query_params:
st.session_state['hash'] = query_params['hash'][0]
append_to_log(st.session_state['phone_number'])
st.write("β
User has authenticated using text")
# Show verification button if hash is not in session
if not st.session_state['hash']:
if st.sidebar.button('π¨ Send Verify Request'):
send_verification_sms()
# Display history log
st.write("## π Verification History")
if os.path.exists('LoggedConfirmations.txt'):
with open('LoggedConfirmations.txt', 'r') as log_file:
history = log_file.read()
st.text(history)
else:
st.write("No verification history found.") |