awacke1 commited on
Commit
cd702ea
β€’
1 Parent(s): 52e59ce

Create backup.app.py

Browse files
Files changed (1) hide show
  1. backup.app.py +91 -0
backup.app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import hashlib
3
+ import streamlit as st
4
+ import textflowsms as tf
5
+
6
+
7
+ # Function to ensure phone number is correctly formatted
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
+ # Initialize session state variables if they don't exist
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
+ # Function to hash a password
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
+ # Button to save the phone number and password
41
+ if st.sidebar.button('πŸ’Ύ Save Settings'):
42
+ st.session_state['phone_number'] = format_phone_number(user_phone_input)
43
+ st.session_state['password'] = hash_password(password_input)
44
+ st.sidebar.success("Settings saved successfully!")
45
+
46
+ # Function to send verification SMS
47
+ def send_verification_sms():
48
+
49
+ # Load the API key from an environment variable
50
+ api_key = os.getenv('API_KEY')
51
+ tf.useKey(api_key)
52
+ #tf.sendSMS("+19522583980", "Test 2")
53
+
54
+ base_url = "https://huggingface.co/spaces/awacke1/RT-SMS-Phone-Verify"
55
+ # Correct the sendSMS function call
56
+ #phone=st.session_state['phone_number']
57
+ phone=user_phone_input
58
+ st.write('Sending SMS to phone number ' + phone)
59
+
60
+ hashmessage=f"Verify here: {base_url}?hash={st.session_state['password']}"
61
+ st.write('Hash message: ' + hashmessage)
62
+
63
+ result = tf.sendSMS(phone, hashmessage)
64
+
65
+ if(result.ok):
66
+ print(result.data)
67
+ st.sidebar.success("Verification link sent via SMS")
68
+ else:
69
+ print(result.message)
70
+
71
+
72
+ # Check if the hash parameter is provided
73
+ query_params = st.experimental_get_query_params()
74
+ if 'hash' in query_params:
75
+ st.session_state['hash'] = query_params['hash'][0]
76
+ append_to_log(st.session_state['phone_number'])
77
+ st.write("βœ… User has authenticated using text")
78
+
79
+ # Show verification button if hash is not in session
80
+ if not st.session_state['hash']:
81
+ if st.sidebar.button('πŸ“¨ Send Verify Request'):
82
+ send_verification_sms()
83
+
84
+ # Display history log
85
+ st.write("## πŸ“œ Verification History")
86
+ if os.path.exists('LoggedConfirmations.txt'):
87
+ with open('LoggedConfirmations.txt', 'r') as log_file:
88
+ history = log_file.read()
89
+ st.text(history)
90
+ else:
91
+ st.write("No verification history found.")