-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
65 lines (51 loc) · 1.71 KB
/
bot.py
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
import os
from twilio.rest import Client
from dotenv import load_dotenv
import sqlite3
from time import sleep
load_dotenv()
account_sid = os.getenv('TWILIO_ACCOUNT_SID')
auth_token = os.getenv('TWILIO_AUTH_TOKEN')
twilio_number = os.getenv('TWILIO_NUMBER')
my_wp_number= os.getenv('MY_WP_NUMBER')
if not account_sid or not auth_token or not twilio_number:
raise ValueError("Environment variables were not loaded correctly.")
client = Client(account_sid, auth_token)
conn = sqlite3.connect('responses.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS saved_contacts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
phone TEXT NOT NULL
)
''')
conn.commit()
def send_message(phone, message):
try:
client.messages.create(
from_=twilio_number,
body=message,
to=f'whatsapp:{phone}',
)
except Exception as e:
print(f"Error sending message to {phone}: {e}")
def save_contact(phone):
try:
cursor.execute('INSERT INTO saved_contacts (phone) VALUES (?)', (phone,))
conn.commit()
except Exception as e:
print(f"Error saving contact {phone}: {e}")
phones = [my_wp_number, '+5511995522666']
initial_message = "Would you like to participate in our promotion? Reply with Yes or No."
for phone in phones:
send_message(phone, initial_message)
print(f"Message sent to {phone}")
answer = input(f"Simulation: Did the user {phone} respond Yes or No? ")
if answer.lower() == 'yes':
save_contact(phone)
print(f"Contact {phone} saved to the database.")
elif answer.lower() == 'no':
print(f"Message cycle ended for {phone}.")
sleep(1)
cursor.close()
conn.close()