From fda603e0c0d3bca6442b2d14e5b810af06581724 Mon Sep 17 00:00:00 2001 From: amazing-username Date: Mon, 20 Mar 2023 00:24:46 +0000 Subject: [PATCH] Initial commit --- README.txt | 1 + main.py | 137 ++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 + send_messages.sh | 21 +++++++ twilio_details.json | 8 +++ 5 files changed, 170 insertions(+) create mode 100644 README.txt create mode 100644 main.py create mode 100644 requirements.txt create mode 100755 send_messages.sh create mode 100644 twilio_details.json diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..3d2628c --- /dev/null +++ b/README.txt @@ -0,0 +1 @@ +# Install twilio diff --git a/main.py b/main.py new file mode 100644 index 0000000..1d0515e --- /dev/null +++ b/main.py @@ -0,0 +1,137 @@ +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() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..766cdba --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +twilio==7.5.1 +pyexcel==0.6.2 +pyexcel_xlsx==0.6.0 diff --git a/send_messages.sh b/send_messages.sh new file mode 100755 index 0000000..d747bae --- /dev/null +++ b/send_messages.sh @@ -0,0 +1,21 @@ +#!/usr/bin/bash + + +root_dir="/home/phoenix/programming/python/experiments/twilo_demo" + +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" + +python main.py "$numbers_path" "$type_message" "$messages_path" "$send_message" + +echo "Done" diff --git a/twilio_details.json b/twilio_details.json new file mode 100644 index 0000000..98d98d5 --- /dev/null +++ b/twilio_details.json @@ -0,0 +1,8 @@ +[ + { + "account_sid": "account_sid", + "service_sid": "service_sid", + "auth_token": "auth_token", + "number": "0123456789" + } +]