Refactoring (#5)
Reviewed-on: phoenix/twilio_sender#5 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
-137
@@ -1,137 +0,0 @@
|
|||||||
import sys
|
|
||||||
import json
|
|
||||||
import time
|
|
||||||
|
|
||||||
from twilio.rest import Client
|
|
||||||
import pyexcel
|
|
||||||
import pyexcel_xlsx
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class MYTwilioConfig(object):
|
|
||||||
|
|
||||||
def __init__(self, account_sid=None, service_sid=None, token=None, number=None):
|
|
||||||
self.account_sid = account_sid
|
|
||||||
self.service_sid = service_sid
|
|
||||||
self.auth_token = token
|
|
||||||
self.phone_number = number
|
|
||||||
|
|
||||||
account_sid = ""
|
|
||||||
service_sid = ""
|
|
||||||
auth_token = ""
|
|
||||||
phone_number = ""
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_twilio_config(account_sid="ACefa1ef516314c9d1a68cbd657de49277", service_sid="MG803f3676706b92eb02e18dd820c447f2", token="f4a1f2b0b79ea3735078c2d8ee9684e1", number="12243026041"):
|
|
||||||
config = MYTwilioConfig(account_sid, service_sid, token, number)
|
|
||||||
|
|
||||||
return config
|
|
||||||
|
|
||||||
|
|
||||||
def json_file_to_object(filepath):
|
|
||||||
json_data = json.load(open(filepath))
|
|
||||||
|
|
||||||
return json.dumps(json_data)
|
|
||||||
|
|
||||||
|
|
||||||
def test_phone_number_lists(file_path, file_type):
|
|
||||||
phone_lists = []
|
|
||||||
|
|
||||||
if file_type == "excel":
|
|
||||||
print("Chose excel: %s" % file_path)
|
|
||||||
records = pyexcel.get_records(file_name=file_path)
|
|
||||||
# records = pyexcel_xlsx.get_data(file_path)
|
|
||||||
|
|
||||||
for record in records:
|
|
||||||
number = record['To']
|
|
||||||
|
|
||||||
phone_lists.append(number)
|
|
||||||
elif file_type == "json":
|
|
||||||
json_data = json.load(open(file_path))
|
|
||||||
|
|
||||||
for data in json_data:
|
|
||||||
print(data)
|
|
||||||
phone_lists.append(data)
|
|
||||||
|
|
||||||
return phone_lists
|
|
||||||
|
|
||||||
def load_message(filepath):
|
|
||||||
json_data = json.load(open(filepath))
|
|
||||||
|
|
||||||
message = json_data[0]
|
|
||||||
|
|
||||||
return message
|
|
||||||
|
|
||||||
|
|
||||||
def send_message(config, number_list, body, send, sleep_sec_amount = 5):
|
|
||||||
client = Client(config.account_sid, config.auth_token)
|
|
||||||
|
|
||||||
|
|
||||||
print("Message to send: %s\n\n" % (body))
|
|
||||||
amount = 0
|
|
||||||
|
|
||||||
for number in number_list:
|
|
||||||
if send:
|
|
||||||
|
|
||||||
print("Sending message to %s" % (number))
|
|
||||||
message = client.messages.create(
|
|
||||||
messaging_service_sid=config.service_sid,
|
|
||||||
body=body,
|
|
||||||
to=number
|
|
||||||
)
|
|
||||||
|
|
||||||
amount += 1
|
|
||||||
|
|
||||||
if amount % 10 == 0:
|
|
||||||
time.sleep(sleep_sec_amount)
|
|
||||||
else:
|
|
||||||
time.sleep(2)
|
|
||||||
else:
|
|
||||||
print("Not sending message to %s" % number)
|
|
||||||
|
|
||||||
def test_message():
|
|
||||||
message = "WEAREOBSESSEDWITHHEALING.WEVOWTOFAIL. SUMOFTHEBROTHERSFORTHESISTAS. FLORIDAMAN EGGROLLS! DIRKTHEDEATHROWBANDIT. KAVAKAT 9 PM SATURDAY 7/31."
|
|
||||||
|
|
||||||
|
|
||||||
return message
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
print("twilio_demo")
|
|
||||||
|
|
||||||
config = test_twilio_config()
|
|
||||||
message = test_message()
|
|
||||||
should_send = False
|
|
||||||
json_phone_path = ""
|
|
||||||
json_message_path = ""
|
|
||||||
file_type = "json"
|
|
||||||
|
|
||||||
if len(sys.argv) <= 2:
|
|
||||||
sys.exit(-1)
|
|
||||||
|
|
||||||
if len(sys.argv) == 5:
|
|
||||||
json_phone_path = sys.argv[1]
|
|
||||||
file_type = sys.argv[2]
|
|
||||||
json_message_path = sys.argv[3]
|
|
||||||
should_send = True if sys.argv[4] == 'True' else False
|
|
||||||
|
|
||||||
print("source file path: %s" % json_phone_path)
|
|
||||||
print("file type: %s" % file_type)
|
|
||||||
print("message path: %s" % json_message_path)
|
|
||||||
print(should_send)
|
|
||||||
else:
|
|
||||||
print("Didn't provide enough arguments")
|
|
||||||
sys.exit(-1)
|
|
||||||
|
|
||||||
|
|
||||||
print("Getting list of numbers")
|
|
||||||
numbers = test_phone_number_lists(json_phone_path, file_type)
|
|
||||||
print("Loading message")
|
|
||||||
message = load_message(json_message_path)
|
|
||||||
|
|
||||||
send_message(config, numbers, message, should_send, 5)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
twilio==7.5.1
|
|
||||||
pyexcel==0.6.2
|
|
||||||
pyexcel_xlsx==0.6.0
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
#!/usr/bin/bash
|
|
||||||
|
|
||||||
|
|
||||||
root_dir="/home/phoenix/programming/python/experiments/twilo_demo"
|
|
||||||
|
|
||||||
echo "Root directory: $root_dir"
|
|
||||||
|
|
||||||
if [[ ! -d "$root_dir" ]]; then
|
|
||||||
echo "Root directory does not exist. Exiting"
|
|
||||||
exit
|
|
||||||
fi
|
|
||||||
|
|
||||||
config_path="twilio_details.json"
|
|
||||||
numbers_part="60"
|
|
||||||
numbers_path="numbers/numbers-$numbers_part.json"
|
|
||||||
# numbers_path="numbers/test_number.json"
|
|
||||||
type_message="json"
|
|
||||||
messages_path="messages/message-$numbers_part.json"
|
|
||||||
send_message="True"
|
|
||||||
|
|
||||||
echo "Changing directory to $root_dir"
|
|
||||||
cd $root_dir
|
|
||||||
|
|
||||||
# source env/bin/activate
|
|
||||||
# echo "Activated virtual environment"
|
|
||||||
|
|
||||||
# Old
|
|
||||||
# python main.py "$numbers_path" "$type_message" "$messages_path" "$send_message"
|
|
||||||
./twilio_sender "$config_path" "$numbers_path" "$messages_path"
|
|
||||||
|
|
||||||
echo "Done"
|
|
||||||
Reference in New Issue
Block a user